Test Failed
Push — master ( 1bab60...8e0a9b )
by Jan
02:31 queued 11s
created

utils.gen_profile_table   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 71
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A HtmlOutput._get_var_value() 0 5 2
A HtmlOutput._get_eligible_rules() 0 4 1
A HtmlOutput.__init__() 0 7 1
A HtmlOutput._generate_shortened_ref() 0 8 3
A HtmlOutput.process_rules() 0 4 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
A update_parser() 0 3 1
A parse_args() 0 7 1
1
#!/usr/bin/python3
2
3
import os
4
import sys
5
6
import argparse
7
8
import ssg.build_yaml
9
10
import tables.table_renderer
11
12
13
class HtmlOutput(tables.table_renderer.TableHtmlOutput):
14
    TEMPLATE_NAME = "tables/profile_tables_template.html"
15
16
    def __init__(self, * args, ** kwargs):
17
        profile_id = kwargs.pop("profile_id")
18
        super(HtmlOutput, self).__init__(* args, ** kwargs)
19
20
        profile_path = str(self.built_content_path / "profiles" / (profile_id + ".profile"))
21
        self.profile = ssg.build_yaml.Profile.from_yaml(profile_path, self.env_yaml)
22
        self.profile_variables = self.profile.variables
23
24
    def _get_var_value(self, varname):
25
        try:
26
            return self.profile_variables[varname]
27
        except KeyError:
28
            return super(HtmlOutput, self)._get_var_value(varname)
29
30
    def _get_eligible_rules(self, refcat):
31
        eligible_rule_ids = self.profile.selected
32
        filenames = [os.path.join(self.rules_root, rid + ".yml") for rid in eligible_rule_ids]
33
        return [ssg.build_yaml.Rule.from_yaml(f, self.env_yaml) for f in filenames]
34
35
    def _generate_shortened_ref(self, reference, rule):
36
        shortened_ref = super(HtmlOutput, self)._generate_shortened_ref(reference, rule)
37
        if shortened_ref == self.DEFAULT_SHORTENED_REF and self.verbose:
38
            print("Rule '{rid}' is included in the profile {profile_id}, "
39
                  "but doesn't contain the {ref_id} reference"
40
                  .format(rid=rule.id_, profile_id=self.profile.id_, ref_id=reference.id),
41
                  file=sys.stderr)
42
        return shortened_ref
43
44
    def process_rules(self, reference):
45
        super(HtmlOutput, self).process_rules(reference)
46
47
        self.template_data["profile"] = self.profile
48
49
50
def update_parser(parser):
51
    parser.add_argument(
52
        "profile", metavar="PROFILE_ID", help="The ID of the profile")
53
54
55
def parse_args():
56
    parser = HtmlOutput.create_parser(
57
        "Generate HTML table that maps references to rules in context of a profile "
58
        "using compiled rules and compiled profiles as source of data.")
59
    tables.table_renderer.update_parser(parser)
60
    update_parser(parser)
61
    return parser.parse_args()
62
63
64
if __name__ == "__main__":
65
    args = parse_args()
66
    renderer = HtmlOutput(
67
        args.product, args.build_dir, profile_id=args.profile, verbose=args.verbose)
68
    reference = ssg.constants.REFERENCES[args.refcategory]
69
    renderer.process_rules(reference)
70
    renderer.output_results(args)
71