Metadata-Version: 2.4
Name: vcti-path-format-descriptors
Version: 2.0.1
Summary: Built-in file format descriptors (HDF5, CAX, JSON, NPY, NPZ, CSV) for the vcti-path-format identification framework
Author: Visual Collaboration Technologies Inc.
License-Expression: LicenseRef-Proprietary
Project-URL: Repository, https://github.com/vcollab/vcti-python-path-format-descriptors
Project-URL: Changelog, https://github.com/vcollab/vcti-python-path-format-descriptors/blob/main/CHANGELOG.md
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: <3.15,>=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: vcti-path-format>=2.1.0
Requires-Dist: vcti-path-format-attributes>=2.0.0
Requires-Dist: vcti-plugin-catalog>=2.0.0
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Provides-Extra: lint
Requires-Dist: ruff; extra == "lint"
Provides-Extra: typecheck
Requires-Dist: mypy; extra == "typecheck"
Dynamic: license-file

# Path Format Descriptors

Built-in file format descriptors (HDF5, CAX, JSON, NPY, NPZ, CSV) for the vcti-path-format identification framework

## Overview

`vcti-path-format-descriptors` ships ready-made `FormatDescriptor`
instances for the file formats VCTI tooling needs to recognize: HDF5,
VCollab CAX, JSON, NumPy NPY/NPZ, and CSV. Each descriptor is built by a
factory function that wires a file-only precondition and the appropriate
magic-byte and/or extension validators onto a `HeuristicEvaluator`, tags
the result with attributes from the shared vocabulary
(`vcti-path-format-attributes`), and returns it for registration with a
`FormatRegistry`. Applications register the descriptors they need (or all
of them at once) and let `FormatIdentifier` do the identification.

## Installation

```bash
pip install "vcti-path-format-descriptors>=2.0.1"
```

### In `pyproject.toml` dependencies

```toml
dependencies = [
    "vcti-path-format-descriptors>=2.0.1",
]
```

---

## Quick Start

```python
from pathlib import Path

from vcti.pathformat.core import FormatIdentifier, FormatRegistry
from vcti.pathformat.descriptors import HDF5_SIGNATURE, register_all_formats

# Register all built-in format descriptors
registry = FormatRegistry()
register_all_formats(registry)

# Identify a file
path = Path("data.h5")
path.write_bytes(HDF5_SIGNATURE + b"\x00" * 64)

identifier = FormatIdentifier(registry)
best = identifier.get_best_match(path)
print(best.id if best else "unknown")  # hdf5-file

for result in identifier.identify_file_format(path)[:2]:
    print(result.descriptor.id, result.confidence.name)
```

### Fast identification at ingest

When every incoming file is identified before it is stored, run only the
cheap checks (a few bytes and the name) and leave deeper checks, and
integrity, to a later pass or to the format's reader:

```python
from vcti.pathformat.core import MatchConfidence, ValidationTier
from vcti.pathformat.descriptors import __version__ as descriptors_version

# Results are ranked, so the first is the best; keep its confidence too.
top = identifier.identify_file_format(path, max_tier=ValidationTier.IDENTIFICATION)[0]
matched = top.confidence.value >= MatchConfidence.LIKELY.value
record = {
    "format": top.descriptor.id if matched else None,
    "confidence": top.confidence.name,
    "tier": ValidationTier.IDENTIFICATION.name,
    "descriptors_version": descriptors_version,
}
print(record)  # {'format': 'hdf5-file', 'confidence': 'DEFINITE', 'tier': 'IDENTIFICATION', ...}

# Later, a refinement pass can run every tier. CANT_EVALUATE means "no
# evidence yet" (keep the earlier answer); CERTAINLY_NOT means "evidence
# against".
refined = identifier.identify_file_format(path)[0]
assert refined.confidence is MatchConfidence.DEFINITE
```

See [docs/design.md](docs/design.md) (*Identify fast, refine later*) for
what each format reads at each tier and what the results mean.

### Individual descriptors

```python
from vcti.pathformat.core import FormatRegistry
from vcti.pathformat.descriptors import (
    get_csv_file_descriptor,
    get_hdf5_file_descriptor,
)

registry = FormatRegistry()
registry.register(get_hdf5_file_descriptor())
registry.register(get_csv_file_descriptor())
```

Each factory returns a new descriptor on every call, so a descriptor can
be registered in several registries or adjusted without affecting others.

---

## API

| Name | Kind | Purpose |
|------|------|---------|
| `register_all_formats(registry)` | function | Register all six built-in descriptors, or none if any ID is taken |
| `get_<format>_file_descriptor()` | function | Build one descriptor (`hdf5`, `cax`, `json`, `npy`, `npz`, `csv`) |
| `<FORMAT>_FILE_FORMAT_DESCRIPTOR_ID` | constant | The descriptor's registry ID |
| `HDF5_SIGNATURE`, `CAX_FILE_SIGNATURE`, `NPY_SIGNATURE`, `NPZ_SIGNATURE`, `NPZ_EMPTY_SIGNATURE` | constant | Magic-byte signatures, as `bytes` |
| `HDF5_SUPERBLOCK_OFFSETS` | constant | Offsets at which the HDF5 signature is accepted |
| `NpzMembersValidator` | class | STRUCTURE-tier GATE: every ZIP member is an `.npy` file |
| `GENERATOR_APPLICATION` | constant | The CAX descriptor's `generator` attribute value |

## Built-in Formats

Every descriptor first requires a regular file (a `PathKind.FILE`
precondition), so a directory is ruled out before any file is read.

| ID | Signature | Extensions | Best confidence | Attributes |
|----|-----------|------------|-----------------|------------|
| `hdf5-file` | `\x89HDF\r\n\x1a\n` at offset 0, 512, 1024, 2048, … | `.h5`, `.hdf5` | DEFINITE | structure=hdf5 |
| `vcti-cax` | `\x89VCF\r\n\x1a\n` | — | DEFINITE | structure=binary, generator=VCollab, content=[4 values] |
| `json-file` | — (text format) | `.json` | LIKELY | structure=json |
| `npy-file` | `\x93NUMPY` | `.npy` | DEFINITE | structure=binary |
| `npz-file` | `PK\x03\x04`, or `PK\x05\x06` for an empty archive; members all `.npy` | `.npz` | DEFINITE | structure=binary |
| `csv-file` | — (text format) | `.csv` | LIKELY | structure=csv |

How the validators combine:

- **Signature and extension both match** — DEFINITE.
- **Signature matches, extension does not** (a renamed file) — LIKELY,
  so `get_best_match()` still finds it.
- **Signature does not match** — CERTAINLY_NOT, whatever the extension.
- **Text formats (JSON, CSV)** have no signature; the extension alone
  gives LIKELY, and the content is not inspected. They have
  `priority=-1`, so when a renamed binary file is LIKELY for both its own
  format and a text format, its own format ranks first.
- **Extensions** are compared case-insensitively.

An HDF5 file may start with a user block, so the signature is accepted at
any offset the HDF5 specification allows. A MATLAB v7.3 `.mat` file, which
is HDF5 behind a 512-byte header, is therefore LIKELY as HDF5.

The NPZ signature is a generic ZIP signature. At the STRUCTURE tier, the
NPZ descriptor also lists the archive's members (reading only the ZIP
central directory) and rules out an archive with any member that is not
an `.npy` file. A caller that passes `max_tier=ValidationTier.IDENTIFICATION`
skips that check and relies on the signature and the `.npz` extension.

---

## Dependencies

- [vcti-path-format](https://pypi.org/project/vcti-path-format/) (>=2.1.0) — format identification framework
- [vcti-path-format-attributes](https://pypi.org/project/vcti-path-format-attributes/) (>=2.0.0) — domain vocabulary enums
- [vcti-plugin-catalog](https://pypi.org/project/vcti-plugin-catalog/) (>=2.0.0) — `DuplicateEntryError`, raised by `register_all_formats()`

## Documentation

| If you want to… | Read |
|---|---|
| Get started using the package | Quick Start above |
| Understand where identification sits in the file lifecycle, and how to identify fast and refine later | [docs/design.md](docs/design.md) (*Identify fast, refine later*) |
| Understand the design decisions | [docs/design.md](docs/design.md) |
| Navigate and understand the source | [docs/source-guide.md](docs/source-guide.md) |
| Add a new format descriptor | [docs/extending.md](docs/extending.md) |
