Python API: Creating Archives¶
This is the tested source code for the snippets used in
Creating Data Archives. The config file we’re using in this example
can be downloaded
here
.
Setup¶
>>> import datafs
>>> from fs.tempfs import TempFS
We test with the following setup:
>>> api = datafs.get_api(
... config_file='examples/snippets/resources/datafs_mongo.yml')
...
This assumes that you have a config file at the above location. The config file
we’re using in this example can be downloaded
here
.
clean up any previous test failures
>>> try:
... api.delete_archive('my_archive_name')
... except (KeyError, OSError):
... pass
...
>>> try:
... api.manager.delete_table('DataFiles')
... except KeyError:
... pass
...
Add a fresh manager table:
>>> api.manager.create_archive_table('DataFiles')
Example 1¶
Displayed example 1 code:
>>> archive = api.create('my_archive_name')
Example 1 cleanup:
>>> api.delete_archive('my_archive_name')
Example 2¶
Example 2 setup
>>> api.attach_authority('my_authority', TempFS())
Displayed example 2 code
>>> archive = api.create('my_archive_name')
Traceback (most recent call last):
...
ValueError: Authority ambiguous. Set authority or DefaultAuthorityName.
Example 3¶
>>> archive = api.create(
... 'my_archive_name',
... authority_name='my_authority')
...
Example 3 cleanup:
>>> try:
... api.delete_archive('my_archive_name')
... except KeyError:
... pass
...
Example 4¶
>>> api.DefaultAuthorityName = 'my_authority'
>>> archive = api.create('my_archive_name')
Example 4 cleanup:
>>> try:
... api.delete_archive('my_archive_name')
... except KeyError:
... pass
...
Example 5¶
>>> archive = api.create(
... 'my_archive_name',
... metadata={
... 'description': 'my test archive',
... 'source': 'Burke et al (2015)',
... 'doi': '10.1038/nature15725'})
...
Example 5 cleanup:
>>> try:
... api.delete_archive('my_archive_name')
... except KeyError:
... pass
...
Example 6¶
Example 6 setup:
>>> api.manager.set_required_archive_metadata(
... {'description': 'Enter a description'})
...
Displayed example:
>>> archive = api.create(
... 'my_archive_name',
... metadata = {
... 'source': 'Burke et al (2015)',
... 'doi': '10.1038/nature15725'})
...
Traceback (most recent call last):
...
AssertionError: Required value "description" not found. Use helper=True or
the --helper flag for assistance.
Example 6 cleanup:
>>> try:
... api.delete_archive('my_archive_name')
... except KeyError:
... pass
...
Example 7¶
>>> archive = api.create(
... 'my_archive_name',
... metadata={
... 'source': 'Burke et al (2015)',
... 'doi': '10.1038/nature15725'},
... helper=True)
...
Enter a description:
Teardown¶
>>> try:
... api.delete_archive('my_archive_name')
... except KeyError:
... pass
...
>>> api.manager.delete_table('DataFiles')