Skip to content

Commit

Permalink
refactor: ♻️ update function names to adhere to standard naming
Browse files Browse the repository at this point in the history
  • Loading branch information
bile0026 committed Mar 26, 2024
1 parent 261da2d commit c128fe4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/user/include_jinja_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
| feature_compliance | netutils.config.compliance.feature_compliance |
| find_unordered_cfg_lines | netutils.config.compliance.find_unordered_cfg_lines |
| section_config | netutils.config.compliance.section_config |
| palo_alto_clean_newlines | netutils.config.conversion.palo_alto_clean_newlines |
| paloalto_panos_brace_to_set | netutils.config.conversion.paloalto_panos_brace_to_set |
| paloalto_panos_clean_newlines | netutils.config.conversion.paloalto_panos_clean_newlines |
| fqdn_to_ip | netutils.dns.fqdn_to_ip |
| is_fqdn_resolvable | netutils.dns.is_fqdn_resolvable |
| hash_data | netutils.hash.hash_data |
Expand Down
18 changes: 9 additions & 9 deletions netutils/config/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}


def palo_alto_clean_newlines(cfg: str) -> str:
def paloalto_panos_clean_newlines(cfg: str) -> str:
r"""Takes in the configuration and replaces any inappropriate newline characters with a space.
Args:
Expand All @@ -20,7 +20,7 @@ def palo_alto_clean_newlines(cfg: str) -> str:
str: Cleaned configuration as a string
Examples:
>>> from netutils.config.conversion import palo_alto_clean_newlines
>>> from netutils.config.conversion import paloalto_panos_clean_newlines
>>> config = '''
... config {
... syslog {
Expand Down Expand Up @@ -64,7 +64,7 @@ def palo_alto_clean_newlines(cfg: str) -> str:
... }
... }
... }'''
>>> palo_alto_clean_newlines(cfg=config) == \
>>> paloalto_panos_clean_newlines(cfg=config) == \
... '''
... config {
... syslog {
Expand Down Expand Up @@ -108,14 +108,14 @@ def palo_alto_clean_newlines(cfg: str) -> str:
... }'''
True
"""
palo_alto_no_newline_cleanup_match = ["private-key", "public-key", "login-banner"]
palo_alto_newline_regex = re.compile(
paloalto_panos_no_newline_cleanup_match = ["private-key", "public-key", "login-banner"]
paloalto_panos_newline_regex = re.compile(
r"\w+?-?\w+\s\"(?:[^\"\\]|\\.)*\n(?:[^\"\\]|\\.)*\";$", re.MULTILINE | re.DOTALL
)

newlines_cleaned_cfg = palo_alto_newline_regex.sub(
newlines_cleaned_cfg = paloalto_panos_newline_regex.sub(
lambda match: match.group().replace("\n", " ")
if not any(substring in match.group() for substring in palo_alto_no_newline_cleanup_match)
if not any(substring in match.group() for substring in paloalto_panos_no_newline_cleanup_match)
else match.group(),
cfg,
)
Expand Down Expand Up @@ -175,9 +175,9 @@ def paloalto_panos_brace_to_set(cfg: str, cfg_type: str = "file") -> str:
raise ValueError("The variable `cfg_type` must be either `file` or `string`.")
if cfg_type == "file":
cfg_raw = _open_file_config(cfg)
cfg_list = palo_alto_clean_newlines(cfg=cfg_raw).splitlines()
cfg_list = paloalto_panos_clean_newlines(cfg=cfg_raw).splitlines()
else:
cfg_raw = palo_alto_clean_newlines(cfg=cfg)
cfg_raw = paloalto_panos_clean_newlines(cfg=cfg)
cfg_list = cfg_raw.splitlines()

for i, line in enumerate(cfg_list):
Expand Down
2 changes: 1 addition & 1 deletion netutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"uptime_seconds_to_string": "time.uptime_seconds_to_string",
"uptime_string_to_seconds": "time.uptime_string_to_seconds",
"get_napalm_getters": "lib_helpers.get_napalm_getters",
"palo_alto_clean_newlines": "config.conversion.palo_alto_clean_newlines",
"paloalto_panos_clean_newlines": "config.conversion.paloalto_panos_clean_newlines",
"paloalto_panos_brace_to_set": "config.conversion.paloalto_panos_brace_to_set",
"compare_version_loose": "os_version.compare_version_loose",
"compare_version_strict": "os_version.compare_version_strict",
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_sanitize.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest
from netutils.config import compliance
from netutils.config.conversion import palo_alto_clean_newlines
from netutils.config.conversion import paloalto_panos_clean_newlines

MOCK_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "mock", "config", "sanitize")

Expand All @@ -23,6 +23,6 @@ def test_panos_newline_character(_file, get_text_data):
truncate_file = os.path.join(MOCK_DIR, _file[0][: -len(TXT_FILE)])

sent_cfg = get_text_data(os.path.join(MOCK_DIR, _file[0]))
sanitized_cfg = palo_alto_clean_newlines(cfg=sent_cfg)
sanitized_cfg = paloalto_panos_clean_newlines(cfg=sent_cfg)
received_data = get_text_data(truncate_file + "_sanitized.txt")
assert sanitized_cfg == received_data

0 comments on commit c128fe4

Please sign in to comment.