Skip to content

Commit ad338e8

Browse files
committed
commit save.
1 parent e9d5f2b commit ad338e8

File tree

9 files changed

+35
-70
lines changed

9 files changed

+35
-70
lines changed

netcompare/check_type.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ def init(*args):
1616
check_type = args[0]
1717
if check_type == "exact_match":
1818
return ExactMatchType(*args)
19-
elif check_type == "tolerance":
19+
if check_type == "tolerance":
2020
return ToleranceType(*args)
21-
else:
22-
raise NotImplementedError
21+
raise NotImplementedError
2322

2423
@staticmethod
2524
def extract_value_from_json_path(
26-
value: Mapping, path: Mapping, exclude: List = list()
25+
value: Mapping, path: Mapping, exclude: List = None
2726
) -> Union[Mapping, List, int, str, bool]:
2827
"""Return the value contained into a Mapping for a defined path."""
2928
return extract_values_from_output(value, path, exclude)

netcompare/evaluator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Diff evaluator."""
1+
"Diff evaluator."
22
import re
33
import sys
44
from collections import defaultdict
@@ -109,7 +109,7 @@ def fix_deepdiff_key_names(obj: Mapping) -> Mapping:
109109
# root['Ethernet1'][0]['port']
110110
pattern = r"'([A-Za-z0-9_\./\\-]*)'"
111111

112-
result = dict()
112+
result = {}
113113
for key, value in obj.items():
114114
key_parts = re.findall(pattern, key)
115115
partial_res = group_value(key_parts, value)

netcompare/runner.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#!/ur/bin/env python3
1+
"Library wrapper for output parsing."
22
import re
3-
import jmespath
43
from typing import Mapping, List, Union
4+
import jmespath
55
from .utils.jmspath_parsers import jmspath_value_parser, jmspath_refkey_parser
66
from .utils.filter_parsers import exclude_filter
77
from .utils.refkey import keys_cleaner, keys_values_zipper, associate_key_of_my_value
@@ -48,11 +48,9 @@ def extract_values_from_output(value: Mapping, path: Mapping, exclude: List) ->
4848
for item in element:
4949
if isinstance(item, dict):
5050
raise TypeError(
51-
'Must be list of lists i.e. [["Idle", 75759616], ["Idle", 75759620]]. You have {}\'.'.format(
52-
wanted_value
53-
)
51+
f'Must be list of lists i.e. [["Idle", 75759616], ["Idle", 75759620]]. You have {wanted_value}\'.'
5452
)
55-
elif isinstance(item, list):
53+
if isinstance(item, list):
5654
wanted_value = flatten_list(wanted_value)
5755
break
5856

@@ -64,5 +62,5 @@ def extract_values_from_output(value: Mapping, path: Mapping, exclude: List) ->
6462
wanted_reference_keys = jmespath.search(jmspath_refkey_parser(path), value)
6563
list_of_reference_keys = keys_cleaner(wanted_reference_keys)
6664
return keys_values_zipper(list_of_reference_keys, paired_key_value)
67-
else:
68-
return paired_key_value
65+
66+
return paired_key_value

netcompare/utils/filter_parsers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"Filtering parsing."
12
from typing import Mapping, List
23

34

@@ -36,5 +37,5 @@ def exclude_filter(data: Mapping, exclude: List):
3637

3738
elif isinstance(data, list):
3839
for element in data:
39-
if isinstance(element, dict) or isinstance(element, list):
40+
if isinstance(element, (dict, list)):
4041
exclude_filter(element, exclude)

netcompare/utils/flatten.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"Flatten multi-nested list."
12
from typing import List, Generator
23

34

netcompare/utils/jmspath_parsers.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"JMSPath expresion parsers."
12
import re
23

34

@@ -19,7 +20,8 @@ def jmspath_value_parser(path: str):
1920
if regex_normalized_value:
2021
normalized_value = regex_match_value.group().split("$")[1]
2122
return path.replace(regex_normalized_value.group(), normalized_value)
22-
else: return path
23+
24+
return path
2325

2426

2527
def jmspath_refkey_parser(path: str):
@@ -33,13 +35,13 @@ def jmspath_refkey_parser(path: str):
3335
"""
3436
splitted_jmspath = path.split(".")
3537

36-
for n, i in enumerate(splitted_jmspath):
37-
regex_match_anchor = re.search(r"\$.*\$", i)
38+
for number, element in enumerate(splitted_jmspath):
39+
regex_match_anchor = re.search(r"\$.*\$", element)
3840

3941
if regex_match_anchor:
40-
splitted_jmspath[n] = regex_match_anchor.group().replace("$", "")
42+
splitted_jmspath[number] = regex_match_anchor.group().replace("$", "")
4143

42-
if regex_match_anchor and not i.startswith("[") and not i.endswith("]"):
43-
splitted_jmspath = splitted_jmspath[: n + 1]
44+
if regex_match_anchor and not element.startswith("[") and not element.endswith("]"):
45+
splitted_jmspath = splitted_jmspath[: number + 1]
4446

4547
return ".".join(splitted_jmspath)

netcompare/utils/refkey.py

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,27 @@
44
def keys_cleaner(wanted_reference_keys: Mapping) -> List[Mapping]:
55
"""
66
Get every required reference key from output.
7-
8-
Args:
9-
wanted_reference_keys: {'10.1.0.0': {'address_family': {'ipv4': ...
10-
11-
Return:
12-
['10.1.0.0', '10.2.0.0', '10.64.207.255', '7.7.7.7']
13-
14-
Example:
15-
>>> from runner import keys_cleaner
16-
>>> wanted_reference_keys = {'10.1.0.0': {'address_family': 'ipv4'}}
17-
>>> keys_cleaner(wanted_reference_keys)
18-
['10.1.0.0', '10.2.0.0', '10.64.207.255', '7.7.7.7']
197
"""
208
if isinstance(wanted_reference_keys, list):
219
return wanted_reference_keys
2210

2311
elif isinstance(wanted_reference_keys, dict):
2412
my_keys_list = list()
2513

26-
if isinstance(wanted_reference_keys, dict):
14+
if isinstance(wanted_reference_keys, dict):
2715
for key in wanted_reference_keys.keys():
2816
my_keys_list.append(key)
2917
else:
30-
raise TypeError(f'Must be a dictionary. You have type:{type(wanted_reference_keys)} output:{wanted_reference_keys}\'.')
31-
18+
raise TypeError(
19+
f"Must be a dictionary. You have type:{type(wanted_reference_keys)} output:{wanted_reference_keys}'."
20+
)
21+
3222
return my_keys_list
3323

3424

3525
def keys_values_zipper(list_of_reference_keys: List, wanted_value_with_key: List) -> List:
3626
"""
3727
Build dictionary zipping keys with relative values.
38-
39-
Args:
40-
list_of_reference_keys: ['10.1.0.0', '10.2.0.0', '10.64.207.255', '7.7.7.7']
41-
wanted_value_with_key: [{'is_enabled': True, 'is_up': False}, ...
42-
43-
Return:
44-
[{'10.1.0.0': {'is_enabled': True, 'is_up': False}}, , ...
45-
46-
Example:
47-
>>> from runner import keys_values_zipper
48-
>>> list_of_reference_keys = ['10.1.0.0']
49-
>>> wanted_value_with_key = [{'is_enabled': True, 'is_up': False}]
50-
>>> keys_values_zipper(list_of_reference_keys, wanted_value_with_key)
51-
[{'10.1.0.0': {'is_enabled': True, 'is_up': False}}]
5228
"""
5329
final_result = list()
5430

@@ -64,19 +40,6 @@ def keys_values_zipper(list_of_reference_keys: List, wanted_value_with_key: List
6440
def associate_key_of_my_value(paths: str, wanted_value: List) -> List:
6541
"""
6642
Associate each key defined in path to every value found in output.
67-
68-
Args:
69-
paths: {"path": "global.peers.*.[is_enabled,is_up]"}
70-
wanted_value: [[True, False], [True, False], [True, False], [True, False]]
71-
72-
Return:
73-
[{'is_enabled': True, 'is_up': False}, ...
74-
75-
Example:
76-
>>> from runner import associate_key_of_my_value
77-
>>> path = {"path": "global.peers.*.[is_enabled,is_up]"}
78-
>>> wanted_value = [[True, False], [True, False], [True, False], [True, False]]
79-
{'is_enabled': True, 'is_up': False}, {'is_enabled': True, 'is_up': False}, ...
8043
"""
8144

8245
# global.peers.*.[is_enabled,is_up] / result.[*].state

tasks.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,42 +109,42 @@ def pytest(context, local=INVOKE_LOCAL):
109109
@task(help={"local": "Run locally or within the Docker container"})
110110
def black(context, path=".", local=INVOKE_LOCAL):
111111
"""Run black to check that Python files adherence to black standards."""
112-
exec_cmd = "black {path}".format(path=path)
112+
exec_cmd = f"black {path}"
113113
run_cmd(context, exec_cmd, local)
114114

115115

116116
@task(help={"local": "Run locally or within the Docker container"})
117117
def flake8(context, path=".", local=INVOKE_LOCAL):
118118
"""Run flake8 code analysis."""
119-
exec_cmd = "flake8 {path}".format(path=path)
119+
exec_cmd = f"flake8 {path}"
120120
run_cmd(context, exec_cmd, local)
121121

122122

123123
@task(help={"local": "Run locally or within the Docker container"})
124124
def pylint(context, path=".", local=INVOKE_LOCAL):
125125
"""Run pylint code analysis."""
126-
exec_cmd = 'find {path} -name "*.py" | xargs pylint'.format(path=path)
126+
exec_cmd = f'find {path} -name "*.py" | xargs pylint'
127127
run_cmd(context, exec_cmd, local)
128128

129129

130130
@task(help={"local": "Run locally or within the Docker container"})
131131
def yamllint(context, path=".", local=INVOKE_LOCAL):
132132
"""Run yamllint to validate formatting adheres to NTC defined YAML standards."""
133-
exec_cmd = "yamllint {path}".format(path=path)
133+
exec_cmd = f"yamllint {path}"
134134
run_cmd(context, exec_cmd, local)
135135

136136

137137
@task(help={"local": "Run locally or within the Docker container"})
138138
def pydocstyle(context, path=".", local=INVOKE_LOCAL):
139139
"""Run pydocstyle to validate docstring formatting adheres to NTC defined standards."""
140-
exec_cmd = "pydocstyle {path}".format(path=path)
140+
exec_cmd = f"pydocstyle {path}"
141141
run_cmd(context, exec_cmd, local)
142142

143143

144144
@task(help={"local": "Run locally or within the Docker container"})
145145
def bandit(context, path=".", local=INVOKE_LOCAL):
146146
"""Run bandit to validate basic static code security analysis."""
147-
exec_cmd = "bandit --recursive ./{path} --configfile .bandit.yml".format(path=path)
147+
exec_cmd = f"bandit --recursive ./{path} --configfile .bandit.yml"
148148
run_cmd(context, exec_cmd, local)
149149

150150

tests/utility.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99

1010
def load_json_file(folder, filename):
11+
"Load mock data from json file."
1112
filepath = os.path.join(dirname, "mock", folder, filename)
12-
with open(filepath) as filehandle:
13+
with open(filepath, encoding="utf-8") as filehandle:
1314
return json.load(filehandle)

0 commit comments

Comments
 (0)