YANG Statements
This module implements the following classes:
ModuleParser
: Recursive-descent parser for YANG modules.Statement
: YANG statements.
Doctest snippets for this module use the YANG module example-5-a, see Example 5.
>>> dm = DataModel.from_file("yang-library-ex5.json")
>>> mex5a = dm.schema_data.modules[('example-5-a', '2018-10-25')].statement
- class yangson.statement.Statement(kw: YangIdentifier, arg: str | None, pref: YangIdentifier = None)
An instance of this class represents a parsed YANG statement. The constructor arguments kw, arg and pref initialize instance attributes
keyword
,argument
andprefix
, respectively.Instance Attributes
- keyword
The statement’s keyword. For extension statements, this is the local part of the keyword.
>>> mex5a.keyword 'module'
- prefix
Optional prefix of the statement keyword. It is
None
for all built-in statements, and for an extension statement it is the prefix of the module where the extension is defined.
- argument
The statement’s argument. It is the final value of the argument string in which all preliminary processing steps, i.e. substitution of escape sequences and concatenation of parts joined with
+
, have already been performed. For statements that have no argument, such as input, the value of this attribute isNone
.>>> mex5a.argument 'example-5-a'
- superstmt
Parent statement, or
None
if there is no parent.
- substatements
List of substatements.
>>> len(mex5a.substatements) 17
Public Methods
- find1(kw: YangIdentifier, arg: str = None, pref: YangIdentifier = None, required: bool = False)
- Optional[Statement]()
Return the first substatement of the receiver with a matching keyword and, optionally, argument. In order to match, the local part of the keyword has to be kw, and prefix has to be pref. If pref is
None
, only built-in statements match. The last argument, required, controls what happens if a matching substatement is not found: if required isTrue
, thenStatementNotFound
is raised, otherwiseNone
is returned. If arg isNone
, then the arguments of substatements are not taken into account.>>> lfs = mex5a.find1('leaf', 'string-leaf') >>> str(lfs) 'leaf "string-leaf" { ... }' >>> lfs.superstmt.keyword 'module' >>> mex5a.find1('rpc') is None True >>> mex5a.find1('rpc', required=True) Traceback (most recent call last): ... yangson.exceptions.StatementNotFound: `rpc' in `module "example-5-a" { ... }'
- class yangson.statement.ModuleParser(text: str, name: YangIdentifier = None, rev: str = None)
This class is a subclass of
Parser
, and implements a recursive-descent parser for YANG modules. Source text of the YANG module is passed to the constructor in the text argument (see also theParser.input
attribute). The other two arguments, name and rev, are optional and may be used for initializing the instance attributes below.Instance Attributes
- name
Module or submodule name that is expected to be found in the module text.
- rev
Module or submodule revision date that is expected to be found in the module text as the most recent revision.
Public Methods
- parse() Statement
Parse the YANG module text.
Apart from parsing exceptions raised in the methods of the
parser
module, this method may raise the following exceptions:ModuleNameMismatch
– if the module name doesn’t match thename
attributeModuleRevisionMismatch
– if the most recent revision date doesn’t match therev
attribute.
>>> with open('example-5-a.yang') as infile: ... m5atxt = infile.read() >>> str(ModuleParser(m5atxt).parse()) 'module "example-5-a" { ... }' >>> str(ModuleParser(m5atxt, rev='2018-04-01').parse()) Traceback (most recent call last): ... yangson.exceptions.ModuleRevisionMismatch: '2018-10-25', expected '2018-04-01'