TextFSM-compatible template parser for Acton, focused on:
- 100% compatibility with TextFSM templates
- Type-safe, ergonomic Acton interface
- Usage varies from the Python version
from textfsm import parse_textfsm, compile_textfsm
def main():
template = r"""Value IFACE (\S+)
Value STATUS (up|down)
Start
^${IFACE}\s+${STATUS} -> Record
"""
rows = parse_textfsm(template, "ge0 up\nge1 down\n")
for row in rows:
print(row.get("IFACE"), row.get("STATUS"))
compiled = compile_textfsm(template)
rows2 = compiled.parse("ge2 up\n")
print(rows2[0].get("IFACE"))
parse_textfsm(template: str, text: str, eof: bool=True) -> list[Row]compile_textfsm(template: str) -> CompiledTemplateCompiledTemplate.parse(text: str, eof: bool=True) -> list[Row]CompiledTemplate.schema() -> list[FieldSchema]Row.get(field: str) -> ?strRow.get_def(field: str, default: str) -> strRow.many(field: str) -> list[str]Row.many_records(field: str) -> list[dict[str, str]](for nested named groups inValue List)Row.to_dict() -> dict[str, list[str]]
Row.get() returns None when a field has no capture. A field that
captures an empty string returns "".
CompiledTemplate.schema() returns one FieldSchema per Value, in
template order. FieldSchema.is_list is True for Value List values
and False for normal values. FieldSchema.record_fields contains nested
named capture groups for Value List, which are exposed on parsed rows
through Row.many_records().
ErrorUsageErrorTextFSMError(runtime parse errors, e.g.-> Error)TextFSMTemplateError(template syntax/semantic errors)
Supported parser/runtime semantics include:
Valueoptions:Required,Filldown,Fillup,List- Rule actions:
Next,Continue,Error,Record,NoRecord,Clear,Clearall - State transitions including
Start,End,EOF - Implicit EOF record behavior and
eof=Falsesuppression - Variable substitution forms:
$name,${name},$$ - Nested named capture groups for
Value List(exposed viaRow.many_records())
Intentionally not implemented:
Value Keyclitable
Acton strings interpolate by default. Use raw strings for templates:
template = r"""..."""
This avoids escaping issues around $name, ${name}, and regex backslashes.
This project is licensed under Apache License 2.0; see LICENSE.
The parser behavior and parts of the test corpus are derived from upstream Python TextFSM:
Upstream TextFSM is also licensed under Apache License 2.0.