Parser Infrastructure¶
StructureRegistry¶
- class aws.osml.io.StructureRegistry¶
Bases:
objectManages loading, caching, and lookup of structure definitions.
The registry discovers
.ksystructure definition files from one or more search paths and makes them available by name. Built-in definitions for NITF headers, image subheaders, and common TREs are loaded automatically. You can extend the registry by adding custom search paths or registering definitions at runtime. Useget()to obtain aStructureDefinitionfor use withStructureAccessororStructureWriter.Example:
```python from aws.osml.io import StructureRegistry
registry = StructureRegistry() registry.add_search_path(“/path/to/my/structures”) definition = registry.get(“TRE_GEOLOB”) for name in registry.list():
print(name)
- add_search_path(path)¶
Add a search path with higher priority than existing paths.
- Parameters:
path (str) – Directory path to add to the search paths.
- get(name)¶
Retrieve a structure definition by name.
- Parameters:
name (str) – The structure name (e.g.,
"NITF_02.10_FileHeader","TRE_GEOLOB").- Returns:
The
StructureDefinition, orNoneif not found.- Return type:
StructureDefinition or None
- list()¶
List all available structure definition names.
- register(name, definition)¶
Register a definition at runtime with highest priority.
Runtime-registered definitions take priority over file-based definitions with the same name.
- Parameters:
name (str) – The name to register the definition under.
definition (StructureDefinition) – The
StructureDefinitionto register.
- reload()¶
Reload all definitions from disk.
Clears the file cache and re-scans search paths. Definitions registered at runtime via
register()are preserved.- Raises:
RuntimeError – If a search path cannot be read.
StructureDefinition¶
- class aws.osml.io.StructureDefinition¶
Bases:
objectA read-only structure definition parsed from a
.ksyfile.Contains field names and layout information used by
StructureAccessorandStructureWriterto read and write binary data. Obtain instances fromStructureRegistryviaStructureRegistry.get().Example:
`python definition = registry.get("TRE_GEOLOB") print(definition.id, definition.field_names) `- field_names¶
Ordered list of field names defined in this structure.
- id¶
The unique identifier for this structure (e.g.,
"TRE_GEOLOB").
- title¶
Human-readable title from the definition, or
Noneif not set.
StructureAccessor¶
- class aws.osml.io.StructureAccessor(definition, data)¶
Bases:
objectProvides lazy, dict-like read access to fields in binary data.
A
StructureAccessorparses fields on demand according to aStructureDefinition, caching computed offsets for efficiency. Access fields with bracket notation (accessor["field_name"]), use dot-notation paths for nested fields ("parent.child"), check for conditional fields withhas()or theinoperator, and iterate over all accessible paths withfields(). Each field access returns aValueobject.Example:
```python from aws.osml.io import StructureAccessor
accessor = StructureAccessor(definition, data) value = accessor[“field_name”] if accessor.has(“optional_field”):
print(accessor[“optional_field”].as_str())
- for path in accessor.fields():
print(path, accessor[path])
- data¶
The underlying binary data buffer.
- definition¶
The
StructureDefinitionused by this accessor.
- fields()¶
List all accessible field paths.
- has(path)¶
Check if a field exists and is accessible.
Returns
Falsefor conditional fields whose condition is not met.
- raw_view(path)¶
Return the raw bytes for a field without interpretation.
Useful for passing binary data to external decoders.
- Parameters:
path (str) – The field path.
- Returns:
The raw bytes for the field.
- Return type:
- Raises:
KeyError – If the field does not exist.
RuntimeError – If the field is not contiguous in memory.
StructureWriter¶
- class aws.osml.io.StructureWriter¶
Bases:
objectEncodes values into binary data according to a
StructureDefinition.A
StructureWriterserializes field values into the correct binary layout defined by a.ksystructure definition. Fields must be written in definition order. Callfinish()to retrieve the final encoded bytes. Field values are set using bracket notation or theset()method, and accepted types includestr,int,float, andbytes. For repeated fields, write elements sequentially with indexed paths (field_0,field_1, …).Example:
```python from aws.osml.io import StructureWriter
writer = StructureWriter.new_streaming(definition) writer[“field1”] = “value1” writer[“field2”] = 42 data = writer.finish() ```
- buffer()¶
Return the current buffer contents without validation.
Useful for debugging or inspecting partial writes.
- Returns:
The current buffer contents.
- Return type:
- finish()¶
Finalize the writer and return the encoded bytes.
After calling
finish(), the writer is consumed and cannot be used again.- Returns:
The encoded binary data.
- Return type:
- Raises:
ValueError – If required fields have not been written.
RuntimeError – If the writer has already been finalized.
Example:
`python writer = StructureWriter.new_fixed(definition) writer["FHDR"] = "NITF" writer["FVER"] = "02.10" data = writer.finish() `
- is_set(path)¶
Check if a field has been written.
- static new_streaming(definition)¶
Create a streaming writer.
Fields must be written in definition order.
- Parameters:
definition (StructureDefinition) – The
StructureDefinitionto encode against.- Returns:
A new writer.
- Return type:
Value¶
- class aws.osml.io.Value¶
Bases:
objectRepresents a parsed field value from binary structure data.
A
Valueis returned byStructureAccessorwhen you access a field. It holds the raw parsed content and provides type conversion methods —as_str(),as_int(),as_float(), andas_bytes()— for interpreting ASCII-encoded fields as native Python types. Most binary format fields (e.g. NITF header fields) are stored as fixed-width ASCII strings, so these converters handle trimming and numeric parsing automatically.Example:
`python value = accessor["NROWS"] num_rows = value.as_int() print(value.as_str()) `- as_bytes()¶
Return the raw bytes of the value.
- Returns:
The raw byte representation.
- Return type:
- as_float()¶
Parse the value as a floating-point number.
Blank or whitespace-only strings are parsed as
0.0.
- as_int()¶
Parse the value as a signed integer.
Blank or whitespace-only strings are parsed as
0.