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.3.0.tar.gz (11.2 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.3.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flatfs-0.3.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.17.0-1013-aws

File hashes

Hashes for flatfs-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5c11b90ed3d23d47443a3f1ccf530634bb021b6a010215915b2ffd737d8d1b39
MD5 dc85022db77056e7c5f6efdec6409b47
BLAKE2b-256 0cca358fef3cc0c978cb080d61ec9e1cfff2dd2e346c439014bb4f5f8dd7a5e4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flatfs-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.12 Linux/6.17.0-1013-aws

File hashes

Hashes for flatfs-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15d564e6d0da78626d38ec5649095826051be71866c1c965e8c39d94d0d76f67
MD5 d1c7b875680cba85033eae158991e00f
BLAKE2b-256 48fb380baa3e7c557f979acd0734db30b8f5c4760c86cd9e5fbab249eebaf34d

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