1
|
1 |
|
import webbrowser |
2
|
1 |
|
import json |
3
|
1 |
|
import os |
4
|
1 |
|
import shutil |
5
|
1 |
|
import pprint |
6
|
1 |
|
from datetime import datetime |
7
|
1 |
|
import sys |
8
|
1 |
|
import uuid |
9
|
|
|
|
10
|
1 |
|
from .converter import Converter |
11
|
1 |
|
from .client import Client |
12
|
|
|
|
13
|
|
|
|
14
|
1 |
|
class ArfToJson(Client): |
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 |
|
def file_is_empty(self, path): |
19
|
1 |
|
return os.stat(path).st_size == 0 |
20
|
|
|
|
21
|
1 |
|
def save_dict_as_json(self, dict_, src): |
22
|
1 |
|
if os.path.isfile(src) and not self.file_is_empty(src): |
23
|
1 |
|
with open(src, "r") as f: |
24
|
1 |
|
data = json.load(f) |
25
|
1 |
|
for key in data: |
26
|
1 |
|
dict_[key] = data[key] |
27
|
1 |
|
with open(src, "w+") as f: |
28
|
1 |
|
json.dump(dict_, f) |
29
|
|
|
|
30
|
1 |
|
def prepare_data(self, rules): |
31
|
1 |
|
try: |
32
|
1 |
|
out = [] |
33
|
1 |
|
rule = None |
34
|
1 |
|
out_oval_tree_dict = dict() |
35
|
1 |
|
for rule in rules['rules']: |
36
|
1 |
|
date = str(datetime.now().strftime("-%d_%m_%Y-%H_%M_%S")) |
37
|
1 |
|
out_oval_tree_dict['graph-of-' + rule + |
38
|
|
|
date] = self.create_dict_of_rule(rule) |
39
|
1 |
|
if self.out is not None: |
40
|
1 |
|
self.save_dict_as_json(out_oval_tree_dict, self.out) |
41
|
1 |
|
out.append(self.out) |
42
|
|
|
else: |
43
|
1 |
|
print( |
44
|
|
|
str(json.dumps(out_oval_tree_dict, sort_keys=False, indent=4))) |
45
|
1 |
|
return out |
46
|
1 |
|
except Exception as error: |
47
|
1 |
|
raise ValueError('Rule: "{}" Error: "{}"'.format(rule, error)) |
48
|
|
|
|
49
|
1 |
|
def prepare_parser_out(self): |
50
|
1 |
|
self.parser.add_argument( |
51
|
|
|
'-o', |
52
|
|
|
'--output', |
53
|
|
|
action="store", |
54
|
|
|
default=None, |
55
|
|
|
help="The file where to save output.") |
56
|
|
|
|