-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathterminal.py
153 lines (144 loc) · 6.55 KB
/
terminal.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
import os
import sys
import log
import help
import util
import database
OPTIONS = ['set', 'run', 'export', 'list', 'q', 'quit', 'exit', 'clear', 'cls', '?', 'stop', 'help', 'general', 'user', 'group', 'computer', 'regex', 'all', 'search']
class Completer:
def __init__(self, options):
self.options = sorted(options)
def complete(self, text, state):
response = None
if state == 0:
if text:
self.matches = [
s
for s in self.options
if s and s.startswith(text)
]
else:
self.matches = self.options[:]
try:
response = self.matches[state]
except IndexError:
response = None
return response
class Terminal:
def __init__(self, user, pwd, db):
try:
self.driver = database.Driver(user, pwd, db)
except Exception as e:
log.log_error(e)
def input_loop(self):
try:
while True:
line = input(': ')
if line == 'q' or line == 'quit' or line == 'exit' or line == 'stop':
self.driver.close()
sys.exit(0)
elif line == 'help' or line == '?' or line == '':
help.print_help()
continue
elif line == 'clear' or line == 'cls':
os.system('cls' if os.name == 'nt' else 'clear')
continue
command = line.split(" ")
if command[0] not in OPTIONS:
log.log_command_invalid(line)
continue
if command[0] == "set":
if len(command) < 3:
log.log_command_invalid(line)
continue
else:
option = command[1]
if not util.validate_set_option(option):
log.log_invalid_option(option)
continue
else:
if option == "user":
user_in = line[9:]
if util.validate_common_config(user_in):
util.validate_user_input(user_in)
self.driver.set_user_param(user_in)
log.log_successful_set("user", user_in)
else:
log.log_error("User is empty or is missing @!")
elif option == "group":
group_in = line[10:]
if util.validate_common_config(group_in):
util.validate_user_input(group_in)
self.driver.set_group_param(group_in)
log.log_successful_set("group", group_in)
else:
log.log_error("Group is empty or is missing @!")
elif option == "computer":
c_in = line[13:]
if util.validate_common_config(c_in):
util.validate_user_input(c_in)
self.driver.set_computer_param(c_in)
log.log_successful_set("computer", c_in)
else:
log.log_error("Computer is empty or is missing @!")
elif option == "regex":
r_in = line[10:]
if util.validate_regex_config(r_in):
util.validate_user_input(r_in)
self.driver.set_regex(r_in)
log.log_successful_set("regex", r_in)
else:
log.log_error("Regex is empty!")
else:
log.log_command_invalid(line)
continue
elif command[0] == "run":
if len(command) != 2:
log.log_command_invalid(line)
continue
else:
option = command[1]
if not 1 <= int(option) <= len(self.driver.queries):
log.log_invalid_option(option)
continue
else:
self.driver.run_query(option, "")
elif command[0] == "export":
if len(command) < 3:
log.log_command_invalid(line)
continue
else:
option = command[1]
fname = command[2]
if not 1 <= int(option) <= len(self.driver.queries):
log.log_invalid_option(option)
continue
else:
if len(command) == 3:
f = util.validate_export_command(fname)
self.driver.run_query(option, f)
else:
log.log_command_invalid(line)
continue
elif command[0] == "list":
if len(command) < 2:
log.log_command_invalid(line)
continue
else:
option = command[1]
if not util.validate_list_command(option):
log.log_invalid_option(option)
else:
self.driver.print_queries_by_type(option)
elif command[0] == "search":
if len(command) < 2:
log.log_command_invalid(line)
continue
else:
search = line[7:]
self.driver.search_queries(search)
else:
log.log_command_invalid(line)
continue
except Exception as e:
log.log_error(e)