Format System
The format system provides pluggable serialization (export) and deserialization (import) of entries across multiple file formats. It powers content negotiation in the REST API and bulk import/export in the CLI.
Key Files
| File | Purpose | |------|---------| | `pyrite/formats/__init__.py` | `FormatRegistry`, `FormatSpec`, `negotiate_format()`, `format_response()` | | `pyrite/formats/json_fmt.py` | JSON serializer (`json_serialize`) | | `pyrite/formats/markdown_fmt.py` | Markdown serializer | | `pyrite/formats/csv_fmt.py` | CSV serializer | | `pyrite/formats/yaml_fmt.py` | YAML serializer | | `pyrite/formats/importers/__init__.py` | `ImporterRegistry`, importer registration | | `pyrite/formats/importers/json_importer.py` | JSON importer (`import_json`) | | `pyrite/formats/importers/markdown_importer.py` | Markdown importer (`import_markdown`) | | `pyrite/formats/importers/csv_importer.py` | CSV importer (`import_csv`) |
Export: `FormatRegistry`
`FormatSpec` Dataclass
Each format is described by a `FormatSpec` with `name`, `media_type`, `file_extension`, and a `serializer` callable `(data, **kwargs) -> str`.
Registered Formats
| Name | Media Type | Extension | Serializer | |------|-----------|-----------|------------| | `json` | `application/json` | `.json` | `json_serialize` (indented, `default=str`) | | `markdown` | `text/markdown` | `.md` | `markdown_serialize` | | `csv` | `text/csv` | `.csv` | `csv_serialize` | | `yaml` | `text/yaml` | `.yaml` | `yaml_serialize` |
Key Functions
Content Negotiation
`negotiate_format(accept_header: str) -> str | None`
Parses the HTTP `Accept` header, sorts by quality factor (`q=`), and returns the name of the best matching format. Defaults to `"json"` for `/` or empty headers. Returns `None` when no registered format matches (signaling HTTP 406).
Import: `ImporterRegistry`
Registered Importers
| Name | Importer | |------|----------| | `json` | `import_json` | | `markdown` | `import_markdown` | | `csv` | `import_csv` |