Python API: Versioning Data

This is the tested source code for the snippets used in Tracking Versions. The config file we’re using in this example can be downloaded here.

SetUp

>>> import datafs
>>> from fs.tempfs import TempFS
>>> import os

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

>>> with open('sample_archive.txt', 'w+', ) as f:
...     f.write(u'this is a sample archive')
...
>>> sample_archive = api.create(
...     'sample_archive',
...     metadata = {'description': 'metadata description'})
...
>>> sample_archive.update('sample_archive.txt', prerelease='alpha')

cleanup:

>>> os.remove('sample_archive.txt')

Example 2

>>> sample_archive.get_versions()
[BumpableVersion ('0.0.1a1')]

Example 3

>>> with open('sample_archive.txt', 'w+', ) as f:
...     f.write(u'Sample archive with more text so we can bumpversion')
...
>>>
>>> sample_archive.update('sample_archive.txt', bumpversion='minor')
>>> sample_archive.get_versions()
[BumpableVersion ('0.0.1a1'), BumpableVersion ('0.1')]

cleanup:

>>> os.remove('sample_archive.txt')

Example 4

>>> sample_archive.get_latest_version()
BumpableVersion ('0.1')

Example 5

>>> sample_archive.get_latest_hash()
u'510d0e2eadd19514788e8fdf91e3bd5c'

Example 6

>>> sample_archive1 = api.get_archive(
...     'sample_archive',
...     default_version='0.0.1a1')
...
>>> with sample_archive1.open('r') as f:
...    print(f.read())
...
this is a sample archive

Teardown

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