Passed
Push — master ( 8261ad...b8618f )
by Jan
07:11 queued 02:20
created

Client._get_choices()   A

Complexity

Conditions 5

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 19.4658

Importance

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