Metaparser module¶
metaparser - the infrastructure class to define and regulate parser object execution.
-
class
genie.metaparser._metaparser.
MetaParser
(device, *, context='cli', **kwargs)¶ Bases:
object
Base parser class for all Genie parser classes
All parser classes will inherit from MetaParser class, for example:
ShowVersion
, etc.- Parameters
device (
Device
) – device which the parser function will be applied tocontext (
str
) – a keyword only arg acting as a selector for user to choose which parsing mechanism will be used to current parsing task. choices of: ‘cl’, ‘xml’, ‘yang’, default is ‘cli’.kwargs (
dict
) – gives the user ability to assign extra attributes while creating the parser object.
- Returns
a
metaparser
object
Examples
>>> from xbu_shared.parser.nxos.show_version import ShowVersion >>> p = ShowVersion()
<parser.nxos.show_version.ShowVersion object at 0xf6e0ec6c>
Class variables:
- schema (
dict
): defines the common data structure among all types of device output (cli, xml, yang) current metaparser supports.
- key_traceable(‘bool’): enable the parser output key usage tracing
functionality, default value is ‘False’
CONTEXT_LIST (
tuple
): static variable defines valid contextsDEFAULT_CONTEXT (
str
): constant variable with value ‘cli’class initialization - set attributes for the parser object according to user’s input.
parsed_output (
dicts
): holds the raw output before the filtering-
CONTEXT_LIST
= ('cli', 'xml', 'yang', 'rest')¶
-
DEFAULT_CONTEXT
= 'cli'¶
-
call_parser
(context)¶ This function returns the object context and raise an attribute error if not found.
-
command
= None¶
-
key_traceable
= False¶
-
parse
(*, selected_keys=None, context=None, ignore_update=True, warn_unsupported_keys=None, **kwargs)¶ public method for users to call to execute a parser
API runs correct parsing methods according to the ‘context’ user specific in the constructor.
Procedure of parse function:
1.Select specific parsing mechanism (cli(), xml(), yang()) based on ‘context’ input. Each parsing mechanism defined in subclass parser object should include 3 steps: - Get the output from the device - Run existing parser or self implemented parser with the output - Transform the output into ‘schema’ compatible dictionary and return. Detail steps to implement parsing mechanism please read template.py within the package.
2.Schema checking - metaparser will do schema checking to make sure the parsing mechanism returns correct data structure.
3.Apply user defined filter on output, only selected key-value pairs will be returned as the original format from the output.
- Parameters
selected_keys (
list
) –keyword-only arg, a filtering mechanism which allows user to select desired keys (nested keys) from the raw parser output and reform a new dict containing only those selected key-value pairs.
selected_keys can be a list of list or a list of tuple which contains flattened nested keys from the output dictionary. Python regular expression pattern can be used to define matching key format.
- For example: selected_keys =
[[‘a’, ‘b’], [r’.*’, ‘c’], [r’^d’, r’.*’]]
Here ‘r’ in front of the key string indicates the ‘raw string literal’ used as the Python regexp pattern.
warn_unsupported_keys (bool) – Log warning instead of raising an exception when unsupported keys are found.
kwarg (dict) – gives the user ability to pass extra parameters.
- Returns
a nested
dict
obj
Example
>>> parser = ShowVersion(device=uut, context='xml')
get the entire output
>>> output = parser.parse()
get the output only contains specific keys selected by users
>>> selected_keys = [['hardware', 'bootflash'], [r'.*', 'kickstart'], ['reason'], [r'^kernel', r'.*']]
>>> output = parser.parse(selected_keys = selected_keys)
>>> {'hardware': {'bootflash': '2048256'}, 'kernel_uptime': {'days': '113', 'hours': '2', 'minutes': '42', 'seconds': '5'}, 'reason': 'Unknown', 'software': {'kickstart': 'version 6.2(6)', 'kickstart_compile_time': '12/5/2013', 'kickstart_image_file': 'bootflash:///kickstart.6.2.bin'}}
-
parse_selected_keys
(selected_keys, output, **kwargs)¶ This function retrieves the corresponding selected_keys portion of the parsed output.
-
schema
= None¶
-
tracer
= {}¶
-
class
genie.metaparser._metaparser.
classproperty
(fget=None, fset=None, fdel=None, doc=None)¶ Bases:
property
decorator class to access the class property
-
genie.metaparser._metaparser.
enable_key_usage_trace
(section)¶ method to enable the global traceability of parser output key usage
This module method designed to be used as a pyats pre-processor
-
genie.metaparser._metaparser.
log_key_usage_trace
(section)¶ method to log overall parser output key usage
This module method designed to be used as a pyats post-processor
-
genie.metaparser._metaparser.
tclparser
(pkgrequire)¶