Skip to main content

Nokia SROS, Cisco IOS style (parent/child with space indent) config edit (add/delete/replace) with regex

Project description

Edit4Config

Nokia SROS, Cisco IOS style (parent/child with space indentation) config edit module with add, delete, replace and search function with regex supported. Every line in config text converted to path (parent) and value (line) based on space indent. After this converting process (Config with Parents or CwP) every config line is unique with own parents and editing can be done easily.


Requirements

Python >= 3.9

For Windows, select the Add Python 3.x to PATH checkbox during installation.


Installation

pip install edit4config

CwP (Config with Parents) Example

Config Text:

configure
    ...
    card 1
        card-type iom-e
        ...
        mda 1
            mda-type me10-10gb-sfp+
            ...
            no shutdown
            ...
        mda 2
            ...
        ...
    ...
...

CwP List:

[
...
['configure', '    card 1'],
['configure,card 1', '        card-type iom-e'],
['configure,card 1', '        mda 1'],
['configure,card 1,mda 1', '            mda-type me10-10gb-sfp+'],
...
['configure,card 1,mda 1', '            no shutdown'],
...
['configure,card 1', '        mda 2'],
...
]

CwP Text:

...
configure,card 1
configure,card 1,card-type iom-e
configure,card 1,mda 1
configure,card 1,mda 1,mda-type me10-10gb-sfp+
...
configure,card 1,mda 1,no shutdown
...
configure,card 1,mda 2
...

Usage

EditConfig Simple Usage

After CwP converting EditConfig add, delete, replace, search and other methods can be used as below.

Add-Delete-Replace methods supported regex, multiple match.

Add-Delete methods supported serial CwP Text lines with newline.

# import EditConfig module
from edit4config import EditConfig

# read config file and get config_text
with open('CONFIG_FILE.txt') as file:
    config_text = file.read()

# define EditConfig object with options e.g. comments, step_space
# comments for Nokia is ('#', 'echo') and for Cisco is ('!')
# step_space for Nokia is 4 and for Cisco is 1
device_cwp = EditConfig(config_text, 4, ('#', 'echo'))

# add "sync-e" before "no shutdown" under configure,card 1,mda 1
device_cwp.add_before_lines(
                            'configure,card 1,mda 1,sync-e',
                            'configure,card 1,mda 1,no shutdown'
                        )

# delete "no shutdown" under configure,card 1,mda 1
device_cwp.delete_serial_lines('configure,card 1,mda 1,no shutdown')

# replace "no shutdown" with "shutdown" under configure,card 1,mda 1
device_cwp.replace_line(
                        'configure,card 1,mda 1,no shutdown',
                        'configure,card 1,mda 1,shutdown'
                    )

# delete "no shutdown" for all card and all mda with regex
device_cwp.delete_serial_lines(
                            'configure,card \d+,mda \d+,no shutdown',
                            regex_match=True, 
                            multiple_match=True
                            )

# after editing done, convert device_cwp object to text file
new_config_text = device_cwp.cwp_to_text()

with open('CONFIG_FILE_NEW.txt', 'w') as file:
    file.write(new_config_text)

Another example below for getting CwP and making custom jobs.

# import EditConfig module
from edit4config import EditConfig

# read config file and get config_text
with open('CONFIG_FILE.txt') as file:
    config_text = file.read()

# define EditConfig object with options e.g. comments, step_space
# comments for Nokia is ('#', 'echo') and for Cisco is ('!')
# step_space for Nokia is 4 and for Cisco is 1
device_cwp = EditConfig(config_text, 4, ('#', 'echo'))

# get full cwp-list from EditConfig object with <cwp> variable
device_cwp_list = device_cwp.cwp

# get ntp config from cwp-list and print
for line_path, line_value in device_cwp.cwp:
    if line_path.startswith('configure,system,time,ntp'):
        print(line_value.strip())
# import EditConfig module
from edit4config import EditConfig

# read config file and get config_text
with open('CONFIG_FILE.txt') as file:
    config_text = file.read()

# define EditConfig object for Cisco
cisco_ec = EditConfig(config_text, 1, ('!'), sep=',,,')

# cwp search for all interface 
interface_cwp_search = cisco_ec.cwp_search(r"^$", fr"interface ")
print(interface_cwp_search)
# print result : [['', 'interface Bundle-Ether1'], ...]

# get only l2transport interface name with regex group capture
interface_l2transport_name_list = cisco_ec.cwp_search_capture("interface (.+$)", "l2transport")
print(interface_l2transport_name_list)
# print result: [['GigabitEthernet1/1/1/1'],...]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

edit4config-2025.9.14.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

edit4config-2025.9.14-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file edit4config-2025.9.14.tar.gz.

File metadata

  • Download URL: edit4config-2025.9.14.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for edit4config-2025.9.14.tar.gz
Algorithm Hash digest
SHA256 d4dc1ad7d921b7ee3424611cc3c05977463867d046cecd02e843926243604a0f
MD5 49ce5b401f18310929688b2bb98b53d6
BLAKE2b-256 b4b07a696ba1663ed8e2f07d31c7f5f5d7d6634608781c505762ce08d9890ee8

See more details on using hashes here.

File details

Details for the file edit4config-2025.9.14-py3-none-any.whl.

File metadata

File hashes

Hashes for edit4config-2025.9.14-py3-none-any.whl
Algorithm Hash digest
SHA256 14de69c3bd8b76cc489f400506f59ae6ab24d6d7573b06e0a86cdc8532596922
MD5 239d5ffd0510a131d5a0c022d2e7e219
BLAKE2b-256 ae35cb9b7418040e70083b6846f912ea7fc2e682cb93c842898ff706abbd4542

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page