Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the crate root directory without loading #197

Closed
multimeric opened this issue Sep 23, 2024 · 1 comment
Closed

Set the crate root directory without loading #197

multimeric opened this issue Sep 23, 2024 · 1 comment

Comments

@multimeric
Copy link

multimeric commented Sep 23, 2024

Let's say you have a directory with data files in it that you want to document, but it's not the current working directory. You also don't have an ro-crate-metadata.json yet.

/path/to/crate/
    some_file.txt

If you crate = ROCrate("/path/to/crate"), you will get ValueError: Not a valid RO-Crate: missing ro-crate-metadata.json.

If you use:

crate = ROCrate()
crate.add_file("some_file.txt")
crate.write("/path/to/crate")

You get FileNotFoundError: [Errno 2] No such file or directory: 'some_file.txt'

Even if you try to hack it:

crate = ROCrate()
crate.source = "/path/to/crate"
crate.add_file("some_file.txt")
crate.write("/path/to/crate")

This fails with FileNotFoundError: [Errno 2] No such file or directory: 'some_file.txt', because it uses self.source as the file path which isn't resolved relative to `crate.source.

I could write out the absolute path of each entity, but it's a bit verbose.

@simleo
Copy link
Collaborator

simleo commented Sep 25, 2024

Try this:

>>> from rocrate.rocrate import ROCrate
>>> crate = ROCrate("/path/to/crate", init=True)
>>> crate.source
'/path/to/crate'
>>> f = crate.get("some_file.txt")
>>> f["encodingFormat"] = "audio/mpeg"
>>> crate.write(crate.source)

ROCrate("/path/to/crate", init=True) means you don't want to read a crate at that path, but initialize one based on the directory tree rooted at that path (the tree is walked recursively, adding files as File and directories as Dataset)

@simleo simleo closed this as completed Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants