Skip to content

Commit e229909

Browse files
committed
Removed duplication in logic for determining column mapping
1 parent 762f48d commit e229909

File tree

1 file changed

+100
-81
lines changed

1 file changed

+100
-81
lines changed

dojo/tools/generic/parser.py

Lines changed: 100 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,71 @@
66

77

88
class ColumnMappingStrategy(object):
9+
10+
mapped_column = None
11+
912
def __init__(self):
1013
self.successor = None
1114

12-
def process_column(self, column_name, column_value, finding):
15+
def map_column_value(self, finding, column_value):
1316
pass
1417

15-
16-
class DateColumnMappingStrategy(ColumnMappingStrategy):
18+
@staticmethod
19+
def evaluate_bool_value(column_value):
20+
if column_value.lower() == 'true':
21+
return True
22+
elif column_value.lower() == 'false':
23+
return False
24+
else:
25+
return None
1726

1827
def process_column(self, column_name, column_value, finding):
1928

20-
if column_name.lower() == 'date' and column_value is not None:
21-
finding.date = parse(column_value).date()
29+
if column_name.lower() == self.mapped_column and column_value is not None:
30+
self.map_column_value(finding, column_value)
2231
elif self.successor is not None:
2332
self.successor.process_column(column_name, column_value, finding)
2433

2534

35+
class DateColumnMappingStrategy(ColumnMappingStrategy):
36+
37+
def __init__(self):
38+
self.mapped_column = 'date'
39+
super(DateColumnMappingStrategy, self).__init__()
40+
41+
def map_column_value(self, finding, column_value):
42+
finding.date = parse(column_value).date()
43+
44+
2645
class TitleColumnMappingStrategy(ColumnMappingStrategy):
2746

28-
def process_column(self, column_name, column_value, finding):
29-
if column_name.lower() == 'title' and column_value is not None:
30-
finding.title = column_value
31-
elif self.successor is not None:
32-
self.successor.process_column(column_name, column_value, finding)
47+
def __init__(self):
48+
self.mapped_column = 'title'
49+
super(TitleColumnMappingStrategy, self).__init__()
50+
51+
def map_column_value(self, finding, column_value):
52+
finding.title = column_value
3353

3454

3555
class CweColumnMappingStrategy(ColumnMappingStrategy):
3656

37-
def process_column(self, column_name, column_value, finding):
38-
if column_name.lower() == 'cweid' and column_value is not None:
39-
if column_value.isdigit():
40-
finding.cwe = int(column_value)
41-
elif self.successor is not None:
42-
self.successor.process_column(column_name, column_value, finding)
57+
def __init__(self):
58+
self.mapped_column = 'cweid'
59+
super(CweColumnMappingStrategy, self).__init__()
60+
61+
def map_column_value(self, finding, column_value):
62+
if column_value.isdigit():
63+
finding.cwe = int(column_value)
4364

4465

4566
class UrlColumnMappingStrategy(ColumnMappingStrategy):
4667

47-
def process_column(self, column_name, column_value, finding):
48-
if column_name.lower() == 'url' and column_value is not None:
49-
finding.url = column_value
50-
elif self.successor is not None:
51-
self.successor.process_column(column_name, column_value, finding)
68+
def __init__(self):
69+
self.mapped_column = 'url'
70+
super(UrlColumnMappingStrategy, self).__init__()
71+
72+
def map_column_value(self, finding, column_value):
73+
finding.url = column_value
5274

5375

5476
class SeverityColumnMappingStrategy(ColumnMappingStrategy):
@@ -58,98 +80,95 @@ def is_valid_severity(severity):
5880
valid_severity = ('Info', 'Low', 'Medium', 'High', 'Critical')
5981
return severity in valid_severity
6082

61-
def process_column(self, column_name, column_value, finding):
62-
if column_name.lower() == 'severity' and column_value is not None:
63-
if self.is_valid_severity(column_value):
64-
finding.severity = column_value
65-
else:
66-
finding.severity = 'Info'
67-
elif self.successor is not None:
68-
self.successor.process_column(column_name, column_value, finding)
83+
def __init__(self):
84+
self.mapped_column = 'severity'
85+
super(SeverityColumnMappingStrategy, self).__init__()
86+
87+
def map_column_value(self, finding, column_value):
88+
if self.is_valid_severity(column_value):
89+
finding.severity = column_value
90+
else:
91+
finding.severity = 'Info'
6992

7093

7194
class DescriptionColumnMappingStrategy(ColumnMappingStrategy):
7295

73-
def process_column(self, column_name, column_value, finding):
74-
if column_name.lower() == 'description' and column_value is not None:
75-
finding.description = column_value
76-
elif self.successor is not None:
77-
self.successor.process_column(column_name, column_value, finding)
96+
def __init__(self):
97+
self.mapped_column = 'description'
98+
super(DescriptionColumnMappingStrategy, self).__init__()
99+
100+
def map_column_value(self, finding, column_value):
101+
finding.description = column_value
78102

79103

80104
class MitigationColumnMappingStrategy(ColumnMappingStrategy):
81105

82-
def process_column(self, column_name, column_value, finding):
83-
if column_name.lower() == 'mitigation' and column_value is not None:
84-
finding.mitigation = column_value
85-
elif self.successor is not None:
86-
self.successor.process_column(column_name, column_value, finding)
106+
def __init__(self):
107+
self.mapped_column = 'mitigation'
108+
super(MitigationColumnMappingStrategy, self).__init__()
109+
110+
def map_column_value(self, finding, column_value):
111+
finding.mitigation = column_value
87112

88113

89114
class ImpactColumnMappingStrategy(ColumnMappingStrategy):
90115

91-
def process_column(self, column_name, column_value, finding):
92-
if column_name.lower() == 'impact' and column_value is not None:
93-
finding.impact = column_value
94-
elif self.successor is not None:
95-
self.successor.process_column(column_name, column_value, finding)
116+
def __init__(self):
117+
self.mapped_column = 'impact'
118+
super(ImpactColumnMappingStrategy, self).__init__()
119+
120+
def map_column_value(self, finding, column_value):
121+
finding.impact = column_value
96122

97123

98124
class ReferencesColumnMappingStrategy(ColumnMappingStrategy):
99125

100-
def process_column(self, column_name, column_value, finding):
101-
if column_name.lower() == 'references' and column_value is not None:
102-
finding.references = column_value
103-
elif self.successor is not None:
104-
self.successor.process_column(column_name, column_value, finding)
126+
def __init__(self):
127+
self.mapped_column = 'references'
128+
super(ReferencesColumnMappingStrategy, self).__init__()
129+
130+
def map_column_value(self, finding, column_value):
131+
finding.references = column_value
105132

106133

107134
class ActiveColumnMappingStrategy(ColumnMappingStrategy):
108135

109-
def process_column(self, column_name, column_value, finding):
110-
if column_name.lower() == 'active' and column_value is not None:
111-
if column_value.lower() == 'true':
112-
finding.active = True
113-
elif column_value.lower() == 'false':
114-
finding.active = False
115-
elif self.successor is not None:
116-
self.successor.process_column(column_name, column_value, finding)
136+
def __init__(self):
137+
self.mapped_column = 'active'
138+
super(ActiveColumnMappingStrategy, self).__init__()
139+
140+
def map_column_value(self, finding, column_value):
141+
finding.active = self.evaluate_bool_value(column_value)
117142

118143

119144
class VerifiedColumnMappingStrategy(ColumnMappingStrategy):
120145

121-
def process_column(self, column_name, column_value, finding):
122-
if column_name.lower() == 'verified' and column_value is not None:
123-
if column_value.lower() == 'true':
124-
finding.verified = True
125-
elif column_value.lower() == 'false':
126-
finding.verified = False
127-
elif self.successor is not None:
128-
self.successor.process_column(column_name, column_value, finding)
146+
def __init__(self):
147+
self.mapped_column = 'verified'
148+
super(VerifiedColumnMappingStrategy, self).__init__()
149+
150+
def map_column_value(self, finding, column_value):
151+
finding.verified = self.evaluate_bool_value(column_value)
129152

130153

131154
class FalsePositiveColumnMappingStrategy(ColumnMappingStrategy):
132155

133-
def process_column(self, column_name, column_value, finding):
134-
if column_name.lower() == 'falsepositive' and column_value is not None:
135-
if column_value.lower() == 'true':
136-
finding.false_p = True
137-
elif column_value.lower() == 'false':
138-
finding.false_p = False
139-
elif self.successor is not None:
140-
self.successor.process_column(column_name, column_value, finding)
156+
def __init__(self):
157+
self.mapped_column = 'falsepositive'
158+
super(FalsePositiveColumnMappingStrategy, self).__init__()
159+
160+
def map_column_value(self, finding, column_value):
161+
finding.false_p = self.evaluate_bool_value(column_value)
141162

142163

143164
class DuplicateColumnMappingStrategy(ColumnMappingStrategy):
144165

145-
def process_column(self, column_name, column_value, finding):
146-
if column_name.lower() == 'duplicate' and column_value is not None:
147-
if column_value.lower() == 'true':
148-
finding.duplicate = True
149-
elif column_value.lower() == 'false':
150-
finding.duplicate = False
151-
elif self.successor is not None:
152-
self.successor.process_column(column_name, column_value, finding)
166+
def __init__(self):
167+
self.mapped_column = 'duplicate'
168+
super(DuplicateColumnMappingStrategy, self).__init__()
169+
170+
def map_column_value(self, finding, column_value):
171+
finding.duplicate = self.evaluate_bool_value(column_value)
153172

154173

155174
class GenericFindingUploadCsvParser(object):

0 commit comments

Comments
 (0)