BaseConfigParser

Quick Start

from ccutils.ccparser import BaseConfigParser
import pathlib

# Optinal - Create pathlib object pointing to your config file
path_to_file = pathlib.Path("/path/to/config_file.txt")

config = BaseConfigParser(config=path_to_file)

# Print number of config lines
print(len(config.lines))
class BaseConfigParser(config=None, verbosity=4, name='BaseConfigParser', **kwargs)

Bases: object

Base Configuration Parser object used for loading configuration files.

Base class for parsing Cisco-like configs

Parameters:
  • config (pathlib.Path or str or list) – Config file in a form of pathlib.Path, or string containing the entire config or list of lines of the config file
  • verbosity (int, optional) – Determines the verbosity of logging output, defaults to 4: Info
lines

Contains list of all config lines stored as objects (see ccutils.ccparser.BaseConfigLine)

Type:list
config_lines_str

Contains list of all config lines stored as strings

Type:list

Examples

Possible config inputs:

# Using pathlib
config_file = pathlib.Path("/path/to/config_file.txt")
config = BaseConfigParser(config=config_file)

# Using string
config_string = '''
hostname RouterA
!
interface Ethernet0/0
 description Test Interface
 ip address 10.0.0.1 255.255.255.0
!
end
'''
config = BaseConfigParser(config=config_string)

# Using list
config_list = [
"hostname RouterA",
    "!",
    "interface Ethernet0/0",
    " description Test Interface",
    " ip address 10.0.0.1 255.255.255.0",
    "!",
    "end"
]
config = BaseConfigParser(config=config_list)
INTERFACE_LINE_CLASS

alias of ccutils.ccparser.BaseInterfaceLine.BaseInterfaceLine

PATTERN_TYPE

alias of re.Pattern

_check_path(filepath)
_compile_regex(regex, flags=<RegexFlag.MULTILINE: 8>)

Helper function for compiling re patterns from string.

Parameters:
  • regex (str) – Regex string
  • flags – Flags for regex pattern, default is re.MULTILINE
Returns:

_create_cfg_line_objects()

Function for generating self.lines.

_device_tracking_attach_policy_regex = re.compile('^ device-tracking attach-policy (?P<policy>\\S+)')
_get_clean_config(first_line_regex='^version \\d+\\.\\d+', last_line_regex='^end')
_get_indent(line)
_vlan_configuration_regex = re.compile('^vlan configuration (?P<vlan_range>[\\d\\-,]+)', re.MULTILINE)
cdp
config_lines_obj

Kept for backwards compatibility, will be removed in future versions.

Returns:BaseConfigParser.lines
Return type:list
domain_name
find_objects(regex, flags=<RegexFlag.MULTILINE: 8>)

Function for filtering Config Lines Objects based on given regex.

Parameters:
  • regex (re.Pattern or str) – Regex based on which the search is done
  • flags (int, optional) – Set custom flags for regex, defaults to re.MULTILINE

Examples

Example:

# Initialize the object
config = BaseConfigParser(config="/path/to/config_file.cfg")

# Define regex for matching config lines
interface_regex = r"^ interface"

# Apply the filter
interface_lines = config.find_objects(regex=interface_regex)

# Returns subset of ``self.lines`` which match specified regex
fix_indents()

Function for fixing the indentation level of config lines.

Returns:
get_section_by_parents(parents)
hostname
interface_lines
lines = None

This is a URI.

match_to_dict(line, patterns)
Parameters:
  • line – Instance of BaseConfigLine object
  • patterns – List of compiled re patterns
  • minimal_result – Bool, if True, omits keys with value None
Returns:

Dictionary containing named groups across all provided patterns

Return type:

dict

name_servers
parse()

Entry function which triggers the parsing process. Called automatically when instantiating the object.

Returns:None
property_autoparse(candidate_pattern, patterns)

Function for searching multiple patterns across all occurrences of lines that matched candidate_pattern :param candidate_pattern: :param patterns:

Returns:

section_property_autoparse(parent, patterns, return_with_line=False)
static_routes
vlan_groups
vlans
vrfs