-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
189 lines (154 loc) · 5.78 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import importlib
import json
import os.path
import readline
import os
from datetime import datetime
def INIT():
#Initial configuration
with open("config.json") as config_file:
config = json.load(config_file)
return config
def CHOOSE_MODULES(config):
#Showing Modules
module_names = config.keys()
readline.set_completer(SimpleCompleter(module_names).complete)
print("\nChoose one of the modules: ")
for name in module_names:
print(" -> " + name)
print(" -> Return to Main Menu")
#Choosing module
print
choosen_module = input("Module: ")
if choosen_module in ["Return", "return", "Quit", "quit", "Exit", "exit"]:
return 0
if choosen_module not in module_names:
print("Invalid module!!")
return -1
return config[choosen_module]
def CHOOSE_MODULES_REPORT(config):
#Showing Modules
module_names = config.keys()
readline.set_completer(SimpleCompleter(list(module_names) + ["execute_report"]).complete)
print("\nChoose the modules for the Report: ")
print("\nTo execute the report choose [execute_report]")
choosen_module = {}
modules = []
while choosen_module != "execute_report":
for name in module_names:
print(" -> " + name)
print(" -> execute_report")
print(" -> Return to Main Menu")
#Choosing module
print
choosen_module = input("Module: ")
if choosen_module in ["Return", "return", "Quit", "quit", "Exit", "exit"]:
return 0
if choosen_module not in module_names and choosen_module != "execute_report":
print("Invalid module!!")
continue
if choosen_module in module_names:
modules.append(config[choosen_module])
return modules
def MODULE_EXECUTION(config, choosen_module):
#Module choosen
module = importlib.import_module("modules." + choosen_module["directory"] + "." + choosen_module["name"])
if os.path.isfile("modules/" + choosen_module["directory"] + "/" + choosen_module["name"] + ".json"):
readline.set_completer(SimpleCompleter(["yes","no"]).complete)
answer = input("Use the last used parameters? [yes/no] ")
if answer == "yes":
return module.RUN("modules/" + choosen_module["directory"] + "/" + choosen_module["name"])
querie = module.BEGIN()
dic = {}
for field in querie:
dic[field] = input(field + ": ")
with open("modules/" + choosen_module["directory"] + "/" + choosen_module["name"] + ".json", "w") as parameters:
json.dump(dic, parameters)
return module.RUN("modules/" + choosen_module["directory"] + "/" + choosen_module["name"])
def REPORT(config, choosen_modules):
str_time = datetime.now().strftime("%d-%m-%Y %H:%M:%S")
report = {}
report["time"] = str_time
report["result"] = {}
answer = ""
for module in choosen_modules:
if os.path.isfile("modules/" + module["directory"] + "/" + module["name"] + "_result.json"):
readline.set_completer(SimpleCompleter(["yes","no"]).complete)
while(answer != "no" and answer != "yes"):
answer = input("Use the last generated report? [yes/no] ")
if answer != "yes" and answer != "no":
print("Invalid answer")
if answer != "yes":
MODULE_EXECUTION(config, module)
with open("modules/" + module["directory"] + "/" + module["name"] + "_result.json","r") as module_report:
report_dict = json.load(module_report)
report["result"][report_dict["title"]] = report_dict["result"]
answer = ""
filename = "reports/" + str_time + ".json"
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename,"w") as output_data:
json.dump(report, output_data)
def MAIN():
config_string = "reload_configuration"
module_string = "execute_module"
report_string = "generate_report"
exit_string = "exit"
action = ""
config = INIT()
readline.parse_and_bind('tab: complete')
while(action not in ["Exit", "exit", "Quit", "quit"]):
readline.set_completer(SimpleCompleter([config_string, module_string, report_string, exit_string]).complete)
print("\nChoose a action: ")
print(" -> " + config_string)
print(" -> " + module_string)
print(" -> " + report_string)
print(" -> " + exit_string)
action = input("Action: ")
if action == config_string:
config = INIT()
elif action == module_string:
module = CHOOSE_MODULES(config)
if module == 0 or module == -1:
continue
else:
MODULE_EXECUTION(config, module)
elif action == report_string:
modules = CHOOSE_MODULES_REPORT(config)
if modules == 0 or modules == -1:
continue
else:
REPORT(config, modules)
elif action not in ["Exit", "exit", "Quit", "quit"]:
print("Invalid Action")
print
print("$$$$$$$\ $$\ $$$$$$\ $$\ $$\ $$$$$$$$\ ")
print("$$ __$$\ $$ | \_$$ _|$$$\ $$$ |$$ _____|")
print("$$ | $$ | $$$$$$\ $$$$$$$\$$$$$$\ $$ | $$$$\ $$$$ |$$ | ")
print("$$$$$$$ |$$ __$$\ $$ __$$\_$$ _| $$ | $$\$$\$$ $$ |$$$$$\ ")
print("$$ ____/ $$$$$$$$ |$$ | $$ |$$ | $$ | $$ \$$$ $$ |$$ __| ")
print("$$ | $$ ____|$$ | $$ |$$ |$$\ $$ | $$ |\$ /$$ |$$ | ")
print("$$ | \$$$$$$$\ $$ | $$ |\$$$$ |$$$$$$\ $$ | \_/ $$ |$$$$$$$$\ ")
print("\__| \_______|\__| \__| \____/ \______|\__| \__|\________|")
return
class SimpleCompleter(object):
def __init__(self, options):
self.options = sorted(options)
return
def complete(self, text, state):
response = None
if state == 0:
# This is the first time for this text, so build a match list.
if text:
self.matches = [s
for s in self.options
if s and s.startswith(text)]
else:
self.matches = self.options[:]
# Return the state'th item from the match list,
# if we have that many.
try:
response = self.matches[state]
except IndexError:
response = None
return response
MAIN()