aiofiledol

aiofile (async filesys operations) with a simple (dict-like or list-like) interface

Classes

AioFileBytesPersister(*args[, delete_func])

Async file persister with configurable deletion.

AioFileBytesReader(rootdir[, subpath, ...])

AioFileStringPersister(*args[, delete_func])

AioFileStringReader(rootdir[, subpath, ...])

RelPathAioFileBytesReader(rootdir[, ...])

RelPathFileStringReader(rootdir[, subpath, ...])

class aiofiledol.AioFileBytesPersister(*args, delete_func=None, **kwargs)[source]

Bases: LocalFileDeleteMixin, AioFileBytesReader, KvPersister

Async file persister with configurable deletion.

Examples

>>> from dol.filesys import mk_tmp_dol_dir
>>> rootdir = mk_tmp_dol_dir('aiofiledol_test')
>>> # Default: safe trash with warning on fallback
>>> store = AioFileBytesPersister(rootdir)
>>> # Permanent deletion without warnings
>>> from dol.trash import permanent_delete
>>> store = AioFileBytesPersister(rootdir, delete_func=permanent_delete)
asetitem(k, v)[source]

Write bytes v to the file at key k (async).

>>> import asyncio, os
>>> from dol.filesys import mk_tmp_dol_dir
>>> rootdir = mk_tmp_dol_dir('aiofiledol_test')
>>> rpath = lambda *p: os.path.join(rootdir, *p)
>>> s = AioFileBytesPersister(rootdir)
>>> k = rpath('foo')
>>> if k in s:
...     del s[k]  # delete key if present
>>> n = len(s)  # number of items in store
>>> asyncio.run(s.asetitem(k, b'bar'))
>>> len(s) == n + 1  # there's one more item in store
True
>>> k in s
True
>>> asyncio.run(s.aget(k))  # read it back (async reader; __getitem__ is disabled)
b'bar'
class aiofiledol.AioFileBytesReader(rootdir, subpath='', pattern_for_field=None, max_levels=None, *, include_hidden=False, assert_rootdir_existence=False)[source]

Bases: FileCollection, KvReader

async aget(k)[source]

Get the bytes contents of the file k.

Async examples are driven with asyncio.run so they run under a plain --doctest-modules collection (top-level await is a syntax error in doctests).

>>> import asyncio, os
>>> from dol.filesys import mk_tmp_dol_dir
>>> rootdir = mk_tmp_dol_dir('aiofiledol_test')
>>> filepath = os.path.join(rootdir, 'greeting')
>>> with open(filepath, 'wb') as fp:
...     _ = fp.write(b'hello world')
>>> s = AioFileBytesReader(rootdir, max_levels=0)
>>> asyncio.run(s.aget(filepath))
b'hello world'
class aiofiledol.AioFileStringPersister(*args, delete_func=None, **kwargs)[source]

Bases: AioFileBytesPersister

class aiofiledol.AioFileStringReader(rootdir, subpath='', pattern_for_field=None, max_levels=None, *, include_hidden=False, assert_rootdir_existence=False)[source]

Bases: AioFileBytesReader

class aiofiledol.RelPathAioFileBytesReader(rootdir, subpath='', pattern_for_field=None, max_levels=None, *, include_hidden=False, assert_rootdir_existence=False)

Bases: PrefixRelativizationMixin, Store

is_valid_key(k, *args, __name='is_valid_key', **kwargs)

is_valid_key on the inner key – see mk_relative_path_store.

validate_key(k, *args, __name='validate_key', **kwargs)

validate_key on the inner key – see mk_relative_path_store.

class aiofiledol.RelPathFileStringReader(rootdir, subpath='', pattern_for_field=None, max_levels=None, *, include_hidden=False, assert_rootdir_existence=False)

Bases: PrefixRelativizationMixin, Store

is_valid_key(k, *args, __name='is_valid_key', **kwargs)

is_valid_key on the inner key – see mk_relative_path_store.

validate_key(k, *args, __name='validate_key', **kwargs)

validate_key on the inner key – see mk_relative_path_store.