Filesystem-based key-value stores

A straightforward implementation is a filesystem-based implementation found in simplekv.fs.FilesystemStore class, as well as a slightly altered version suitable for web applications, simplekv.fs.WebFilesystemStore.

class simplekv.fs.FilesystemStore(root, perm=None, **kwargs)

Store data in files on the filesystem.

The FilesystemStore stores every value as its own file on the filesystem, all under a common directory.

Any call to url_for() will result in a file://-URL pointing towards the internal storage to be generated.

iter_keys(prefix=u'')

Return an Iterator over all keys currently in the store, in any order. If prefix is not the empty string, iterates only over all keys starting with prefix.

Raises:exceptions.IOError – If there was an error accessing the store.
iter_prefixes(delimiter, prefix=u'')

Returns an Iterator over all prefixes currently in the store, in any order. The prefixes are listed up to the given delimiter.

If the prefix contains the delimiter, the first delimiter after the prefix is used as a cut-off point.

The uniqueness of the prefixes is ensured.

The default uses an naive key iteration. Some backends may implement more efficient variants.

Raises:exceptions.IOError – If there was an error accessing the store.
keys(prefix=u'')

Return a list of keys currently in store, in any order If prefix is not the empty string, returns only all keys starting with prefix.

Raises:exceptions.IOError – If there was an error accessing the store.
class simplekv.fs.WebFilesystemStore(root, url_prefix, **kwargs)

FilesystemStore that supports generating URLs suitable for web applications. Most common use is to make the root directory of the filesystem store available through a webserver. Example:

>>> from simplekv.fs import WebFilesystemStore
>>> webserver_url_prefix = 'https://some.domain.invalid/files/'
>>> webserver_root = '/var/www/some.domain.invalid/www-data/files/'
>>> store = WebFilesystemStore(webserver_root, webserver_url_prefix)
>>> print(store.url_for(u'some_key'))
https://some.domain.invalid/files/some_key

Note that the prefix is simply prepended to the relative URL for the key. It therefore, in most cases, must include a trailing slash.

url_prefix may also be a callable, in which case it gets called with the filestore and key as an argument and should return an url_prefix.

>>> from simplekv.fs import WebFilesystemStore
>>> webserver_url_prefix = 'https://some.domain.invalid/files/'
>>> webserver_root = '/var/www/some.domain.invalid/www-data/files/'
>>> prefix_func = lambda store, key: webserver_url_prefix
>>> store = WebFilesystemStore(webserver_root, prefix_func)
>>> print(store.url_for(u'some_key'))
https://some.domain.invalid/files/some_key