BaseConfigLine

class BaseConfigLine(number, text, config, verbosity=3, name='BaseConfigLine')

Bases: object

This class is not meant to be instantiated directly, but only from BaseConfigParser instance.

Parameters:
  • number (int) – Index of line in config
  • text (str) – Text of the config line
  • config (BaseConfigParser) – Reference to the parent BaseConfigParser object
  • verbosity (int, optional) – Logging output level, defaults to 3: Warning
PATTERN_TYPE

alias of re.Pattern

comment_regex = re.compile('^(\\s+)?!.*', re.MULTILINE)
get_children()

Return all children lines (all following lines with larger indent)

Returns:List of child config lines (objects)
Return type:list
get_parent
get_parents
get_type

Return types of config line. Used mostly for filtering purposes.

Currently available values are:

  • parent
  • child
  • interface
  • comment
Returns:List of types
Return type:list
is_child

Check whether this line is a child

Returns:True if line is a child line, False otherwise
Return type:bool
is_interface
is_parent

Check whether this line is a parent

Returns:True if line is a parent line, False otherwise
Return type:bool
re_match(regex, group=None)

Search config line for given regex

Parameters:
  • regex (re.Pattern or str) – Regex to search for
  • group (str or int, optional) – Return only specific (named or numbered) group of given regex. If set to “ALL”, return value will be a dictionary with all named groups of the regex.

Examples

Example:

# Given the following line stored in `line` variable
# " ip address 10.0.0.1 255.255.255"
pattern = r"^ ip address (?P<ip>\S+) (?P<mask>\S+)"

# Basic search
result = line.re_search(regex=pattern)
print(result)
# Returns: " ip address 10.0.0.1 255.255.255"

# Search for specific group
result = line.re_search(regex=pattern, group="ip")
print(result)
# Returns: "10.0.0.1"

# Get all named groups
result = line.re_search(regex=pattern, group="ALL")
print(result)
# Returns: {"ip": "10.0.0.1", "mask": "255.255.255"}
Returns:String that matched given regex, or, if group was provided, returns only specific group.

Returns None if regex did not match.

Return type:str
re_search_children(regex, group=None)

Search all children for given regex.

Parameters:
  • regex (re.Pattern or str) – Regex to search for
  • group (str or int, optional) – Return only specific (named or numbered) group of given regex. If set to “ALL”, return value will be a dictionary with all named groups of the regex.
Returns:

List of all child object which match given regex, or, if group was provided, returns list containing matched grop across all children.

Example:

# Given following config section, interface line stored in `line` variable
config = '''
interface Ethernet0/0
 description Test Interface
 ip address 10.0.0.1 255.255.255.0
 ip address 10.0.1.1 255.255.255.0 secondary
!
'''
pattern = r"^ ip address (?P<ip>\S+) (?P<mask>\S+)"

result = line.re_search_children(regex=pattern)
print(result)
# Returns: [
#   [BaseConfigLine #2 (child): ip address 10.0.0.1 255.255.255.0],
#   [BaseConfigLine #3 (child): ip address 10.0.1.1 255.255.255.0 secondary]
# ]

result = line.re_search_children(regex=pattern, group="ip")
print(result)
# Returns: [
#   "10.0.0.1",
#   "10.0.1.1"
# ]

result = line.re_search_children(regex=pattern, group="ALL")
print(result)
# Returns: [
#   {"ip": "10.0.0.1", "mask": "255.255.255.0"},
#   {"ip": "10.0.1.1", "mask": "255.255.255.0"}
# ]

Return type:

list

return_obj()