Filesystem-based key-value stores

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

class minimalkv.fs.FilesystemStore(root: str, perm: int | None = None)

Store data in files on the filesystem under a common directory.

When files are created, they will receive permissions depending on the current umask if perm is None. Otherwise, permissions are set explicitly.

Note that when using put_file() with a filename, an attempt to move the file will be made. Permissions and ownership of the file will be preserved that way. If perm is set, permissions will be changed.

The method url_for() can be used to get a file://-URL pointing to the internal storage.

Parameters

rootstr

The base directory for the store.

permint or None, optional, default = None

The permissions for files in the filesystem store.

iter_keys(prefix: str = '') Iterator[str]

Iterate over all keys in the store starting with prefix.

Parameters

prefixstr, optional, default = ‘’

Only iterate over keys starting with prefix. Iterate over all keys if empty.

iter_prefixes(delimiter: str, prefix: str = '') Iterator[str]

Iterate over unique prefixes in the store up to delimiter, starting with prefix.

If prefix contains delimiter, return the prefix up to the first occurence of delimiter after the prefix.

Parameters

delimiterstr, optional, default = ‘’

Delimiter up to which to iterate over prefixes.

prefixstr, optional, default = ‘’

Only iterate over prefixes starting with prefix.

keys(prefix: str = '') List[str]

List all keys in the store starting with prefix.

Parameters

prefixstr, optional, default = ‘’

Only list keys starting with prefix. List all keys if empty.

class minimalkv.fs.WebFilesystemStore(root, url_prefix: Callable[[Any, str], str] | str, **kwargs)

FilesystemStore supporting generating URLS for web applications.

The most common use is to make the root directory of the filesystem store available through a webserver.

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

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.

Parameters

rootstr

The base directory for the store.

permint or None, optional, default = None

The permissions for files in the filesystem store.

url_prefixCallable or str

Prefix that will get prepended to every url.

Example

>>> from minimalkv.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
>>> from minimalkv.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