Passed
Pull Request — master (#156)
by Jan
04:02
created

Client.prepare_parser()   B

Complexity

Conditions 1

Size

Total Lines 39
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 38
nop 1
dl 0
loc 39
ccs 9
cts 9
cp 1
crap 1
rs 8.968
c 0
b 0
f 0
1 1
import argparse
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
introduced by
import missing from __future__ import absolute_import
Loading history...
2 1
import re
3 1
import sys
4 1
from datetime import datetime
5
6 1
from ..__init__ import __version__
7
8
9 1
class Client():
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
best-practice introduced by
Too many instance attributes (12/7)
Loading history...
10 1
    def __init__(self, args):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
11 1
        self.parser = None
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
12 1
        self.arg = self.parse_arguments(args)
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
13 1
        self.hide_passing_tests = self.arg.hide_passing_tests
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
14 1
        self.source_filename = self.arg.source_filename
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
15 1
        self.rule_name = self.arg.rule_id
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
16 1
        self.out = self.arg.output
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
17 1
        self.verbose = self.arg.verbose
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
18
19 1
        self.date = str(datetime.now().strftime("-%d_%m_%Y-%H_%M_%S"))
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
20 1
        self.isatty = sys.stdout.isatty()
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
21
22 1
        self.all_rules = self.arg.all
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
23 1
        self.show_failed_rules = False
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
24 1
        self.show_not_selected_rules = False
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
25
26 1
    @staticmethod
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
27
    def _get_message():
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
28 1
        return {
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
29
            'description': '',
30
            'source_filename': '',
31
        }
32
33
    #    Non-implemented functions can handle different types of the input file.
34
    #    Functions for input file processing and corresponding rule ID.
35
36 1
    def prepare_data(self, rules):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
37
        """
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
38
        Function processes HTML graphs or JSON and
39
        return array of where is saved output file if exitsts.
40
        """
41 1
        raise NotImplementedError
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
42
43
    # Functions for selection of rules
44
45 1
    def search_rules_id(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
46
        """
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
47
        Function retunes array of all matched IDs of rules in selected file.
48
        """
49 1
        raise NotImplementedError
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
50
51 1
    def get_only_fail_rule(self, rules):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
52
        """
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
53
        Function processes array of matched IDs of rules in selected file.
54
        Function retunes array of failed matched IDs of rules in selected file.
55
        """
56 1
        raise NotImplementedError
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
57
58 1
    def _get_lines_of_wanted_not_selected_rules(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
Coding Style Naming introduced by
Method name "_get_lines_of_wanted_not_selected_rules" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
59
        """
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
60
        Function retunes array of lines where is not selected IDs of rules in selected file.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (92/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
61
        """
62 1
        raise NotImplementedError
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
63
64 1
    def run_gui_and_return_answers(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
introduced by
Missing function or method docstring
Loading history...
65 1
        if self.isatty:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
66 1
            if self.all_rules:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
67 1
                return self._get_rules()
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 16 tabs, expected 4
Loading history...
68
69 1
            try:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
70 1
                import inquirer
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 16 tabs, expected 4
Loading history...
introduced by
Import outside toplevel (inquirer)
Loading history...
71 1
                return inquirer.prompt(self.get_questions())
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 16 tabs, expected 4
Loading history...
72 1
            except ImportError:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
73 1
                print(self.get_selection_rules())
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 16 tabs, expected 4
Loading history...
74 1
            return None
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
75 1
        return self._get_rules()
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
76
77 1
    def _get_rules(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
78 1
        if self.show_failed_rules:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
79 1
            return {'rules': self.get_only_fail_rule(self.search_rules_id())}
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
80 1
        return {'rules': self.search_rules_id()}
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
81
82 1
    def _get_list_of_matched_rules(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
83 1
        if self.show_failed_rules:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
84 1
            return self.get_only_fail_rule(self.search_rules_id())
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
85 1
        return self.search_rules_id()
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
86
87 1
    def _get_list_of_lines(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
88 1
        lines = ['== The Rule ID regular expressions ==']
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
89 1
        for rule in self._get_list_of_matched_rules():
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
90 1
            lines.append("^" + rule + "$")
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
91 1
        if self.show_not_selected_rules:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
92 1
            for line in self._get_lines_of_wanted_not_selected_rules():
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
93 1
                lines.append(line)
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 16 tabs, expected 4
Loading history...
94 1
        lines.append(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
95
            "Interactive rule selection is not available,"
96
            " because inquirer is not installed."
97
            " Copy id of the rule you want to visualize and"
98
            " paste it into a command with regular"
99
            " expression characters(^$).\n"
100
            "Alternatively, use the --all or --all-in-one arguments.")
101 1
        return lines
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
102
103 1
    def get_selection_rules(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
introduced by
Missing function or method docstring
Loading history...
104 1
        return "\n".join(self._get_list_of_lines())
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
105
106 1
    def _get_choices(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
107 1
        if self.show_not_selected_rules:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
108 1
            print("\n".join(self._get_lines_of_wanted_not_selected_rules()))
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
109 1
        return self._get_list_of_matched_rules()
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
110
111 1
    def get_questions(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
introduced by
Missing function or method docstring
Loading history...
112 1
        from inquirer.questions import Checkbox as checkbox
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
introduced by
Import outside toplevel (inquirer.questions.Checkbox)
Loading history...
113 1
        choices = self._get_choices()
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
114 1
        questions = [
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
115
            checkbox(
116
                'rules',
117
                message=(
118
                    "= The Rules IDs = (move - UP and DOWN arrows,"
119
                    " select - SPACE or LEFT and RIGHT arrows, submit - ENTER)"),
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (81/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
120
                choices=choices,
121
            ),
122
        ]
123 1
        return questions
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
124
125 1
    def _get_wanted_rules_from_array_of_ids(self, rules):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
Coding Style Naming introduced by
Method name "_get_wanted_rules_from_array_of_ids" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
126 1
        return [
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
127
            x for x in rules if re.search(
128
                self.rule_name, x)]
129
130 1
    def _check_rules_id(self, rules, notselected_rules):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
131 1
        if notselected_rules and not rules:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
132 1
            raise ValueError(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
133
                ('Rule(s) "{}" was not selected, '
134
                 "so there are no results. The rule is"
135
                 ' "notselected" because it'
136
                 " wasn't a part of the executed profile"
137
                 " and therefore it wasn't evaluated "
138
                 "during the scan.")
139
                .format(notselected_rules))
140 1
        if not notselected_rules and not rules:
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
141 1
            raise ValueError('404 rule "{}" not found!'.format(self.rule_name))
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 12 tabs, expected 3
Loading history...
142 1
        return rules
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
143
144
    # Function for setting arguments
145
146 1
    def parse_arguments(self, args):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
introduced by
Missing function or method docstring
Loading history...
147 1
        self.prepare_parser()
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
148 1
        return self.parser.parse_args(args)
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
149
150 1
    def prepare_args_when_user_can_list_in_rules(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
Coding Style Naming introduced by
Method name "prepare_args_when_user_can_list_in_rules" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
introduced by
Missing function or method docstring
Loading history...
151 1
        self.parser.add_argument(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
152
            '--show-failed-rules',
153
            action="store_true",
154
            default=False,
155
            help="Show only FAILED rules")
156 1
        self.parser.add_argument(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
157
            '--show-not-selected-rules',
158
            action="store_true",
159
            default=False,
160
            help="Show notselected rules. These rules will not be visualized.")
161
162 1
    def prepare_parser(self):
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 4 tabs, expected 1
Loading history...
introduced by
Missing function or method docstring
Loading history...
163 1
        self.parser = argparse.ArgumentParser(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
164
            prog='oval-graph',
165
            description=self._get_message().get('description'))
166 1
        self.parser.add_argument(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
167
            '--version',
168
            action='version',
169
            version='%(prog)s ' + __version__)
170 1
        self.parser.add_argument(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
171
            '-a',
172
            '--all',
173
            action="store_true",
174
            default=False,
175
            help="Process all matched rules.")
176 1
        self.parser.add_argument(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
177
            '--hide-passing-tests',
178
            action="store_true",
179
            default=False,
180
            help=(
181
                "Do not display passing tests for better orientation in"
182
                " graphs that contain a large amount of nodes."))
183 1
        self.parser.add_argument(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
184
            '-v',
185
            '--verbose',
186
            action="store_true",
187
            default=False,
188
            help="Displays details about the results of the running command.")
189 1
        self.parser.add_argument(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
190
            '-o',
191
            '--output',
192
            action="store",
193
            default=None,
194
            help='The file where to save output.')
195 1
        self.parser.add_argument(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
196
            "source_filename",
197
            help=self._get_message().get('source_filename'))
198 1
        self.parser.add_argument(
0 ignored issues
show
Coding Style introduced by
Bad indentation. Found 8 tabs, expected 2
Loading history...
199
            "rule_id", help=(
200
                "Rule ID to be visualized. A part from the full rule ID"
201
                " a part of the ID or a regular expression can be used."
202
                " If brackets are used in the regular expression "
203
                "the regular expression must be quoted."))
204