| Total Complexity | 14 |
| Total Lines | 53 |
| Duplicated Lines | 0 % |
| Coverage | 97.22% |
| Changes | 0 | ||
| 1 | 1 | import json |
|
|
|
|||
| 2 | 1 | import os |
|
| 3 | |||
| 4 | 1 | from ..exceptions import NotChecked |
|
| 5 | 1 | from .client_arf_input import ClientArfInput |
|
| 6 | |||
| 7 | |||
| 8 | 1 | class ArfToJson(ClientArfInput): |
|
| 9 | 1 | def _get_message(self): |
|
| 10 | 1 | return { |
|
| 11 | 'description': 'Client for generating JSON of SCAP rule evaluation results', |
||
| 12 | 'source_filename': 'ARF scan file', |
||
| 13 | } |
||
| 14 | |||
| 15 | 1 | def create_dict_of_rule(self, rule_id): |
|
| 16 | 1 | return self.xml_parser.get_oval_tree(rule_id).save_tree_to_dict() |
|
| 17 | |||
| 18 | 1 | @staticmethod |
|
| 19 | def file_is_empty(path): |
||
| 20 | 1 | return os.stat(path).st_size == 0 |
|
| 21 | |||
| 22 | 1 | def save_dict_as_json(self, dict_, src): |
|
| 23 | 1 | if os.path.isfile(src) and not self.file_is_empty(src): |
|
| 24 | 1 | with open(src, "r") as file_: |
|
| 25 | 1 | data = json.load(file_) |
|
| 26 | 1 | for key in data: |
|
| 27 | 1 | dict_[key] = data[key] |
|
| 28 | 1 | with open(src, "w+") as file_: |
|
| 29 | 1 | json.dump(dict_, file_) |
|
| 30 | |||
| 31 | 1 | def prepare_data(self, rules): |
|
| 32 | 1 | out = [] |
|
| 33 | 1 | rule = None |
|
| 34 | 1 | out_oval_tree_dict = dict() |
|
| 35 | 1 | for rule in rules['rules']: |
|
| 36 | 1 | try: |
|
| 37 | 1 | out_oval_tree_dict[ |
|
| 38 | rule + self.date] = self.create_dict_of_rule(rule) |
||
| 39 | 1 | except NotChecked as error: |
|
| 40 | out_oval_tree_dict[ |
||
| 41 | rule + self.date] = str(error) |
||
| 42 | 1 | if self.out is not None: |
|
| 43 | 1 | self.save_dict_as_json(out_oval_tree_dict, self.out) |
|
| 44 | 1 | out.append(self.out) |
|
| 45 | else: |
||
| 46 | 1 | print( |
|
| 47 | str(json.dumps(out_oval_tree_dict, sort_keys=False, indent=4))) |
||
| 48 | 1 | return out |
|
| 49 | |||
| 50 | 1 | def prepare_parser(self): |
|
| 51 | 1 | super().prepare_parser() |
|
| 52 | self.prepare_args_when_user_can_list_in_rules() |
||
| 53 |