1
|
1 |
|
import argparse |
2
|
1 |
|
import re |
3
|
1 |
|
import sys |
4
|
1 |
|
from datetime import datetime |
5
|
|
|
|
6
|
1 |
|
from .. import __version__ |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
class Client(): |
10
|
1 |
|
def __init__(self, args): |
11
|
1 |
|
self.arg = self.parse_arguments(args) |
12
|
|
|
|
13
|
1 |
|
self.source_filename = self.arg.source_filename |
14
|
1 |
|
self.rule_name = self.arg.rule_id |
15
|
|
|
|
16
|
1 |
|
self.isatty = sys.stdout.isatty() |
17
|
|
|
|
18
|
1 |
|
self.all_rules = self.arg.all |
19
|
1 |
|
self.show_failed_rules = False |
20
|
1 |
|
self.show_not_selected_rules = False |
21
|
1 |
|
self.show_not_tested_rules = False |
22
|
|
|
|
23
|
1 |
|
self.verbose = self.arg.verbose |
24
|
|
|
|
25
|
1 |
|
@staticmethod |
26
|
|
|
def _get_message(): |
27
|
1 |
|
return { |
28
|
|
|
'command_name': '', |
29
|
|
|
'description': '', |
30
|
|
|
'source_filename': '', |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
@staticmethod |
34
|
|
|
def _get_date(): |
35
|
1 |
|
return str(datetime.now().strftime("-%d_%m_%Y-%H_%M_%S")) |
36
|
|
|
|
37
|
|
|
# Functions for selection of rules |
38
|
|
|
|
39
|
1 |
|
def search_rules_id(self): |
40
|
|
|
""" |
41
|
|
|
Function returns array of all matched IDs of rules in selected file. |
42
|
|
|
""" |
43
|
1 |
|
raise NotImplementedError |
44
|
|
|
|
45
|
1 |
|
def get_only_fail_rule(self, rules): |
46
|
|
|
""" |
47
|
|
|
Function processes array of matched IDs of rules in selected file. |
48
|
|
|
Function returns array of failed matched IDs of rules in selected file. |
49
|
|
|
""" |
50
|
1 |
|
raise NotImplementedError |
51
|
|
|
|
52
|
1 |
|
def _get_rows_of_unselected_rules(self): |
53
|
|
|
""" |
54
|
|
|
Function returns array of rows where is not selected IDs of rules in selected file. |
55
|
|
|
""" |
56
|
|
|
raise NotImplementedError |
57
|
|
|
|
58
|
1 |
|
def load_file(self): |
59
|
|
|
""" |
60
|
|
|
Function returns parser or data. |
61
|
|
|
""" |
62
|
|
|
raise NotImplementedError |
63
|
|
|
|
64
|
1 |
|
def _get_rows_not_visualizable_rules(self): |
65
|
|
|
""" |
66
|
|
|
Function returns array of rows where is not selected IDs of rules in selected file. |
67
|
|
|
""" |
68
|
|
|
raise NotImplementedError |
69
|
|
|
|
70
|
1 |
|
def run_gui_and_return_answers(self): |
71
|
1 |
|
if self.isatty: |
72
|
1 |
|
if self.all_rules: |
73
|
|
|
return self._get_rules() |
74
|
|
|
|
75
|
1 |
|
print(self.get_selection_rules()) |
76
|
1 |
|
return None |
77
|
1 |
|
return self._get_rules() |
78
|
|
|
|
79
|
1 |
|
def _get_rules(self): |
80
|
1 |
|
rules = self.search_rules_id() |
81
|
1 |
|
if self.show_failed_rules: |
82
|
1 |
|
return {'rules': self.get_only_fail_rule(rules)} |
83
|
1 |
|
return {'rules': rules} |
84
|
|
|
|
85
|
1 |
|
def _get_list_of_matched_rules(self): |
86
|
1 |
|
rules = self.search_rules_id() |
87
|
1 |
|
if self.show_failed_rules: |
88
|
1 |
|
return self.get_only_fail_rule(rules) |
89
|
1 |
|
return rules |
90
|
|
|
|
91
|
1 |
|
def _get_list_of_lines(self): |
92
|
1 |
|
lines = ['== The Rule ID regular expressions =='] |
93
|
1 |
|
for rule in self._get_list_of_matched_rules(): |
94
|
1 |
|
lines.append("^" + rule + "$") |
95
|
1 |
|
if self.show_not_selected_rules and not self.show_not_tested_rules: |
96
|
1 |
|
for line in self._get_rows_of_unselected_rules(): |
97
|
1 |
|
lines.append(line) |
98
|
1 |
|
if not self.show_not_selected_rules and self.show_not_tested_rules: |
99
|
|
|
for line in self._get_rows_not_visualizable_rules(): |
100
|
|
|
lines.append(line) |
101
|
1 |
|
lines.append( |
102
|
|
|
" Copy id of the rule you want to visualize and" |
103
|
|
|
" paste it into a command with regular" |
104
|
|
|
" expression characters(^$).\n" |
105
|
|
|
"Alternatively, use the --all or --all-in-one arguments.") |
106
|
1 |
|
return lines |
107
|
|
|
|
108
|
1 |
|
def get_selection_rules(self): |
109
|
1 |
|
return "\n".join(self._get_list_of_lines()) |
110
|
|
|
|
111
|
1 |
|
def _get_choices(self): |
112
|
|
|
if self.show_not_selected_rules and not self.show_not_tested_rules: |
113
|
|
|
print("\n".join(self._get_rows_of_unselected_rules())) |
114
|
|
|
if not self.show_not_selected_rules and self.show_not_tested_rules: |
115
|
|
|
print("\n".join(self._get_rows_not_visualizable_rules())) |
116
|
|
|
return self._get_list_of_matched_rules() |
117
|
|
|
|
118
|
1 |
|
def _get_wanted_rules(self, rules): |
119
|
1 |
|
return [ |
120
|
|
|
x for x in rules if re.search( |
121
|
|
|
self.rule_name, x)] |
122
|
|
|
|
123
|
|
|
# Function for setting arguments |
124
|
|
|
|
125
|
1 |
|
def parse_arguments(self, args): |
126
|
1 |
|
parser = argparse.ArgumentParser( |
127
|
|
|
prog=self._get_message().get('command_name'), |
128
|
|
|
description=self._get_message().get('description')) |
129
|
1 |
|
self.prepare_parser(parser) |
130
|
1 |
|
if args is None: |
131
|
|
|
return parser.parse_args() |
132
|
1 |
|
return parser.parse_args(args) |
133
|
|
|
|
134
|
1 |
|
@staticmethod |
135
|
|
|
def prepare_args_when_user_can_list_in_rules(parser): |
136
|
1 |
|
parser.add_argument( |
137
|
|
|
'--show-failed-rules', |
138
|
|
|
action="store_true", |
139
|
|
|
default=False, |
140
|
|
|
help="Show only FAILED rules") |
141
|
1 |
|
parser.add_argument( |
142
|
|
|
'--show-not-selected-rules', |
143
|
|
|
action="store_true", |
144
|
|
|
default=False, |
145
|
|
|
help="Show notselected rules. These rules will not be visualized.") |
146
|
1 |
|
parser.add_argument( |
147
|
|
|
'--show-not-tested-rules', |
148
|
|
|
action="store_true", |
149
|
|
|
default=False, |
150
|
|
|
help="Shows rules which weren't tested. These rules will not be visualized.") |
151
|
|
|
|
152
|
1 |
|
def prepare_parser(self, parser): |
153
|
1 |
|
parser.add_argument( |
154
|
|
|
'--version', |
155
|
|
|
action='version', |
156
|
|
|
version='%(prog)s ' + __version__) |
157
|
1 |
|
parser.add_argument( |
158
|
|
|
'-a', |
159
|
|
|
'--all', |
160
|
|
|
action="store_true", |
161
|
|
|
default=False, |
162
|
|
|
help="Process all matched rules.") |
163
|
1 |
|
parser.add_argument( |
164
|
|
|
'--hide-passing-tests', |
165
|
|
|
action="store_true", |
166
|
|
|
default=False, |
167
|
|
|
help=( |
168
|
|
|
"Do not display passing tests for better orientation in" |
169
|
|
|
" graphs that contain a large amount of nodes.")) |
170
|
1 |
|
parser.add_argument( |
171
|
|
|
'-v', |
172
|
|
|
'--verbose', |
173
|
|
|
action="store_true", |
174
|
|
|
default=False, |
175
|
|
|
help="Displays details about the results of the running command.") |
176
|
1 |
|
parser.add_argument( |
177
|
|
|
'-o', |
178
|
|
|
'--output', |
179
|
|
|
action="store", |
180
|
|
|
default=None, |
181
|
|
|
help=("The path where to save the output. If there are more report " |
182
|
|
|
"files to generate it will create an OUTPUT path as a directory.")) |
183
|
1 |
|
parser.add_argument( |
184
|
|
|
"source_filename", |
185
|
|
|
help=self._get_message().get('source_filename')) |
186
|
1 |
|
parser.add_argument( |
187
|
|
|
"rule_id", help=( |
188
|
|
|
"Rule ID to be visualized. A part from the full rule ID" |
189
|
|
|
" a part of the ID or a regular expression can be used." |
190
|
|
|
" If brackets are used in the regular expression " |
191
|
|
|
"the regular expression must be quoted.")) |
192
|
|
|
|