Skip to main content

fslib is a wrapper around Python’s os.* low-level functions, aiming to:

  • Provide more pythonic APIs for low-level primitives

  • Merge various physical locations into a simple pseudo-filesystem for the application

  • Handle UnionFS-like features, including transparent write overlay on top of read-only paths

fslib supports Python 3.3+; it requires only the standard Python library.

Download

PyPI: https://pypi.python.org/pypi/fslib/

$ pip install fslib

Source: https://github.com/rbarrois/fslib/

$ git clone git://github.com/rbarrois/fslib/
$ python setup.py install

Usage

Replacing import os

>>> import fslib
>>>
>>> fs = fslib.FileSystem(fslib.OSFS('/'))
>>> fs.dir_exists('/etc/')
True
>>> fs.listdir('/')
['/etc', '/dev', '/proc']

Forcing read-only

In order to ensure that user code doesn’t change any physical file, fslib provides a simple wrapper that prevents writing to the filesystem.

>>> import fslib
>>> import fslib.stacking
>>>
>>> fs = fslib.FileSystem(fslib.stacking.ReadOnlyFS(fslib.OSFS('/')))
>>> fs.read_one_line('/etc/hostname')
"ithor"
>>> fs.open('/tmp/x', 'w')
OSError: [Errno 30] Read-only file system: '/tmp/x'

UnionFS-like mounting

fslib provides a UnionFS-like wrapper as fslib.stacking.UnionFS. This backend will provide a merged view of several branch, redirecting all writes to the branch(es) declared writable=True.

>>> import fslib, fslib.builders, fslib.stacking
>>>
>>> mem_fs = fslib.builders.make_memory_fake()
>>> union_fs = fslib.stacking.UnionFS()
>>> union_fs.add_branch(mem_fs, ref='memory', rank=0, writable=True)
>>> union_fs.add_branch(fslib.stacking.ReadOnlyFS(fslib.OSFS('/')), ref='os', rank=1)
>>>
>>> fs = fslib.FileSystem(backend=union_fs)
>>> fs.writelines('/tmp/x', ['aa', 'bb'])
>>>
>>> open('/tmp/x', 'r')
IOError: [Errno 2] No such file or directory: '/tmp/x'
>>> fs.file_exists('/tmp/x')
True
>>> fs.readlines('/tmp/x')
['aa', 'bb']
>>> fs.access('/tmp/x', os.F_OK)
True

Unix-like filesystem tree

It is possible to “overlay” physical or virtual file systems to present a simple, unified structure to the program.

>>> import fslib, fslib.stacking
>>> mnt = fslib.MountFS()
>>> mnt.mount_fs('/', fslib.stacking.ReadOnlyFS(fslib.OSFS('/')))
>>> mnt.mount_fs('/home/xelnor/.myapp', fslib.stacking.MemoryFS())
>>> mnt.mount_fs('/home/xelnor/.myapp/cache', fslib.OSFS('/tmp/myapp/shared_cache'))

With this setup:

  • All reads/writes to /home/xelnor/.myapp/cache will actually occur within /tmp/myapp/shared_cache

  • All reads/writes within /home/xelnor/.myapp (except for /cache) will occur in memory

  • No write will be permitted anywhere else.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fslib-0.3.4.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fslib-0.3.4-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file fslib-0.3.4.tar.gz.

File metadata

  • Download URL: fslib-0.3.4.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for fslib-0.3.4.tar.gz
Algorithm Hash digest
SHA256 d2f6949da0ab140c8a7836c2d6834fbe6ec87f91c2dc368171cc008733a18d22
MD5 55b68cca88b877b51590bb83539c2620
BLAKE2b-256 f665b34e6f1883de5fdccf1d97d2507d8c3d38e2947eff11c3797c96f9b43823

See more details on using hashes here.

File details

Details for the file fslib-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: fslib-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for fslib-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 224dcd976fee59088a61d0a92b6c2313a7fb4e7286794a0e16e90a6fd07e6982
MD5 e58f5aab65441fe58aa85032814c4ef5
BLAKE2b-256 042d58e3a8053969aeefc348b50d73cf46ca4cdf364976cd2bfd9fdac0cfea27

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.4 This release

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.1

1 file

0.2.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page