hier_config package

Submodules

hier_config.base module

hier_config.child module

class hier_config.child.HConfigChild(parent: Union[HConfig, HConfigChild], text: str)[source]

Bases: hier_config.base.HConfigBase

append_tag(tag: str) → None[source]

Add a tag to self._tags on all leaf nodes

append_tags(tags: Union[str, List[str], Set[str]]) → None[source]

Add tags to self._tags on all leaf nodes

cisco_style_text(style: str = 'without_comments', tag: Optional[str] = None) → str[source]

Return a Cisco style formated line i.e. indentation_level + text ! comments

delete() → None[source]

Delete the current object from its parent

depth() → int[source]

Returns the distance to the root HConfig object i.e. indent level

property is_branch

returns True if there are children or is an instance of HConfig

is_idempotent_command(other_children: Iterable[hier_config.child.HConfigChild]) → bool[source]

Determine if self.text is an idempotent change.

property is_leaf

returns True if there are no children and is not an instance of HConfig

line_inclusion_test(include_tags: Set[str], exclude_tags: Set[str]) → bool[source]

Given the line_tags, include_tags, and exclude_tags, determine if the line should be included

lineage() → Iterator[hier_config.child.HConfigChild][source]

Yields the lineage of parent objects, up to but excluding the root

lineage_test(rule: dict, strip_negation: bool = False) → bool[source]

A generic test against a lineage of HConfigChild objects

property logs
move(new_parent: Union[HConfig, HConfigChild]) → None[source]

move one HConfigChild object to different HConfig parent object

hier1 = HConfig(host=host)
interface1 = hier1.add_child('interface Vlan2')
interface1.add_child('ip address 10.0.0.1 255.255.255.252')

hier2 = Hconfig(host)

interface1.move(hier2)
Parameters

new_parent – HConfigChild object -> type list

Returns

None

negate()hier_config.child.HConfigChild[source]

Negate self.text

property options
overwrite_with(other: HConfigChild, delta: Union[HConfig, HConfigChild], negate: bool = True) → None[source]

Deletes delta.child[self.text], adds a deep copy of self to delta

path() → Iterator[str][source]

Return a list of the text instance variables from self.lineage

remove_tag(tag: str) → None[source]

Remove a tag from self._tags on all leaf nodes

remove_tags(tags: Union[str, List[str], Set[str]]) → None[source]

Remove tags from self._tags on all leaf nodes

property root

returns the HConfig object at the base of the tree

sectional_overwrite_check() → bool[source]

Determines if self.text matches a sectional overwrite rule

sectional_overwrite_no_negate_check() → bool[source]

Check self’s text to see if negation should be handled by overwriting the section without first negating it

property tags

Recursive access to tags on all leaf nodes

property text

hier_config.host module

hier_config.root module

class hier_config.root.HConfig(host: Host)[source]

Bases: hier_config.base.HConfigBase

A class for representing and comparing Cisco configurations in a hierarchical tree data structure.

Example usage:

# Setup basic environment

from hier_config import HConfig, Host
import yaml

options = yaml.safe_load(open('./tests/fixtures/options_ios.yml'))
host = Host('example.rtr', 'ios', options)

# Build HConfig object for the Running Config

running_config_hier = HConfig(host=host)
running_config_hier.load_from_file('./tests/fixtures/running_config.conf')

# Build Hierarchical Configuration object for the Generated Config

generated_config_hier = HConfig(host=host)
generated_config_hier.load_from_file('./tests/fixtures/generated_config.conf')

# Build Hierarchical Configuration object for the Remediation Config

remediation_config_hier = running_config_hier.config_to_get_to(generated_config_hier)

for line in remediation_config_hier.all_children():
    print(line.cisco_style_text())

See:

./tests/fixtures/tags_ios.yml and ./tests/fixtures/options_ios.yml

for test examples of options and tags.

add_ancestor_copy_of(parent_to_add: hier_config.child.HConfigChild) → Union[hier_config.root.HConfig, hier_config.child.HConfigChild][source]

Add a copy of the ancestry of parent_to_add to self and return the deepest child which is equivalent to parent_to_add

add_sectional_exiting() → None[source]

Adds the sectional exiting text as a child

add_tags(tag_rules: list, strip_negation: bool = False) → None[source]

Handler for tagging sections of Hierarchical Configuration data structure for inclusion and exclusion.

config_to_get_to(target: hier_config.root.HConfig, delta: Optional[hier_config.root.HConfig] = None)hier_config.root.HConfig[source]

Figures out what commands need to be executed to transition from self to target. self is the source data structure(i.e. the running_config), target is the destination(i.e. generated_config)

depth() → int[source]

Returns the distance to the root HConfig object i.e. indent level

difference(target: hier_config.root.HConfig)hier_config.root.HConfig[source]

Creates a new HConfig object with the config from self that is not in target

Example usage: whats in the config.lines v.s. in running config i.e. did all my configuration changes get written to the running config

Parameters

target – HConfig - The configuration to check against

Returns

HConfig - missing config additions

dump(lineage_rules: Optional[List[dict]] = None) → List[dict][source]

Dump a list of loaded HConfig data

property is_branch

returns True if there are children or is an instance of HConfig

property is_leaf

returns True if there are no children and is not an instance of HConfig

lineage() → Iterator[hier_config.child.HConfigChild][source]

Yields the lineage of parent objects, up to but excluding the root

load_from_dump(dump: List[dict]) → None[source]

Load an HConfig dump

load_from_file(file_path: Union[str, pathlib.Path]) → None[source]

Load configuration text from a file

load_from_string(config_text: str) → None[source]

Create Hierarchical Configuration nested objects from text

property logs
merge(other: hier_config.root.HConfig) → None[source]

Merges two HConfig objects

property options
property root

returns the HConfig object at the base of the tree

set_order_weight() → None[source]

Sets self.order integer on all children

property tags

Recursive access to tags on all leaf nodes

with_tags(tags: Set[str])hier_config.root.HConfig[source]

Returns a new instance containing only sub-objects with one of the tags in tags

hier_config.text_match module

hier_config.text_match.anything(text: str, expression: str) → bool[source]

Always returns True

hier_config.text_match.contains(text: str, expression: str) → bool[source]

Text contains test

hier_config.text_match.dict_call(test: str, text: str, expression: str) → bool[source]

Allows test methods to be called easily from variables

hier_config.text_match.endswith(text: str, expression: Union[str, Tuple[str, ]]) → bool[source]

Text ends with test

hier_config.text_match.equals(text: str, expression: Union[str, Set[str]]) → bool[source]

Text equivalence test

hier_config.text_match.nothing(text: str, expression: str) → bool[source]

Always returns False

Test regex match. This method is comparatively very slow and should be avoided where possible.

hier_config.text_match.startswith(text: str, expression: Union[str, Tuple[str, ]]) → bool[source]

Text starts with test

Module contents