oc_chat
Loading...
Searching...
No Matches
oc_chat.registry.source.canonical Namespace Reference

Classes

class  _FlowMap
class  CanonicalDumper

Functions

yaml.ScalarNode _represent_str (yaml.SafeDumper dumper, str data)
yaml.MappingNode _represent_flowmap (yaml.SafeDumper dumper, _FlowMap data)
Any _field_default (Any field_info)
Any _to_yaml_obj (Any value)
Any _depends_on_to_yaml (Any edge)
dict[str, Any] model_to_frontmatter (RegistryModel model)
str dump_frontmatter (dict[str, Any] mapping)
str _normalize_body (str body)
str format_record_text (RegistryModel model, str body="")
tuple[str, str] split_record_text (str text)
None _assert_no_yaml_features (str frontmatter, str rel_path)
Any _model_for (ScannedRecord scanned)
tuple[RegistryModel, str] load_record (ScannedRecord scanned)
None _check_identity (ScannedRecord scanned, RegistryModel model)
str canonicalize_file (ScannedRecord scanned)
list[str] format_source_tree (list[ScannedRecord] records, *, bool check=False)
bytes canonical_record_bytes (ScannedRecord scanned)

Variables

str _STR_TAG = "tag:yaml.org,2002:str"
frozenset _RESERVED_DEVICE_STEMS

Detailed Description

Canonical YAML dumper + record loader for the AuthoredSource format.

Purpose
-------
Read a source record (``yaml.safe_load``) and re-emit byte-canonical frontmatter
with a custom ``yaml.SafeDumper`` subclass, per Registry Source & Storage Spec
V0.22 §3.2: scalar string values double-quoted, plain keys, field-declaration
key order, pydantic defaults + empty containers omitted, ``depends_on``
canonical emission, no anchors/aliases/tags, LF / UTF-8 / no-BOM / one trailing
newline. The formatter is idempotent (AC §8.4).

Responsibilities
----------------
- ``format_record_text(model, body)`` — canonical bytes for a record.
- ``load_record(scanned)`` — parse + strict-validate against the P1.1 model.
- ``canonicalize_file`` / ``format_source_tree`` — format or ``--check`` a tree.

Diagnostics
-----------
Domain: OC_CHAT.MAIN
Levels:
    L2 - format/load lifecycle
    L3 - per-record canonicalisation
    L4 - YAML node emission details

Contracts
---------
- Idempotent: ``format(format(x)) == format(x)``.
- Round-trip safe: a canonical record re-parses to an equal model.
- All string scalar VALUES double-quoted (round-trips any single-line content);
  keys emitted plain. ``depends_on`` blocks → bare quoted id; enables/gates →
  inline ``{id: "..", kind: ..}`` (never ``{id, kind: blocks}``).

Dependencies
------------
- oc_diagnostics (structured debug logging via _dbg)
- yaml (safe_load + custom SafeDumper)
- option_c_registry_contracts (P1.1 entity models)
- oc_chat.registry.source.errors (SRC_* codes)
- oc_chat.registry.source.promoted_item (PromotedItem - the model for promoted_item records)
- oc_chat.registry.source.scanner (ScannedRecord - the scanned record every loader and formatter takes)
- pydantic (ValidationError - reported as an SRC_STRICT_TYPE failure)
- pydantic_core (PydanticUndefined - telling a field with no default apart)

Function Documentation

◆ _assert_no_yaml_features()

None oc_chat.registry.source.canonical._assert_no_yaml_features ( str frontmatter,
str rel_path )
protected
Reject anchors/aliases/merge/custom-tags (§3.2) → SRC_BAD_YAML_FEATURE.

Definition at line 240 of file canonical.py.

Here is the caller graph for this function:

◆ _check_identity()

None oc_chat.registry.source.canonical._check_identity ( ScannedRecord scanned,
RegistryModel model )
protected
Filename stem == frontmatter id (§4); items/<kind>/ matches track_kind.

[NEW·V24] A changelog ``version`` record's identity is its ``slug`` field
(§3.5 / §4 item 6), so its stem is now checked against ``slug`` (the earlier
``version``-skip is removed); ``framing`` records check stem == ``id`` via the
generic path. Only ``manifest`` (which has no id) is exempt.

Definition at line 326 of file canonical.py.

Here is the caller graph for this function:

◆ _depends_on_to_yaml()

Any oc_chat.registry.source.canonical._depends_on_to_yaml ( Any edge)
protected
Canonical ``depends_on`` emission (§3.2 / AC §8.36).

Definition at line 159 of file canonical.py.

Here is the caller graph for this function:

◆ _field_default()

Any oc_chat.registry.source.canonical._field_default ( Any field_info)
protected

Definition at line 141 of file canonical.py.

Here is the caller graph for this function:

◆ _model_for()

Any oc_chat.registry.source.canonical._model_for ( ScannedRecord scanned)
protected

Definition at line 272 of file canonical.py.

Here is the caller graph for this function:

◆ _normalize_body()

str oc_chat.registry.source.canonical._normalize_body ( str body)
protected

Definition at line 204 of file canonical.py.

Here is the caller graph for this function:

◆ _represent_flowmap()

yaml.MappingNode oc_chat.registry.source.canonical._represent_flowmap ( yaml.SafeDumper dumper,
_FlowMap data )
protected

Definition at line 133 of file canonical.py.

◆ _represent_str()

yaml.ScalarNode oc_chat.registry.source.canonical._represent_str ( yaml.SafeDumper dumper,
str data )
protected

Definition at line 121 of file canonical.py.

◆ _to_yaml_obj()

Any oc_chat.registry.source.canonical._to_yaml_obj ( Any value)
protected

Definition at line 149 of file canonical.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ canonical_record_bytes()

bytes oc_chat.registry.source.canonical.canonical_record_bytes ( ScannedRecord scanned)
The canonical UTF-8 bytes for a record (source-tree-hash preimage input).

Definition at line 402 of file canonical.py.

Here is the call graph for this function:

◆ canonicalize_file()

str oc_chat.registry.source.canonical.canonicalize_file ( ScannedRecord scanned)
Return the canonical text for a scanned record (load + re-emit).

Definition at line 366 of file canonical.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dump_frontmatter()

str oc_chat.registry.source.canonical.dump_frontmatter ( dict[str, Any] mapping)
Serialize a frontmatter mapping to canonical YAML text (LF, no BOM).

Definition at line 192 of file canonical.py.

Here is the caller graph for this function:

◆ format_record_text()

str oc_chat.registry.source.canonical.format_record_text ( RegistryModel model,
str body = "" )
Return the canonical bytes (text) of a record: frontmatter + body.

Definition at line 210 of file canonical.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ format_source_tree()

list[str] oc_chat.registry.source.canonical.format_source_tree ( list[ScannedRecord] records,
* ,
bool check = False )
Format (or, with ``check=True``, verify) every formattable record.

Returns the list of relative paths that were rewritten (or, in check mode,
the non-canonical paths). [OP-5] promoted parity: promoted items are
canonicalised + ``--check``-verified like every other entity (the earlier
model-gap skip is removed) so ``format --check`` catches a non-canonical
promoted file.

Definition at line 372 of file canonical.py.

Here is the call graph for this function:

◆ load_record()

tuple[RegistryModel, str] oc_chat.registry.source.canonical.load_record ( ScannedRecord scanned)
Load + strict-validate a scanned record into its P1.1 model + body.

Definition at line 287 of file canonical.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ model_to_frontmatter()

dict[str, Any] oc_chat.registry.source.canonical.model_to_frontmatter ( RegistryModel model)
Build the frontmatter mapping in field-declaration order, omitting
pydantic defaults and empty containers (§3.2).

Definition at line 168 of file canonical.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ split_record_text()

tuple[str, str] oc_chat.registry.source.canonical.split_record_text ( str text)
Split a record file into (frontmatter_text, body); LF-normalised.

Definition at line 219 of file canonical.py.

Here is the caller graph for this function:

Variable Documentation

◆ _RESERVED_DEVICE_STEMS

frozenset oc_chat.registry.source.canonical._RESERVED_DEVICE_STEMS
protected
Initial value:
1= frozenset(
2 {"con", "prn", "aux", "nul"}
3 | {f"com{i}" for i in range(1, 10)}
4 | {f"lpt{i}" for i in range(1, 10)}
5)

Definition at line 87 of file canonical.py.

◆ _STR_TAG

str oc_chat.registry.source.canonical._STR_TAG = "tag:yaml.org,2002:str"
protected

Definition at line 81 of file canonical.py.