Skip to main content

MX8 File system

This library provides environment agnostic file system access across local and AWS, including:

  • File / IO
  • List / Glob
  • Locking
  • Caching
  • Comparing Dictionaries

2.0.0 breaking change

FileLock now uses one {file}.lock claim record instead of {file}.{timestamp}.{random}.lock candidate files. S3 conditional writes and deletes, and a local OS-backed guard, prevent a delayed contender from acquiring a lock that another client already holds. Local locking also retains a {file}.lock.guard file for synchronization; keep that file in place while clients are running.

filelock is pinned to 3.20.3 to preserve the Unix guard inode across operations. Do not override this dependency: mixing guard lifecycles can allow overlapping locks even when every client runs mx8fs 2.x.

1.x and 2.x clients must not acquire locks for the same resource concurrently. The new protocol does not inspect legacy candidate files, so clients using different protocols can enter the same critical section.

To upgrade, or to roll back across this boundary:

  1. Stop new lock acquisitions and let existing critical sections finish, or stop the processes holding those locks. Waiting for maximum_age alone does not stop a process that is still running.
  2. Upgrade every client that can lock the same resource before restarting them. Include API processes, background workers, Lambda variants and running container tasks, even when they come from the same repository. For a rollback, return all of those clients to the previous protocol before restarting them.
  3. Resume lock acquisitions after all participants use the same protocol. Orphaned legacy candidate files may be removed once their old clients have stopped; 2.x ignores them.

The constructor, context-manager API and defaults are unchanged. maximum_age remains the checker's policy for the age of existing locks; the creator does not set an expiry deadline for other clients.

API Reference

Below are the functions and classes exported by the library (mx8fs.__all__). Paths can be local (e.g. /tmp/file.txt) or S3 (s3://bucket/key). HTTPS reads are supported in specific helpers.

File I/O and Paths

  • read_file(path: str) -> str: Read text from S3, HTTPS, or local using UTF‑8.

    • S3: GetObject and decode body.
    • HTTPS: simple GET; raises FileNotFoundError for network or 404.
    • Local: standard open/read.

    Example:

    from mx8fs import read_file
    
    text = read_file('/tmp/example.txt')
    s3_text = read_file('s3://my-bucket/path/data.txt')
    https_text = read_file('https://example.com/info.txt')
    
  • read_file_with_version(path: str, *, version_id: str | None = None) -> tuple[str, str]: Read text and a concurrency version, optionally from a historical version.

    • S3: returns (content, etag) (quotes stripped).
    • Local: returns (content, mtime-as-string).

    Example:

    from mx8fs import read_file_with_version
    
    content, version = read_file_with_version('s3://my-bucket/app/config.json')
    
  • write_file(path: str, data: str) -> str: Write UTF‑8 text to S3 or local and return its resulting version. Creates parent directories for local writes.

    Example:

    from mx8fs import write_file
    
    write_file('/tmp/output.txt', 'hello world')
    write_file('s3://my-bucket/logs/run.txt', 'done')
    
  • update_file_if_version_matches(path: str, data: str, version: str) -> str: Version-aware write that returns the resulting version.

    • S3: uses IfMatch=<etag>. Raises VersionMismatchError on mismatch, FileNotFoundError if key missing.
    • Local: compares the nanosecond mtime and atomically replaces the file without a lock. Portable filesystems do not provide an atomic compare-and-replace primitive, so another process can still write between the comparison and replacement.

    Example (optimistic concurrency):

    from mx8fs import read_file_with_version, update_file_if_version_matches, VersionMismatchError
    
    path = 's3://my-bucket/app/config.json'
    data, ver = read_file_with_version(path)
    new_data = data + "\n" + "# updated"
    try:
        update_file_if_version_matches(path, new_data, ver)
    except VersionMismatchError:
        # somebody else updated it — reload & retry as needed
        pass
    

File history

S3 history follows the bucket's native versioning configuration. Local history is disabled by default and can be enabled process-wide:

MX8FS_LOCAL_VERSIONING=true

Enabled local history is stored in adjacent .mx8fs-versions sidecars. These directories are excluded from normal file and folder listings. Mutations made through mx8fs capture an existing file before changing it and append the resulting live version or delete marker afterward.

  • list_file_versions(path: str) -> list[FileVersionMetadata]: Return newest-first live versions and delete markers.
  • read_file_version(path: str, version: FileVersionMetadata) -> str: Read a previously listed version without listing its metadata again.
  • read_file(path: str, *, version_id: str | None = None) -> str: Read the current file or an explicit historical version. Reading a delete marker raises FileVersionDeletedError.
  • restore_file_version(path: str, version_id: str) -> FileVersionMetadata: Restore historical content as a new current version without rewriting history.
  • undelete_file(path: str) -> FileVersionMetadata: Restore the newest live version when the current state is a delete marker.
  • list_files(..., include_deleted=True) and list_files_with_metadata(..., include_deleted=True): Include names whose latest state is deleted. Deleted metadata has is_deleted=True, and latest_readable_version contains the newest live FileVersionMetadata found by the same bulk history scan. Pass it directly to read_file_version(); it is None when the key has no readable version.

Local history requests raise VersioningNotEnabledError while the environment switch is off. HTTPS paths raise VersioningNotSupportedError. Local changes made outside mx8fs are not recorded retroactively, although their current state is captured before the next mx8fs mutation.

  • file_exists(path: str) -> bool: True if file exists (HEAD on S3; os.path.exists locally).

    Example:

    from mx8fs import file_exists
    if file_exists('s3://my-bucket/data.csv'):
        ...
    
  • delete_file(path: str) -> None: Delete from S3 or local. Local deletion ignores missing files.

    Example:

    from mx8fs import delete_file
    delete_file('/tmp/temp.txt')
    delete_file('s3://my-bucket/tmp/stale.json')
    
  • delete_files(paths: list[str], max_workers: int = 500) -> None: Batch delete mixed S3/local paths.

    • S3: groups by bucket; uses DeleteObjects in chunks of 1000.
    • Local: deletes concurrently with a thread pool.

    Example:

    from mx8fs import delete_files
    delete_files([
        '/tmp/a.txt',
        's3://my-bucket/path/old.log',
    ])
    
  • copy_file(src: str, dst: str, chunk_size: int = 131072) -> None: Copy between S3, local storage, and HTTPS sources with bounded memory. S3→S3 uses CopyObject; other S3 destinations use sequential multipart uploads, and local destinations are replaced atomically after a successful transfer.

    Example:

    from mx8fs import copy_file
    copy_file('/tmp/input.bin', '/tmp/output.bin')
    copy_file('s3://my-bucket/a.txt', 's3://my-bucket/archive/a.txt')
    
  • move_file(src: str, dst: str) -> None: Copy then delete source.

    Example:

    from mx8fs import move_file
    move_file('s3://my-bucket/new.txt', 's3://my-bucket/archive/new.txt')
    
  • get_files(root_path: str, prefix: str = "", cutoff_date: datetime | None = None, cutoff_earlier: bool = True) -> list[str]: List files under a root.

    • S3: returns keys relative to the root prefix; optional prefix filter and cutoff_date (defaults to older than) on LastModified.
    • Local: returns relative file paths under the directory; supports the same filters (mtime in UTC).

    Example:

    from datetime import datetime, timezone
    from mx8fs import get_files
    
    files = get_files('/data/reports/', prefix='2025-')
    old = get_files('s3://my-bucket/reports/', cutoff_date=datetime.now(timezone.utc))
    
  • list_files(root_path: str, file_type: str, prefix: str = "") -> list[str]: List files with a given extension (without dot).

    • Returns basenames without extension, optionally filtered by prefix.

    Example:

    from mx8fs import list_files
    names = list_files('/data', 'json', prefix='user_')  # ['user_1', 'user_2']
    
  • list_files_with_metadata(root_path: str, file_type: str, prefix: str = "") -> list[FileMetadata]: List files with portable metadata.

    • Each result contains the extension-free name, UTC last_modified, size_bytes, and an opaque version token.
    • S3 metadata comes from the object listing without additional HeadObject requests; local metadata comes from stat.

    Example:

    from datetime import UTC, datetime
    from mx8fs import list_files_with_metadata
    
    cutoff = datetime.now(UTC)
    files = list_files_with_metadata('/data', 'json')
    marked = [(file.name, file.last_modified > cutoff) for file in files]
    
  • get_folders(root_path: str, prefix: str = "") -> list[str]: List immediate subfolders (non‑recursive).

    • S3: uses Delimiter='/' and returns top‑level folder names under the prefix.
    • Local: returns directory names directly under root_path.

    Example:

    from mx8fs import get_folders
    top = get_folders('s3://my-bucket/data/')  # e.g. ['a', 'b']
    
  • most_recent_timestamp(root_path: str, file_type: str) -> float: Latest modification time for files of a type.

    • S3: scans with a paginator and returns the newest LastModified as epoch seconds.
    • Local: max of os.path.getmtime for *.ext, or 0 when none.

    Example:

    from mx8fs import most_recent_timestamp
    ts = most_recent_timestamp('/data', 'csv')
    
  • get_public_url(path: str, expires_in: int = 3600, method: str = "get_object") -> str: For S3, returns a presigned URL for GET/PUT; for local, returns the input path.

    Example:

    from mx8fs import get_public_url
    get_url = get_public_url('s3://my-bucket/file.txt')
    put_url = get_public_url('s3://my-bucket/file.txt', method='put_object')
    
  • purge_folder(root_path: str, dry_run: bool = True, max_workers: int = 500, cutoff_date: datetime | None = None) -> list[str]: List and optionally delete all files under a folder/prefix.

    • Returns the full paths that were (or would be) deleted; respects cutoff_date when provided.

    Example:

    from mx8fs import purge_folder
    paths = purge_folder('s3://my-bucket/tmp/', dry_run=True)
    deleted = purge_folder('/tmp/workdir', dry_run=False)
    

Stream Handlers

  • class BinaryFileHandler(path: str, mode: str = "rb", content_type: str | None = None): Context manager for binary reads/writes.

    • S3: rb downloads to an in‑memory buffer; wb uploads on exit (uses content_type if provided).
    • HTTPS: supports rb read‑only; writing or text modes raise NotImplementedError.
    • Local: proxies to built‑in file object; ensures directories exist for writes.
    • Usage:
    from mx8fs import BinaryFileHandler
    with BinaryFileHandler('s3://bucket/key.bin', 'wb', content_type='application/octet-stream') as f:
        f.write(b'data')
    
  • GzipFileHandler(path: str, mode: str = "rb", encoding: str | None = None): Context manager for gzip‑compressed files.

    • Modes: rb, wb, rt, wt (text modes require encoding). Wraps BinaryFileHandler under the hood.

    Example:

    from mx8fs import GzipFileHandler
    # write text
    with GzipFileHandler('/tmp/data.gz', 'wt', encoding='utf-8') as f:
        f.write('hello')
    # read binary
    with GzipFileHandler('/tmp/data.gz', 'rb') as f:
        blob = f.read()
    

Both handlers accept keyword-only versioned=False, version_id=None and expected_version=None. Use versioned=True to track the current read or successful write in handler.version. Historical version_id reads and conditional expected_version writes enable version tracking automatically. version_id also accepts a listed FileVersionMetadata object to reuse its metadata without another lookup. The revision is the S3 ETag or local nanosecond mtime; HTTPS current reads have no revision.

handler = GzipFileHandler(path, "rt", encoding="utf-8", versioned=True)
with handler as source:
    text = source.read()

writer = GzipFileHandler(path, "wt", encoding="utf-8", expected_version=handler.version)
with writer as destination:
    destination.write(text)
new_version = writer.version

Versioned writes buffer until successful context exit. Gzip finishes before the underlying binary handler publishes, so a body or compression-finalization failure leaves the destination untouched. Local versioned writes preserve local history and publish with atomic replacement. Conditional writes use the existing mtime checks locally and S3 IfMatch; local compare-and-replace retains the concurrency limitation described above. Versioned S3 writes use PutObject and its single-request size limit; default stream writes retain managed uploads. GzipFileHandler also accepts compression_level=9 (0–9), preserving its existing default.

JsonFileHandler(path, mode="r", *, compression=None, compression_level=1, version_id=None, expected_version=None, decoder=json.loads, encoder=json.dumps) wraps these handlers for JSON documents. It supports r and w, returns itself from the context, and exposes read(), write(value) and .version. Compression accepts None or "gzip"; the supplied path is used unchanged. JSON I/O always enables version tracking and successful-exit publication, including when compression is disabled. Historical version IDs and listed FileVersionMetadata objects work in the same way as the binary and gzip handlers.

from mx8fs import JsonFileHandler

reader = JsonFileHandler("/tmp/settings.json.gz", compression="gzip")
with reader as source:
    settings = source.read()

with JsonFileHandler(
    "/tmp/settings.json.gz", "w", compression="gzip", expected_version=reader.version
) as destination:
    destination.write(settings)
new_version = destination.version

The decoder receives UTF-8 JSON text; the encoder returns JSON text. Supplying model conversion functions preserves custom validation and serialization without adding a Pydantic dependency to the handler. JsonFileStorage supplies its existing model hooks through FileHandler, which owns logical filename selection, fallback, history and version routing. The storage classes do not implement those file policies.

TextFileHandler(path, mode="r", *, encoding="utf-8", newline="auto", compression=None, compression_level=1, version_id=None, expected_version=None) owns text conversion. Its read(size=-1) and write(text) methods use TextIOWrapper for encoding and newline handling. It returns itself from its context and exposes .version. Text is flushed before gzip finalization and binary publication. JsonFileHandler delegates text I/O to this handler, preserving JSON text without newline translation.

The default newline="auto" preserves the existing text-read behaviour: local reads normalize line endings to \n, while S3 and HTTPS preserve their original line endings. Explicit newline=None normalizes on every backend; newline="" preserves line endings on every backend. The text handler owns this policy, including reads through the legacy helpers.

Typed file operations

FileHandler(path, data_type, *, compression=None, compression_level=1, fallback_uncompressed=False, encoding="utf-8", newline="auto", decoder=None, encoder=None) is the complete logical-file API. Use bytes for binary data, str for text, and JSON types such as dict or list for JSON values. Pydantic model types use their JSON validation and serialization methods; custom codecs can override them without adding a Pydantic dependency to the handler.

The path is logical: FileHandler("/tmp/settings.json", dict, compression="gzip") writes /tmp/settings.json.gz. primary_path exposes that physical destination. Low-level binary, gzip, text and JSON stream handlers continue to use their supplied physical paths unchanged.

from mx8fs import FileHandler

file = FileHandler("/tmp/settings.json", data_type=dict, compression="gzip", fallback_uncompressed=True)
settings, revision = file.read_with_version()
file.write(settings, expected_version=revision)
metadata = file.metadata()
versions = file.history()
old_settings = file.read(version_id=versions[-1])

folder = FileHandler("/tmp", data_type=dict, compression="gzip", fallback_uncompressed=True)
keys = folder.list("json")
files = folder.list_with_metadata("json", include_deleted=True)
child = folder.joinpath("settings.json")

The handler provides read(version_id=None), read_with_version(version_id=None), write(value, expected_version=None), exists(), delete(), current_version(), metadata(include_deleted=False), history(), restore_version(version_id), undelete() and get_lock(). Version IDs are keyword-only on reads and accept historical ID strings or listed FileVersionMetadata. write() returns the published revision; .version records the revision of the last opened or published content. HTTPS supports current reads and existence checks, without concurrency versions or portable metadata.

Directory listings accept prefix="" and return extension-free logical keys. The prefix is passed to the backend to limit S3 listing work. Single-file metadata uses the logical filename, including its data extension. Gzip/plain listings and history are merged by the handler, with opaque source-aware tokens. Fallback happens only while opening an absent file or historical version, before JSON or model decoding. Normal gzip writes publish before retiring plain current files; conditional updates retain the representation that was read.

open_read() yields a FileReader containing a deferred read() and the revision of those exact bytes. Indexed storage uses it to retain the source revision even when model validation fails. File metadata, version parsing and backend operations remain inside the handler; storage owns keys and models, while indexed storage owns SQL projections and reconciliation.

Locking

  • class Waiter(wait_period: float, time_out_seconds: float): Helper for periodic waits with a timeout.

    • Methods: start_timeout(), check_timeout(), wait(), timed_out(); usable as a context manager.

    Example:

    from mx8fs import Waiter
    with Waiter(0.1, 5) as w:
        while not some_condition():
            w.check_timeout()
    
  • class FileLock(file: str, wait_period: float = 0.1, time_out_seconds: int = 840, maximum_age: int = 900): Cross‑process/client lock via lock files.

    • Uses one {file}.lock claim record. S3 acquisition, expired-record takeover and release use conditional object operations. Local state changes use an OS-backed guard and atomic file replacement; the local {file}.lock.guard file is retained for synchronization.
    • wait_period controls polling after contention. Each contender respects records younger than its own maximum_age; expiry does not stop a previous holder from executing, but that holder cannot release its successor's lock.
    • Follow the 2.0.0 migration instructions before upgrading or rolling back clients that lock the same resource.
    • Usage:
    from mx8fs import FileLock, write_file
    with FileLock('s3://bucket/data.txt'):
        write_file('s3://bucket/data.txt', 'content')
    

Caching

  • get_cache_filename(path: str, name: str, extension: str, expiration_seconds: int = 0, **kwargs) -> str: Build a deterministic, optionally time‑scoped cache filename based on args/kwargs.

    Example:

    from mx8fs import get_cache_filename
    fname = get_cache_filename('/tmp/cache', 'fetch_users', 'txt', expiration_seconds=3600, region='us', extra_args=(1,2))
    
  • cache_to_disk(path: str, expiration_seconds: int = 0, log_group: str = "", ignore_kwargs: list[str] | None = None): Decorator for caching text results.

    • Stores .txt payloads using read_file/write_file; logs cache hits when log_group is set.

    Example:

    from mx8fs import cache_to_disk
    
    @cache_to_disk('/tmp/mycache', expiration_seconds=600)
    def compute(user_id: str) -> str:
        return f"hello {user_id}"
    
    print(compute('42'))
    
  • cache_to_disk_binary(path: str, expiration_seconds: int = 0, log_group: str = "", ignore_kwargs: list[str] | None = None): Decorator for caching arbitrary Python objects via pickle.

    • Stores .pickle payloads using BinaryFileHandler.

    Example:

    from mx8fs import cache_to_disk_binary
    
    @cache_to_disk_binary('/tmp/bin_cache')
    def heavy() -> dict:
        return {'a': 1}
    data = heavy()
    

JSON Storage

Install JSON storage dependencies with pip install "mx8fs[json-storage]".

  • class JsonFileStorage(base_path: str, randomizer: Callable | None = None): Base class for simple JSON model storage.

    • Methods: list(include_deleted=False), list_with_metadata(include_deleted=False), history(key), read(key), read_with_version(key), read_version(key, version_id), read_many(keys, max_workers=None), write(model), write_dict(dict, key=None), update(model), update_if_version(model, version), mutate(key, function, max_attempts=3), delete(key), restore_version(key, version_id), undelete(key), and get_lock(key, wait_period=0.1, time_out_seconds=840, maximum_age=900).
    • Implements unique key generation and defers serialization to subclass hooks.

    Example (using factory below for a Pydantic model): see next section.

  • json_file_storage_factory(extension: str, model: Any, key_field: str = "key") -> type[JsonFileStorage]: Generates a concrete JsonFileStorage for a Pydantic model.

    • The resulting class knows how to serialize/deserialize the given model and stores files as <key>.<extension>.

    Example:

    from pydantic import BaseModel
    from mx8fs import json_file_storage_factory
    
    class User(BaseModel):
        key: str | None = None
        name: str
    
    UserStorage = json_file_storage_factory('json', User, key_field='key')
    store = UserStorage('/tmp/users')
    
    u = store.write(User(name='Ada'))     # auto-keyed
    with store.get_lock(u.key):
        u = store.update(User(key=u.key, name='Ada Lovelace'))
    got = store.read(u.key)
    all_keys = store.list()
    store.delete(u.key)
    

Optional gzip storage reuses these handlers:

UserStorage = json_file_storage_factory(
    extension="json", model=User, key_field="key",
    compression="gzip", compression_level=1, fallback_uncompressed=True,
)

The factory configures FileHandler for compression and fallback, using the same JsonFileStorage class with or without gzip. Indexed storage uses that same handler API. Its read fallback catches FileNotFoundError while opening gzip and then opens the uncompressed filename; decoding and validation failures propagate. A separate gzip storage subclass is not required.

Defaults remain uncompressed, with plain fallback disabled. Gzip mode appends .gz to the extension and uses compression level 1 unless configured otherwise. With fallback_uncompressed=True, reads prefer gzip and fall back only on absence; corrupt gzip, invalid JSON and permission errors propagate. Listings deduplicate logical keys, key allocation checks both names, and deletion removes both current representations. Normal writes finish publishing gzip before deleting the plain current object.

Local Unix file modes are not configurable through the storage API. Creating a gzip destination uses normal creation permissions and the process umask; it does not inherit the plain file's mode. Under umask 022, migrating a 0600 plain file can therefore create a 0644 gzip file. Use a restrictive process umask when creating private local records. An existing gzip destination retains its own mode.

Gzip-mode concurrency tokens identify the representation with gzip: or plain:. Pass them unchanged to update_if_version(). Conditional mutations retain the representation read: a plain mutation stays plain until a normal save. A concurrent gzip publication causes a plain mutation to report a version conflict so mutate() can reread and recompute. Normal saves and deletes are not cross-object transactions; coordinate them with get_lock() when the application needs serialization.

Locks use the logical filename so plain and gzip callers serialize access to the same record. With local history enabled, migration captures the plain state before publishing gzip so recovery selects the newly saved value.

History combines both filenames and supplies tagged version_id values for read_version() and restore_version(). Deleted listing metadata includes a tagged latest_readable_version when available. Existing untagged historical IDs are accepted, trying gzip then plain only if that version is absent. Restoring or undeleting writes a new gzip current version. Indexed storage supports the same factory options; run rebuild_index() when switching an existing indexed namespace to gzip to refresh its source versions.

Indexed JSON Storage

json_index_factory adds a SQL secondary index while keeping JSON files canonical. Local storage uses a hidden SQLite database at <base_path>/.mx8fs-index.sqlite3; indexed S3 storage uses Aurora DSQL.

Use pip install "mx8fs[indexed-json-storage]" for indexed storage. It uses SQLite locally and Aurora DSQL for S3 storage.

from datetime import datetime
from pydantic import BaseModel
from mx8fs import json_file_storage_factory, json_index_factory

class Job(BaseModel):
    key: str | None = None
    status: str
    created_at: datetime
    not_before: datetime | None = None

JobIndex = json_index_factory(
    Job,
    fields=["status", "created_at", "not_before"],
    table_name="jobs",  # optional; otherwise derived from the model
)

JobStorage = json_file_storage_factory("json", Job, index=JobIndex)
jobs = JobStorage("/tmp/jobs")

query = (
    jobs.query()
    .where(jobs.index.status == "pending")
    .order_by(jobs.index.created_at.desc())
    .page(1, 50)
)

rows = query.all()       # SQLAlchemy RowMapping objects: key + indexed fields
keys = query.keys()      # ordered keys only
total = query.count()    # count before limit/offset
models = query.models()  # concurrently load and validate the JSON models

Every index definition is fingerprinted from the selected Pydantic fields. Missing index tables and namespaces are created and built automatically from canonical JSON. Existing incompatible or corrupt indexes are never migrated or healed automatically. Different storage paths using the same definition share a physical table but remain isolated by an internal namespace.

Run index migrations as part of deployment, before serving traffic:

jobs = JobStorage("s3://bucket/jobs")
jobs.migrate_index()

migrate_index() explicitly replaces a missing or incompatible physical index when necessary, then rebuilds the current storage namespace. Normal operations validate readiness once per storage instance and raise IndexSchemaError with migration instructions when an existing index is incompatible. A corrupt local SQLite database raises its original database error and must be repaired or removed explicitly before migration.

Indexed S3 storage requires MX8FS_DSQL_ENDPOINT. MX8FS_DSQL_USER defaults to admin, and MX8FS_DSQL_DATABASE defaults to postgres. AWS credentials and Region resolution use the normal AWS chain.

Aurora DSQL engines intentionally use SQLAlchemy pool_pre_ping=True. AWS Lambda execution environments can retain pooled database connections while frozen, after which the remote TLS session may have closed. Pre-ping invalidates that stale connection before application SQL executes. Do not disable it merely to remove a checkout round trip.

rebuild_index() can be called to force a full reconciliation. Rebuilds read canonical objects concurrently with up to 100 workers by default, retain primitive SQL projections rather than full Pydantic models, reconcile the namespace index in bounded write transactions, and verify that source versions remained stable. If objects change during the rebuild, only changed, new, or deleted keys are repaired in bounded delta passes. Pass max_workers to tune read concurrency.

Indexed writes use the ETag or local version returned by the canonical write to update the index without rereading uncontended JSON. The canonical version is checked afterward; a GET and another index synchronization happen only when another writer changed the object concurrently.

Migrating every indexed storage

Runtime storage paths may come from environment variables, dependency injection, tenant configuration, or application factories, so static source scanning cannot reliably construct every storage instance. Applications must provide an authoritative registry callable:

# app/mx8fs_migrations.py
def indexed_storages():
    return {
        "JobStorage": JobStorage(settings.jobs_path),
        "UserStorage": UserStorage(settings.users_path),
    }

Expose that callable as an installed package entry point so only registries explicitly declared by the application can be loaded:

[project.entry-points."mx8fs.index_registries"]
application = "app.mx8fs_migrations:indexed_storages"

Registry keys must match the assigned storage symbols reported by discover-indexes. Poetry applications can declare the same entry-point mapping under [tool.poetry.plugins."mx8fs.index_registries"].

Install the indexed storage extra and run one deployment migration job before starting application replicas:

mx8fs migrate-indexes application \
  --jobs 4 \
  --total-read-workers 100

The runner deduplicates physical table/namespace pairs, migrates each shared physical schema once, then rebuilds independent namespaces concurrently. --jobs limits concurrent namespace migrations. --total-read-workers is divided across those jobs so several migrations do not each create 100 S3 readers. Use --dry-run to print the migration plan without changing index schemas or namespaces, and --json for machine-readable results. Any failed schema or namespace produces a nonzero exit status while independent targets are still attempted.

Do not run this command independently in every application replica. SQL leases protect schema operations, but repeated deployment jobs would still perform redundant namespace rebuilds.

An advisory scanner helps find declarations that may be missing from the registry:

mx8fs discover-indexes ./src \
  --registry application

The scanner parses Python without importing it and reports indexed json_file_storage_factory(..., index=...) declarations with file and line numbers. It understands direct imports, aliases, and module-qualified calls. Its results are advisory: dynamic calls, wrappers, and runtime-generated storage paths may not be discoverable, and the scanner never performs migrations. The explicit registry remains authoritative.

All JSON storage exposes the same three update policies. Indexed storage adds index synchronization without acquiring per-record file locks:

  • update(model) always overwrites canonical JSON, then stabilizes its index from the latest canonical version.
  • update_if_version(model, version) performs one version-matched update attempt and raises VersionMismatchError when the observed version is already stale. S3 provides atomic compare-and-swap through If-Match; local storage has the portable-filesystem race described above.
  • mutate(key, function, max_attempts=3) rereads and reapplies a side-effect-free mutation after version conflicts.
job = jobs.mutate(
    "job-key",
    lambda current: current.model_copy(update={"status": "complete"}),
)

Comparison Utilities

  • class ResultsComparer(ignore_keys: list[str] | None, create_test_data: bool = False, obfuscate_regex: str | None = None): Compare text/JSON with optional obfuscation and test‑data generation.

    • Methods: compare_dicts, get_text_differences, get_dict_differences, get_api_response_differences.
    • Obfuscates sensitive content by default (passwords, tokens, keys, etc.).

    Example:

    from mx8fs import ResultsComparer
    
    rc = ResultsComparer(ignore_keys=['timestamp'])
    diffs = rc.compare_dicts({'a': 1, 'timestamp': 1}, {'a': 2, 'timestamp': 2})
    if diffs:
        print(diffs)
    

Exceptions

  • class VersionMismatchError(FileNotFoundError): Raised by update_file_if_version_matches on conditional write mismatches.

  • VersioningNotEnabledError: Local history was requested while MX8FS_LOCAL_VERSIONING is off.

  • VersioningNotSupportedError: The path backend does not support history.

  • VersionNotFoundError: The requested historical version does not exist.

  • FileVersionDeletedError: The selected version is a delete marker and has no content.

  • FileNotDeletedError: Undelete was requested for a currently live file.

    Example (catching):

    from mx8fs import VersionMismatchError
    try:
        raise VersionMismatchError('example')
    except VersionMismatchError:
        pass
    

Notes

  • S3 settings: the client uses environment variables BOTO_MAX_CONNECTIONS, BOTO_CONNECT_TIMEOUT, BOTO_READ_TIMEOUT, BOTO_MAX_RETRIES, and BOTO_RETRY_MODE to tune behavior.
  • Time handling: cutoff_date is normalized to timezone‑aware UTC for consistent comparisons.
  • HTTPS reads: read_file and BinaryFileHandler (rb) support https:// sources; writing to HTTPS is not supported.

Pre-commit hooks

We use precommit to run formatting checks, so whenever you clone a project run:

pre-commit install

Before you do anything else.

You can run this at any time using:

pre-commit run --all-files

Setting up the development environment

You can install the full dev requirements by running setup.sh to

  1. Install the current repo and the python lib
  2. Run the pre-commit hooks on all files

The project should open reasonable well in vs.code and includes three launch configurations for running unit tests and the debug server.

Code conventions and structure

We use python type hinting with pylance and flake8 for linting. Unit tests are created to 100% branch coverage.

The code is structured as follows:

  • The mx8fs folder contains the full library.
  • Tests are stored in the tests folder and run using pytest.

License

Copyright © 2025 MX8 Labs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Development setup

Run ./setup.sh to install dependencies and configure the environment.

Tooling

Use black, ruff, and mypy for formatting/linting/type checking. Run pytest for tests.

Metadata

Release files for mx8fs 2.0.0.45

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mx8fs 2.0.0.45
File Size Uploaded Provenance Actions
mx8fs-2.0.0.45.tar.gz 69.5 kB With provenance Details for mx8fs-2.0.0.45.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mx8fs 2.0.0.45
File Interpreter ABI Platform Actions
mx8fs-2.0.0.45-py3-none-any.whl Python 3 none any Details for mx8fs-2.0.0.45-py3-none-any.whl

Total release size: 134.5 kB

Release files / mx8fs-2.0.0.45.tar.gz

Download URL mx8fs-2.0.0.45.tar.gz
Size 69.5 kB
Tags Source
SHA-256 checksum
How to use checksums
cb9193dfa4ddaa729e8b1a5d2f2ae01b6f73ea4937ec15923585f50b0d7ecd01
BLAKE2b-256 checksum
How to use checksums
5c8666617747c3a01d3de327428eb943ead74281cc638d1c8b86cfe3a15a1055
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Release files / mx8fs-2.0.0.45-py3-none-any.whl

Download URL mx8fs-2.0.0.45-py3-none-any.whl
Size 65.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0b92614c03e91f3963fac5bf007add077b3c83e86a1c9b18238204174ba4fd5f
BLAKE2b-256 checksum
How to use checksums
25e64c20784521a3e900159ef229cdd670018e470e3f4cb2599e2d9ebdee758c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Security for mx8fs 2.0.0.45

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mx8fs-2.0.0.45.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.0.45 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page