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