Skip to content

Commit

Permalink
working updates for graph-vector refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mburridge96 committed Nov 20, 2024
1 parent bdbc674 commit e499672
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 296 deletions.
253 changes: 1 addition & 252 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,255 +4,4 @@ The ro-crate-rs cli tool can be used to immediately interface with RO-Crates wit

# Basics

This cli allows basic interaction with an RO-Crate, such as reading, writing, updating and so on.

## Initial commands

```bash
Usage: rocrate <COMMAND>

Commands:
init Initialise a new empty Ro-Crate
add Add an entity to an Ro-Crate
delete Delete an entity in an Ro-Crate
modify Modify a particular entity within an Ro-Crate (includes Root and Descriptor)
read Read the crate and display
package Allows you to package crate into different formats
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
-V, --version Print version
```

## Init command

```bash
Initialise a new empty Ro-Crate

Usage: rocrate init [OPTIONS]

Options:
-d, --default Default ro-crate initialisation using latest spec
-c, --context-type <context> Type of context:
1 - Reference Context: Basic context type with minimal fields.
2 - Extended Context: Includes additional metadata fields.
3 - Embedded Context: Contains embedded data for richer context.
-m, --minimal Initialise with default minimal entites or leave empty
-h, --help Print help
```

## Add command
```bash
Add an entity to an Ro-Crate

Usage: rocrate add [OPTIONS] <ID> [DATATYPE]...

Arguments:
<ID> Input ID
[DATATYPE]... Input Datatype. This can be a single datatype, or a list seperated by ',' (e.g type1,type2)

Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-e, --extra-information Extra information. Allows you to input Custom fields and values
-h, --help Print help
```
## Delete command
```bash
Delete an entity in an Ro-Crate

Usage: rocrate delete [OPTIONS] <ID>

Arguments:
<ID> Input ID to delete

Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-r, --recursive Recursive deletion. Allows you to delete all occurances of the ID linked in other research objects
-h, --help Print help
```

## Modify command
```bash
Modify a particular entity within an Ro-Crate (includes Root and Descriptor)

Usage: rocrate modify <COMMAND>

Commands:
add-string Add a string value to an entity
add-id-value Add a ID to an entity
add-id-vec-values Add a list of ID's to an entity
add-multiple Add multiple new fields to entity - Useful for large crates
remove-field Remove a specific field from an entity
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
```
### add-string
```bash
Add a string value to an entity
Usage: rocrate modify add-string [OPTIONS] <ID> <KEY> <VALUE>
Arguments:
<ID> Input ID to add to
<KEY> Key to add
<VALUE> Value to add
Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-h, --help Print help
```
### add-id-value
```bash
Add a ID to an entity
Usage: rocrate modify add-id-value [OPTIONS] <ID> <KEY> <VALUE>
Arguments:
<ID> Input ID to target
<KEY> Key to add
<VALUE> ID value to add
Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-h, --help Print help
```
### add-id-vec-values
```bash
Add a list of ID's to an entity

Usage: rocrate modify add-id-vec-values [OPTIONS] <ID> <KEY> [VALUES]...

Arguments:
<ID> Input ID to target
<KEY> Key to add
[VALUES]... ID values to add. This can be a single id, or a list seperated by ',' (e.g id1,id2)

Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-h, --help Print help

```
### add-multiple
```bash
Add multiple new fields to entity - Useful for large crates

Usage: rocrate modify add-multiple [OPTIONS] <ID>

Arguments:
<ID> Input ID to target

Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-h, --help Print help
```
### remove-field
```bash
Remove a specific field from an entity

Usage: rocrate modify remove-field [OPTIONS] <ID> <FIELD>

Arguments:
<ID> Input ID to delete
<FIELD> Field to delete

Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-h, --help Print help
```

## Read command
```bash
Read the crate and display

Usage: rocrate read <COMMAND>

Commands:
crate Read full crate
entity Read entity of crate
fields Read all of one field in crate
value Read entity containing specific value
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
```

### crate
```bash
Read full crate

Usage: rocrate read crate [OPTIONS]

Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-r, --raw-struct Raw struct data
-h, --help Print help
```
### entity
```bash
Read entity of crate

Usage: rocrate read entity [OPTIONS] <ID>

Arguments:
<ID> Entity ID to search

Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-r, --raw-struct Raw struct data
-h, --help Print help
```
### fields
```bash
Read all of one field in crate

Usage: rocrate read fields [OPTIONS] <FIELD>

Arguments:
<FIELD> Field to search for

Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-h, --help Print help
```
### value
```bash
Read entity containing specific value

Usage: rocrate read value [OPTIONS] <VALUE>

Arguments:
<VALUE> Field to search for

Options:
-t, --target-crate <TARGET_CRATE> Target crate [default: ro-crate-metadata.json]
-l, --location Show the object location
-h, --help Print help
```
## Package command
```bash
Allows you to package crate into different formats

Usage: rocrate package <COMMAND>

Commands:
zip Read full crate
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
```
### zip
```bash
Read full crate

Usage: rocrate package zip [OPTIONS]

Options:
-t, --target-folder <TARGET_FOLDER> [default: ./]
-h, --help Print help
```

This cli allows basic interaction with an RO-Crate, such as reading, writing, updating, validating and so on.
19 changes: 7 additions & 12 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,8 @@ fn main() {
fn open_and_load_crate(input: &str) -> RoCrate {
let target_crate = crate_path(input);
match read_crate(&target_crate, 1) {
Ok(ro_crate) => {
// Process ro_crate if read successfully
// ...
ro_crate
}
Ok(ro_crate) => ro_crate,
Err(e) => {
// Handle the error
eprintln!("Error processing crate: {:?}", e);
std::process::exit(1)
}
Expand All @@ -251,7 +246,7 @@ fn create_rocrate_with_context(context_type: ContextType) -> RoCrate {

// Get the primary context (probably ro-crate spec)
println!("Please primary context (default to ro-crate spac (1)):");
io::stdout().flush().unwrap(); // Make sure 'Enter key' is printed before input
io::stdout().flush().unwrap();
let mut answer = String::new();
io::stdin().read_line(&mut answer).unwrap();

Expand Down Expand Up @@ -302,12 +297,12 @@ fn loop_input(mut key_value_pairs: HashMap<String, String>) -> HashMap<String, S
}

print!("Enter key: ");
io::stdout().flush().unwrap(); // Make sure 'Enter key' is printed before input
io::stdout().flush().unwrap();
let mut key = String::new();
io::stdin().read_line(&mut key).unwrap();

print!("Enter value: ");
io::stdout().flush().unwrap(); // Make sure 'Enter value' is printed before input
io::stdout().flush().unwrap();
let mut value = String::new();
io::stdin().read_line(&mut value).unwrap();

Expand Down Expand Up @@ -486,7 +481,7 @@ fn prompt_for_types() -> u8 {
println!("6 - A boolean (true/false)");

let mut answer = String::new();
io::stdout().flush().unwrap(); // Ensure prompt is displayed before input
io::stdout().flush().unwrap();
io::stdin().read_line(&mut answer).unwrap();

match answer.trim().parse::<u8>() {
Expand Down Expand Up @@ -522,7 +517,7 @@ fn get_field_values_with_count<T: Serialize>(
fn collect_field_values_recursive(
json: &JsonValue,
field_name: &str,
collected_values: &mut HashMap<(String, String), isize>, // (id, value) -> count
collected_values: &mut HashMap<(String, String), isize>,
) {
match json {
JsonValue::Object(obj) => {
Expand All @@ -536,7 +531,7 @@ fn collect_field_values_recursive(
for (key, value) in obj {
if key == field_name {
let value_str = value.to_string();
let key = (current_id.clone(), value_str); // Store as tuple (id, type)
let key = (current_id.clone(), value_str);
*collected_values.entry(key).or_insert(0) += 1;
}
// Continue recursive search within the object
Expand Down
19 changes: 12 additions & 7 deletions examples/create_and_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
"id": "./",
"type": "Dataset",
"datePublished": "2017",
"name": "Example",
"description": "Example Ro-Crate for using python library",
"license": {"id": "https://creativecommons.org/licenses/by-nc-sa/3.0/au/"},
"author": {"id": "#johndoe"},
"author": {"id": "#JohnDoe"},
}
# Data entity
data = {"id": "output/data_file.txt", "type": "Dataset", "name": "Data file name"}
Expand All @@ -52,24 +54,26 @@


# Now that a new crate is written, we can open it again!
crate = read("ro-crate-metadata.json", 1)
crate = read("ro-crate-metadata.json", 0)
# print(f"Example of crate created and read in \n {crate}")

# Update the data entity and make modification
data_target = crate.get_entity("output/data_file.txt")
# print(data_target)
data_target["description"] = "A text file dataset containing information"

print(f"This is the loaded and modified data_file entity \n {data_target}")
# print(f"This is the loaded and modified data_file entity \n {data_target}")

crate.update_data(data_target)

print(f"This is now the updated, in memory, crate: \n {crate}")
# print(f"This is now the updated, in memory, crate: \n {crate}")

# Update the contextual entity and make modification
contextual_target = crate.get_entity("#JohnDoe")
contextual_target.update({"id": "#JaneDoe"})

crate.update_contextual(contextual_target)
print(f"Example of a modified entity id that will save as a new entity: \n {crate}")
# print(f"Example of a modified entity id that will save as a new entity: \n {crate}")

# To delete a key:value
data_target.pop("description")
Expand All @@ -80,12 +84,13 @@

# To delete an entity - this immediately updates the crate object
crate.delete_entity("#JaneDoe", True)
crate.replace_id("#JohnDoe", "#JaneDoe")

# print(crate)
crate.write()

# Final example of modified crate
crate = read("ro-crate-metadata.json", 1)
print(crate)

# Zip the crate to get all data
zip("ro-crate-metadata.json", 1)
# zip("ro-crate-metadata.json", True, 2)
Loading

0 comments on commit e499672

Please sign in to comment.