# MimeArchiveFS-py
+
+ ## MimeArchiveFS-py (`mimearchivefs`) Specification & Skeleton
+
+ This document outlines the specification and proposed project structure for the `mimearchivefs` Python package, designed to implement a virtual filesystem based on an MHTML-compatible single-file archive format (`.mht`). The structure is modeled after the `_priv/twat_mp` project.
+
+ ### 1. Core Concept
+
+ - **Format:** A single `.mht` file containing multiple files.
+ - **Structure:** Uses MIME multipart structure (`multipart/mixed` or similar).
+ - **File Delimiter:** Each file entry is marked by a distinct boundary, potentially incorporating a `--- FILE: path/to/file ---` marker within a header or the part itself for easy identification.
+ - **Encoding:** Plaintext files stored as-is (UTF-8 preferred). Binary files encoded using Base64.
+ - **Compatibility:** Backwards-compatible with standard MHTML parsers. Filesystem access provided via `fsspec`.
+
+ ### 2. Core Functionality
+
+ - **Parsing:** Read and parse `.mht` files into an internal representation (e.g., a dictionary mapping paths to content/metadata).
+ - **fsspec Interface:** Implement `fsspec.spec.AbstractFileSystem` (`MimeArchiveFileSystem`).
+ - **Read Operations:**
+ - List directory contents (`ls`).
+ - Get file status/metadata (`info`).
+ - Read file content (`open`, `cat_file`).
+ - Check existence (`exists`).
+ - **Write Operations (Stretch Goal):**
+ - Create/overwrite files (`pipe_file`, `put_file`).
+ - Remove files/directories (`rm_file`, `rmdir`).
+ - Create directories (`mkdir`).
+ - Save changes back to the `.mht` file or a new file (`save`).
+ - **Metadata:** Store basic file metadata (name, size, potentially modification time if feasible within the format).
+
+ ### 3. Proposed Project Structure
+
+ ```
+ mimearchivefs/
+ ├── .github/
+ │ └── workflows/
+ │ └── ci.yml # GitHub Actions for testing, linting, publishing
+ ├── .gitignore
+ ├── .pre-commit-config.yaml # Pre-commit hooks (ruff, mypy, etc.)
+ ├── CHANGELOG.md
+ ├── LICENSE
+ ├── NOTES.md # Developer notes (like the source for this spec)
+ ├── README.md # Project overview, usage, installation
+ ├── TODO.md # This file
+ ├── docs/ # Documentation (Sphinx or MkDocs)
+ │ ├── api.md
+ │ ├── conf.py # (if Sphinx)
+ │ ├── index.md
+ │ ├── make.bat # (if Sphinx)
+ │ ├── Makefile # (if Sphinx)
+ │ └── usage.md
+ ├── examples/
+ │ └── basic_usage.py # Example scripts demonstrating usage
+ ├── pyproject.toml # Project metadata, dependencies, build config (using hatch)
+ ├── src/
+ │ └── mimearchivefs/
+ │ ├── __init__.py # Exports MimeArchiveFileSystem, core functions
+ │ ├── cli.py # Optional CLI interface (using fire/rich)
+ │ ├── core.py # Parsing & writing logic for the .mht format
+ │ ├── exceptions.py # Custom exceptions
+ │ ├── filesystem.py # fsspec implementation (MimeArchiveFileSystem, MimeArchiveFile)
+ │ └── utils.py # Helper functions (path handling, encoding etc.)
+ └── tests/
+ ├── conftest.py # Pytest fixtures
+ ├── fixtures/ # Sample .mht files for testing
+ │ ├── empty.mht
+ │ ├── simple.mht
+ │ └── binary.mht
+ ├── test_cli.py # Tests for the CLI
+ ├── test_core.py # Unit tests for parsing/writing
+ ├── test_filesystem.py # Integration tests using fsspec's test suite
+ └── test_utils.py # Tests for utility functions
+ ```
+
+ ### 4. Key Classes & Functions
+
+ - **`src/mimearchivefs/filesystem.py`:**
+ - `MimeArchiveFileSystem(fsspec.spec.AbstractFileSystem)`
+ - `__init__(self, path_to_mht, mode='r', **storage_options)`: Loads/parses the MHT file on init.
+ - `_load_archive(self)`: Internal method to parse the MHT.
+ - `ls`, `info`, `exists`, `cat_file`, `open`, etc. (Standard fsspec methods)
+ - (Write methods): `pipe_file`, `put_file`, `rm_file`, `save()` etc.
+ - `MimeArchiveFile(fsspec.spec.AbstractBufferedFile)`: File-like object returned by `open()`. Handles reading (and potentially writing) content, including decoding Base64.
+ - **`src/mimearchivefs/core.py`:**
+ - `parse_mht(file_obj)`: Takes a file-like object, parses the MIME structure using `email.parser`, extracts files, decodes content, and returns a structured representation (e.g., `dict[str, bytes | str]`).
+ - `write_mht(file_obj, file_data: dict)`: Takes a structured representation, constructs the MIME message using `email.message` / `email.mime`, encodes binary files, and writes to the file-like object.
+ - Helper functions for identifying file boundaries, extracting paths, handling encodings.
+ - **`src/mimearchivefs/exceptions.py`:**
+ - `MimeArchiveFormatError(Exception)`: For parsing errors.
+ - `FileNotFoundInArchiveError(FileNotFoundError)`: Specific file not found.
+
+ ### 5. Dependencies
+
+ - **Core:** `fsspec`
+ - **Standard Library:** `email`, `base64`, `mimetypes`, `io`, `os`
+ - **Development:** `pytest`, `pytest-cov`, `ruff`, `mypy`, `hatch` (or `uv`), `pre-commit`
+ - **Optional CLI:** `python-fire`, `rich`
+ - **Optional Docs:** `sphinx` or `mkdocs`
+
+ ### 6. Development & Testing
+
+ - **Environment:** Use `hatch` or `uv` for virtual environment and dependency management.
+ - **Linting/Formatting:** `ruff` (configured in `pyproject.toml`), `mypy` for type checking.
+ - **Testing:** `pytest`. Use `fsspec.tests.abstract.AbstractFileSystemTests` for comprehensive filesystem interface testing. Add specific unit tests for `core.py` logic.
+ - **CI:** GitHub Actions to run tests, linters on pushes/PRs.
+
+ ### 7. Documentation
+
+ - **README:** High-level overview, installation, quick start.
+ - **Usage Guide:** Detailed examples of reading and writing archives.
+ - **API Reference:** Auto-generated from docstrings (using Sphinx autodoc or similar).
+ - **Format Spec:** Clear description of the `--- FILE: ---` marker and expected MIME structure.
+
+ ### 8. Initial Tasks (High-Level)
+
+ 1. Set up project structure using `hatch new mimearchivefs`.
+ 2. Configure `pyproject.toml` (dependencies, linters, build).
+ 3. Implement basic parsing logic in `core.py` using `email.parser`.
+ 4. Implement read-only `MimeArchiveFileSystem` in `filesystem.py`, leveraging `core.py`.
+ 5. Write tests for `core.py` and `filesystem.py` (read operations).
+ 6. Set up CI and pre-commit hooks.
+ 7. Write basic README and usage docs.
+ 8. (Later) Implement write operations and associated tests.
+ 9. (Later) Implement optional CLI.
OK, I've added the specification and proposed skeleton to TODO.md. This provides a roadmap for building the mimearchivefs package, including its core functionality, structure, key components, dependencies, and initial development steps.