ResearchObject / ro-crate-py

Python library for RO-Crate
https://pypi.org/project/rocrate/
Apache License 2.0
46 stars 23 forks source link

Add ro-crate instantiation from a dictionary #183

Closed simleo closed 4 months ago

simleo commented 4 months ago

Fixes #178.

from rocrate.rocrate import ROCrate

metadata = {
    "@context": "https://w3id.org/ro/crate/1.1/context",
    "@graph": [
        {
            "@id": "ro-crate-metadata.json",
            "@type": "CreativeWork",
            "about": {"@id": "./"},
            "conformsTo": {"@id": "https://w3id.org/ro/crate/1.1"}
        },
        {
            "@id": "./",
            "@type": "Dataset",
            "creator": {"@id": "#josiah"},
            "hasPart": {"@id": "http://example.org/foo"}
        },
        {
            "@id": "http://example.org/foo",
            "@type": "File"
        },
        {
            "@id": "#josiah",
            "@type": "Person",
            'name': 'Josiah Carberry'
        },
    ]
}

crate = ROCrate(metadata)
crate.write("crate")

If the metadata points to nonexistent local files or directories, the behavior when writing the crate is as described in #136 (see also the unit test added in this PR).