10
10
SCW_DIR = 'remediation_training'
11
11
THIRD_PARTY_MAPPING_DIR = 'third-party-mappings'
12
12
13
+
13
14
def get_json (filename ):
14
15
with open (filename ) as f :
15
16
return json .loads (f .read ())
16
17
18
+
17
19
def all_versions (filename ):
18
20
"""
19
- Find, open and parse all tagged versions of a json file, including the current version
21
+ Find, open and parse all tagged versions of a json file,
22
+ including the current version
20
23
21
24
:param filename: The filename to find
22
25
:return: a dictionary of all the versions, in the form
@@ -41,10 +44,12 @@ def id_valid(vrt, id_list):
41
44
Check if a vrt id is valid
42
45
43
46
:param vrt: The vrt object
44
- :param id_list: The vrt id, split into components, eg ['category', 'subcategory', 'variant']
47
+ :param id_list: The vrt id, split into components,
48
+ eg ['category', 'subcategory', 'variant']
45
49
:return: True/False
46
50
"""
47
- # this is not particularly efficient, but it's more readable than other options so until we need to care...
51
+ # this is not particularly efficient, but it's more readable than other
52
+ # options so until we need to care...
48
53
return id_list in all_id_lists (vrt )
49
54
50
55
@@ -53,7 +58,8 @@ def has_mapping(mapping, id_list, key):
53
58
Check if a vrt id has a mapping
54
59
55
60
:param mapping: The mapping object, keyed by id
56
- :param id_list: The vrt id, split into components, eg ['category', 'subcategory', 'variant']
61
+ :param id_list: The vrt id, split into components,
62
+ eg ['category', 'subcategory', 'variant']
57
63
:param key: The mapping key to look for, eg 'cvss_v3'
58
64
:return: True/False
59
65
"""
@@ -72,9 +78,16 @@ def key_by_id(mapping):
72
78
Converts arrays to hashes keyed by the id attribute for easier lookup. So
73
79
[{'id': 'one', 'foo': 'bar'}, {'id': 'two', 'foo': 'baz'}]
74
80
becomes
75
- {'one': {'id': 'one', 'foo': 'bar'}, 'two': {'id': 'two', 'foo': 'baz'}}
81
+ {
82
+ 'one': {'id': 'one', 'foo': 'bar'},
83
+ 'two': {'id': 'two', 'foo': 'baz'}
84
+ }
76
85
"""
77
- if isinstance (mapping , list ) and isinstance (mapping [0 ], dict ) and 'id' in mapping [0 ]:
86
+ if isinstance (
87
+ mapping , list
88
+ ) and isinstance (
89
+ mapping [0 ], dict
90
+ ) and 'id' in mapping [0 ]:
78
91
return {x ['id' ]: key_by_id (x ) for x in mapping }
79
92
elif isinstance (mapping , dict ):
80
93
return {k : key_by_id (v ) for k , v in mapping .items ()}
@@ -84,10 +97,12 @@ def key_by_id(mapping):
84
97
85
98
def all_id_lists (vrt , include_internal = True ):
86
99
"""
87
- Get all valid vrt ids for a given vrt object, including internal nodes by default
100
+ Get all valid vrt ids for a given vrt object, including internal nodes
101
+ by default
88
102
89
103
:param vrt: The vrt object
90
- :param include_internal: Whether to include internal nodes or only leaf nodes
104
+ :param include_internal: Whether to include internal nodes or only
105
+ leaf nodes
91
106
:return: ids in the form
92
107
[
93
108
['category'],
@@ -98,7 +113,10 @@ def all_id_lists(vrt, include_internal=True):
98
113
"""
99
114
def _all_id_lists (sub_vrt , prefix ):
100
115
if isinstance (sub_vrt , list ):
101
- return [vrt_id for entry in sub_vrt for vrt_id in _all_id_lists (entry , prefix )]
116
+ return [
117
+ vrt_id for entry in sub_vrt
118
+ for vrt_id in _all_id_lists (entry , prefix )
119
+ ]
102
120
elif isinstance (sub_vrt , dict ):
103
121
if 'children' in sub_vrt :
104
122
new_prefix = prefix + [sub_vrt ['id' ]]
0 commit comments