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.2.0.tar.gz (9.3 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.2.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flatfs-0.2.0.tar.gz
  • Upload date:
  • Size: 9.3 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.2.0.tar.gz
Algorithm Hash digest
SHA256 24463d85869d9195cfae18275f9c2ffa157ead02bdced1bf05375d38a7d5d681
MD5 f41a76e416282948fa24de2ff9cf2524
BLAKE2b-256 04a61f10b07e457596092085c5441f7a274f482016f524656730209c4a6223e7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flatfs-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98d937da66c5c764c7f7f390a34c19508c509d5219f18185d1a1e6c1ebba5c4c
MD5 e9d97452b28655c201c28af4126680de
BLAKE2b-256 e372123ab985a6d075077386bd23b3b0a67e524c71bec215c2a905cc365d9ab6

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