Skip to main content

Minimal filesystem abstraction with pluggable backends, treating paths as keys and file contents as values.

Project description

FlatFS

Minimal filesystem abstraction with pluggable backends, treating paths as keys and file contents as values.

About

FlatFS provides minimalistic protocol-based API for accessing files. The API allows you to:

  • create files using generator of binary chunks as source of data
  • read files in chunks
  • checking if file exists
  • removing file from the filesystem
  • scanning filesystem to retrieve all the files that are available

The API from above is supplied with a rich set of helpers that can simplify common actions, like writing/reading whole files in text or binary mode, reading files line-by-line, reading exact amount of bytes etc.

FlatFS does not use directories; those are just used to organize paths (aka keys) and some implementations may use real directories, and the other may not.

The library is implemented in a way allowing it to be used with large files thanks to the generator-based core API.

Installation

The project can be installed directly from PyPI:

$ pip install flatfs

You can alternatively use any of the existing Python package management tools (e.g. Poetry or uv) to install the library directly to your project.

Examples

Here are some practical examples of how this library can be used:

  1. Access /tmp directory:

    import pathlib
    
    from flatfs.api import LocalFlatFs, FlatFsReaderWriter, write_text, read_text
    
    def make_temp_fs() -> FlatFsReaderWriter:
        return LocalFlatFs(pathlib.Path("/tmp"))
    
    temp_fs = make_temp_fs()
    write_text(temp_fs, "/foo/bar.txt", "content of file")  # Creates text file at /tmp/foo/bar.txt
    content = read_text(temp_fs, "/foo/bar.txt")  # Read the content back
    
  2. Create in-memory flat filesystem (this implementation can be used during testing):

    from flatfs.api import InMemoryFlatFs
    
    fs = InMemoryFlatFs()
    fs.write_chunks("foo.txt", [b"spam"])
    paths = list(fs.scan())  # Will return: ["/foo.txt"]
    
  3. Wrap flat filesystem created earlier with async interface:

    from flatfs.api import AsyncFlatFsAdapter, async_read_text
    
    async_temp_fs = AsyncFlatFsAdapter(temp_fs)
    content = await async_read_text(async_temp_fs, "foo/bar.txt")  # Paths are normalized and leading / is automatically added
    print(content)  # Would print: content of file
    

Rationale

During developing of various systems and tools that follow clean architecture and layered design (business logic, use case interactors, data ports and gateways etc.) I found myself writing similar tool as one of the port/gateway pair over and over again. This library was created to have such toolkit a bit more advanced, based on chunk generators and producers, backed up with comprehensive set of helpers, just to avoid writing it again for the (N+1)th time.

Author

Maciej Wiatrzyk maciej.wiatrzyk@gmail.com

License

This project is released under the terms of the MIT license.

See LICENSE.txt for more details.

Project details


Download files

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

Source Distribution

flatfs-0.0.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

flatfs-0.0.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file flatfs-0.0.0.tar.gz.

File metadata

  • Download URL: flatfs-0.0.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.4 Linux/6.1.0-44-amd64

File hashes

Hashes for flatfs-0.0.0.tar.gz
Algorithm Hash digest
SHA256 d84561b64ca70d68bbdba4ec27d7bbf2daa16be595f8692652f82b92d78ab92c
MD5 fcf6d5d927668aa902d98238fcb3de0c
BLAKE2b-256 e3198bf0e0d1944ed012a3f0b787968d72167a66e629e53668478f1bc7bc4966

See more details on using hashes here.

File details

Details for the file flatfs-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: flatfs-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.4 Linux/6.1.0-44-amd64

File hashes

Hashes for flatfs-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 259cea2bbe63ca8365bfda8552201988afc357884899ccccdd40ecd039021d09
MD5 c37335b68b2a0434c8605c1d2a166eb0
BLAKE2b-256 d369d0aad30615093b9a4567c1cb7c8ce2e90942210b0e791f24202dcb914235

See more details on using hashes here.

Supported by

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