Python API: Managing Metadata

This is the tested source code for the snippets used in Managing Metadata. 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('sample_archive')
... 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

>>> sample_archive = api.create(
...    'sample_archive',
...     metadata = {
...         'oneline_description': 'tas by admin region',
...         'source': 'NASA BCSD',
...         'notes': 'important note'})
...

Example 2

Displayed example 2 code

>>> for archive_name in api.filter():
...     print(archive_name)
...
sample_archive

Example 3

Displayed example 3 code

>>> sample_archive.get_metadata() 
{u'notes': u'important note', u'oneline_description': u'tas by admin region
', u'source': u'NASA BCSD'}

The last line of this test cannot be tested directly (exact dictionary formatting is unstable), so is tested in a second block:

>>> sample_archive.get_metadata() == {
...     u'notes': u'important note',
...     u'oneline_description': u'tas by admin region',
...     u'source': u'NASA BCSD'}
...
True

Example 4

Displayed example 4 code

>>> sample_archive.update_metadata(dict(
...     source='NOAAs better temp data',
...     related_links='http://wwww.noaa.gov'))
...

Example 5

Displayed example 5 code

>>> sample_archive.get_metadata() 
{u'notes': u'important note', u'oneline_description': u'tas by admin region
', u'source': u'NOAAs better temp data', u'related_links': u'http://wwww.no
aa.gov'}

The last line of this test cannot be tested directly (exact dictionary formatting is unstable), so is tested in a second block:

>>> sample_archive.get_metadata() == {
...     u'notes': u'important note',
...     u'oneline_description': u'tas by admin region',
...     u'source': u'NOAAs better temp data',
...     u'related_links': u'http://wwww.noaa.gov'}
...
True

Example 6

Displayed example 6 code

>>> sample_archive.update_metadata(dict(related_links=None))
>>>
>>> sample_archive.get_metadata() 
{u'notes': u'important note', u'oneline_description': u'tas by admin region
', u'source': u'NOAAs better temp data'}

The last line of this test cannot be tested directly (exact dictionary formatting is unstable), so is tested in a second block:

>>> sample_archive.get_metadata() == {
...     u'notes': u'important note',
...     u'oneline_description': u'tas by admin region',
...     u'source': u'NOAAs better temp data'}
...
True

Teardown

>>> try:
...     api.delete_archive('sample_archive')
... except KeyError:
...     pass
...

>>> api.manager.delete_table('DataFiles')