Skip to content

Commit 8e728d6

Browse files
author
Matt
committed
Merge branch 'feature/python_list'
Merging updated python lib Added support for latest pyo3 Fixed zip reading from cli Improved crate context handling for rocraters Added basic validation dict for rocraters
2 parents 25bcab8 + 161d534 commit 8e728d6

11 files changed

Lines changed: 4696 additions & 89 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ro-crate-rs-cli"
3-
version = "0.4.11"
3+
version = "0.4.12"
44
edition = "2021"
55
repository = "https://github.com/intbio-ncl/ro-crate-rs"
66
authors = ["Matt Burridge <m.burridge1@newcastle.ac.uk>"]

cli/src/args.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ pub struct ZipCrateCommand {
226226

227227
#[derive(Debug, Subcommand)]
228228
pub enum ReadCommand {
229+
/// Read full crate from zip archive
230+
Zip(ReadZipCommand),
229231
/// Read full crate
230232
Crate(ReadCrateCommand),
231233
/// Read entity of crate
@@ -257,6 +259,25 @@ pub struct ReadCrateCommand {
257259
pub json: bool,
258260
}
259261

262+
#[derive(Debug, Args)]
263+
pub struct ReadZipCommand {
264+
/// Target zip archive
265+
#[clap(short, long, required = true)]
266+
pub target_zip: String,
267+
/// Validation level for schema checks (0-2)
268+
#[clap(short = 'l', long, default_value_t = 0)]
269+
pub validation_level: i8,
270+
/// Raw struct data
271+
#[clap(short, long)]
272+
pub raw_struct: bool,
273+
/// Prints full view without trimming
274+
#[clap(short, long)]
275+
pub fit: bool,
276+
/// Prints as json
277+
#[clap(short, long)]
278+
pub json: bool,
279+
}
280+
260281
/// TODO: Add a field to recursively show all linked ids
261282
#[derive(Debug, Args)]
262283
pub struct ReadEntityCommand {

cli/src/main.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,34 @@ fn main() {
155155
}
156156
},
157157
CrateAction::Read(read_command) => match read_command {
158+
ReadCommand::Zip(read_zip_command) => {
159+
let rocrate = open_and_load_zip_crate(
160+
&read_zip_command.target_zip,
161+
read_zip_command.validation_level,
162+
);
163+
164+
if read_zip_command.raw_struct {
165+
println!("{:#?}", rocrate);
166+
return;
167+
}
168+
if read_zip_command.json {
169+
println!("{}", to_string_pretty(&rocrate).unwrap());
170+
} else {
171+
match to_string_pretty(&rocrate) {
172+
Ok(_json_ld) => {
173+
let mut table = json_to_table(&json!(&rocrate.graph)).into_table();
174+
table.with(Style::modern_rounded());
175+
if read_zip_command.fit {
176+
table.modify(Rows::new(1..), Width::truncate(200).suffix("..."));
177+
} else {
178+
table.modify(Rows::new(1..), Width::truncate(79).suffix("..."));
179+
}
180+
println!("{}", table)
181+
}
182+
Err(e) => eprintln!("Failed to display crate: {}", e),
183+
}
184+
}
185+
}
158186
ReadCommand::Crate(read_crate_command) => {
159187
let rocrate = open_and_load_crate(&read_crate_command.target_crate);
160188

@@ -286,13 +314,7 @@ fn main() {
286314
/// Input requires target_crate file string
287315
fn open_and_load_crate(input: &str) -> RoCrate {
288316
if input.ends_with(".zip") {
289-
match parse_zip(input, 0) {
290-
Ok(ro_crate) => ro_crate,
291-
Err(e) => {
292-
eprintln!("Error processing crate: {:?}", e);
293-
std::process::exit(1)
294-
}
295-
}
317+
open_and_load_zip_crate(input, 0)
296318
} else {
297319
let target_crate = crate_path(input);
298320
match read_crate(&target_crate, 1) {
@@ -305,6 +327,26 @@ fn open_and_load_crate(input: &str) -> RoCrate {
305327
}
306328
}
307329

330+
fn open_and_load_zip_crate(input: &str, validation_level: i8) -> RoCrate {
331+
if !input.ends_with(".zip") {
332+
eprintln!("Target archive must end with .zip");
333+
std::process::exit(1)
334+
}
335+
336+
if !(0..=2).contains(&validation_level) {
337+
eprintln!("Validation level must be between 0 and 2");
338+
std::process::exit(1)
339+
}
340+
341+
match parse_zip(input, validation_level) {
342+
Ok(ro_crate) => ro_crate,
343+
Err(e) => {
344+
eprintln!("Error processing zip crate: {:?}", e);
345+
std::process::exit(1)
346+
}
347+
}
348+
}
349+
308350
fn create_rocrate_with_context(context_type: ContextType) -> RoCrate {
309351
match context_type {
310352
ContextType::Reference => {

0 commit comments

Comments
 (0)