Repository navigation
Expand file tree
/
Copy pathview.py
More file actions
168 lines (96 loc) · 3.91 KB
/
Copy pathview.py
File metadata and controls
168 lines (96 loc) · 3.91 KB
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
import os
import re
import sys
import time
from .control import DBInterface
exit_command_list = ["q", "quit", "x", "exit"]
def front_prompt(database_type: str=None, database_nickname: str=None):
def platform_prompt(platform_name: str) -> str:
platform_prompt_str = None
if re.search(r"^(linux*|freebsd*|openbsd*|darwin*)$", platform_name):
platform_prompt_str = "{0}:{1}".format(
os.environ["USER"],
os.environ["HOME"]
)
elif platform_name == "win32":
platform_prompt_str = "{0}:{1}".format(
os.environ["USERNAME"],
os.environ["USERDOMAIN"]
)
return platform_prompt_str
default_prompt_str = "{0} ".format(
str(time.ctime(time.time())),
)
if database_type:
if database_nickname:
default_prompt_str += "{0} {1} {2}> ".format(
database_type,
database_nickname,
platform_prompt(sys.platform)
)
else:
default_prompt_str += "{0} {1}> ".format(
database_type,
platform_prompt(sys.platform)
)
else:
default_prompt_str += "{0}> ".format(
platform_prompt(sys.platform)
)
return input(default_prompt_str)
def database_interface(database_type: str, db_nickname: str=None):
command_buffer = None
command_keys = [
"r", #return to previous command
"t", #execute previous command
"table", #display table inside the database
"db", #List out all the registered database
"help", #Look for help
"h" #Look for help
]
if db_nickname:
received_command = front_prompt(database_type, db_nickname)
else:
received_command = front_prompt(database_type)
os.environ["DB_TYPE"] = database_type
if database_type == "sqlite3":
db_client = DBInterface("sqlite3", sqlite3_filename=db_nickname)
os.environ["DB_NAME"] = db_nickname
else:
db_client = DBInterface(database_type, db_nickname=db_nickname)
os.environ["DB_NAME"] = db_nickname
db_client.connect()
while (received_command not in exit_command_list):
try:
if (received_command in command_keys) or re.search(r"save\s.+", received_command):
db_client.command_interface(received_command, command_buffer)
elif re.search(r"^column \w+(\.)*\w+\s?\|?$", received_command):
db_client.delay_column(received_command)
elif re.search(r"switch \w+$", received_command):
database_interface(database_type, received_command.split(" ")[1])
elif received_command == "webapp":
from app.webapp import app
app.run(host="127.0.0.1", port=5000)
else:
sql_regex = re.compile(r"^(?i)(CREATE|SELECT|UPDATE|INSERT|DELETE)$")
if sql_regex.match(received_command.split(" ")[0]):
db_client.delay_command(received_command)
else:
print("Not a valid SQL Expression!")
except Exception as error:
print(error)
if not (received_command in command_keys):
command_buffer = received_command
received_command = front_prompt(database_type, db_nickname)
#Clear the environment variable after the webapp
del os.environ["DB_NAME"]
del os.environ["DB_TYPE"]
def initial_interface() -> str:
command = front_prompt()
while command not in exit_command_list:
if re.search(r"^connect\s([\w\s])+\s?([\w\s])*$", command):
return command.split(" ")[1:]
else:
print("You are not connected to any registered DB")
command = front_prompt()
exit(1)