-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
403 lines (290 loc) · 9.77 KB
/
utilities.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import os
from ai import ai
from queries import *
from usage import usage
from Constants import *
from details import details
from dotenv import load_dotenv
from colorama import init
from Levenshtein import distance
from format import *
from typing import Tuple
import requests
def setup() -> Tuple | Tuple[None, None]:
"""
setup() initializes colorama, loads enviornment variables,
and attempts to create a connection to postgresql server.
If the attempt to connect to the postgresql server fails,
a tuble of None, None is returned. setup() then creates
the table in the db. setup() then clears the console and
returns the conn and cur objects.
Parameters:
None
Returns:
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
"""
init()
load_dotenv()
conn, cur=create_connection()
if conn==None and cur==None:
return None, None
create_table(conn, cur)
os.system(CLR)
return conn, cur
def split_cmds(cmds: str) -> list[str]:
"""
split_cmds() replaces any instances of
the text `&&` with `;` and returns a
list of parsed commands split on the
occurrences of `;`.
Parameters:
cmds: string (The parsed string of commands)
Returns:
cmds.split(";"): list[str] (The list of parsed commands)
"""
if cmds.find("&&") != -1:
cmds=cmds.replace("&&", ";")
return cmds.split(";")
def parse_ai_flags(msg: str, ai_cmd: str) -> str | None:
"""
parse_ai_flags() attempts to parse
the ai flag that can either be:
f [filename] -> Specify the filename
to parse and append to prompt message.
or
l [link] -> Specify the link of a webpage
to parse and append to the prompt message.
Then attempts to add the contents of
the file or url with the prompt
message.
Parameters:
msg: string (The ai flags with prompt message asked to the target ai)
msg: string (The target ai)
Returns:
new_msg: string (The prompt message with the specified file or url contents)
or
None
"""
cmd_args = msg.split(" ")
file_contents = ""
url_contents = ""
first_arg_found = False
first_arg = cmd_args[0]
first_arg_filename = ""
first_arg_url = ""
if first_arg == FLE[0] or first_arg == FLE:
if len(cmd_args) < 2:
usage(usage=AI, ai=ai_cmd)
return
first_arg_found = True
first_arg_filename = cmd_args[1]
try:
with open(first_arg_filename, 'r') as f:
file_contents = f.read()
except:
usage(usage=FLE, file_not_found=first_arg_filename)
return
if first_arg == LNK[0] or first_arg == LNK:
if len(cmd_args) < 2:
usage(usage=AI, ai=ai_cmd)
return
first_arg_found = True
first_arg_url = cmd_args[1]
if not first_arg_url.startswith("https://"):
first_arg_url = "https://" + first_arg_url
try:
response = requests.get(first_arg_url, timeout=STANDARD_TIMEOUT)
response.raise_for_status()
url_contents = response.text
except:
usage(usage=LNK, link_not_found=first_arg_url)
return
if first_arg_found:
cmd_args = cmd_args[2:]
if len(cmd_args) == 0:
usage(usage=AI, ai=ai_cmd)
return
new_msg = " ".join(cmd_args)
if file_contents:
new_msg += f" Filename: {first_arg_filename}, File Contents: {file_contents}"
if url_contents:
new_msg += f" Url: {first_arg_url}, Url Contents: {url_contents}"
return new_msg
def process_ai_cmd(conn, cur, msg: str, ai_cmd: str) -> None:
"""
process_ai_cmd() calls usage() with the
flag of AI constant column string and
the target ai if no prompt message is
passed. Otherwise calls parse_ai_flags()
and ai() with the parsed prompt message
and target ai.
Parameters:
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
msg: string (The prompt message asked to the target ai)
ai_cmd: string (The target ai)
Returns:
None
"""
if msg=="":
usage(usage=AI, ai=ai_cmd)
return
new_msg = parse_ai_flags(msg, ai_cmd)
if new_msg == None:
return
ai(ai_cmd, conn, cur, msg.strip(), new_msg.strip())
def process_details_cmd(cur, cmd: str) -> None:
"""
process_details_cmd() confirms that valid
flags are passed to details command. Otherwise
process_details_cmd() attempts to determine
which flag is passed and envoke target logic.
If passed in parsed flags do not match stored
flags for details command, then the stored
command that is the closest in terms of
insertions, deletions, and replacements is
returned. Otherwise process_details_cmd()
calls usage() with the parsed flag.
Parameters:
cur: cursor (The cursor object returned from `conn.cursor()`)
cmd: string (The parsed details command)
Returns:
None
"""
if cmd=="":
usage(usage=DTS)
return
if cmd.isnumeric():
details(cur, cmd)
return
processed_cmd=cmd.split(" ")[0].lower()
if len(processed_cmd)==1:
for std_cmd in STD_FLGS_1ST_CHAR:
if std_cmd == processed_cmd:
details(cur, cmd)
return
usage(usage=DTS)
else:
min_dist=4
process_details_cmd=""
for std_cmd in STD_FLGS:
dist=distance(processed_cmd, std_cmd)
if dist <= min_dist:
min_dist=dist
process_details_cmd=std_cmd
if min_dist==0:
if processed_cmd==AI:
details(cur, cmd)
return
if processed_cmd==DTE:
processed_cmd=processed_cmd[:len(processed_cmd)]
processed_cmd="d" + processed_cmd
details(cur, processed_cmd)
return
details(cur, processed_cmd[:1])
return
if min_dist < 4:
usage(usage=PRB, probable_command=f"{process_details_cmd}")
return
usage(usage=DTS)
return
def exec_cmd(conn, cur, cmds: str) -> int:
"""
exec_cmd() attempts to parse the passed in
command and confirms if the parsed command
is valid. If true, exec_cmd() envokes
target logic to execute parsed command.
Otherwise calls usage() with the
corresponding parsed command and flags.
If a parsed command does not match to a
stored command, the closest matching
command is passed to usage(). If the
`exit` command is parsed, the variable
`exit_code` is set to 1 and returned.
Otherwise exec_cmd() returns a 0.
Parameters:
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
cmds: string (The parsed command)
Returns:
exit_code: integer (Either a 1 or 0 for whether the `exit` command is parsed)
"""
exit_code=0
for cmd in cmds:
cmd=cmd.strip()
processed_cmd=cmd.split(" ")[0].lower()
if processed_cmd=="":
continue
elif processed_cmd==EXT[0]:
exit_code=1
break
elif processed_cmd==WLC[0]:
usage(usage=WLC)
continue
elif processed_cmd==HLP[0]:
usage(usage=HLP)
continue
elif cmd==CLR[0]:
os.system(CLR)
continue
elif processed_cmd==GPT[0]:
process_ai_cmd(conn, cur, cmd[1:].strip(), GPT)
continue
elif processed_cmd==PER[0]:
process_ai_cmd(conn, cur, cmd[1:].strip(), PER)
continue
elif processed_cmd==LGT[0]:
process_ai_cmd(conn, cur, cmd[1:].strip(), LMA)
continue
elif processed_cmd==DTS[0]:
process_details_cmd(cur, cmd[1:].strip())
continue
elif processed_cmd=="cd":
try:
os.chdir(cmd[3:].strip())
continue
except:
usage(usage=FLE, file_not_found=cmd[3:].strip())
continue
elif processed_cmd=="ls":
os.system("ls")
continue
elif processed_cmd=="pwd":
os.system("pwd")
continue
if len(processed_cmd) < 4:
usage(command_not_found=processed_cmd)
break
min_dist=4
probable_command=""
for std_cmd in STD_CMDS:
dist=distance(processed_cmd, std_cmd)
if dist <= min_dist:
min_dist=dist
probable_command=std_cmd
if probable_command==EXT and min_dist==0:
exit_code=1
break
if min_dist==0:
if probable_command==CLR:
os.system(CLR)
elif probable_command==HLP:
usage(usage=HLP)
elif probable_command==WLC:
usage(usage=WLC)
elif probable_command==GPT:
process_ai_cmd(conn, cur, cmd[GPT_LEN:].strip(), GPT)
elif probable_command==PER:
process_ai_cmd(conn, cur, cmd[PER_LEN:].strip(), PER)
elif probable_command==LMA:
process_ai_cmd(conn, cur, cmd[LMA_LEN:].strip(), LMA)
elif probable_command==DTS:
process_details_cmd(cur, cmd[DTS_LEN:].strip())
elif min_dist==4:
usage(command_not_found=f"{processed_cmd}")
else:
usage(usage=PRB, probable_command=f"{probable_command}")
if exit_code:
usage(usage=EXT)
return exit_code