-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
66 lines (55 loc) · 1.51 KB
/
utils.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
import re
import os
def inv_opt(func,opt):
print(func,": invalid option -- \'"+opt+'\'')
def log_cmd(str,hf_path,count):
if(count < 1000):
hf = open(hf_path,'r+')
hf.seek(0,2)
hf.write(str)
hf.write("\n")
count = count + 1
hf.close()
else:
hf = open(hf_path,'r')
lines = hf.readlines()
hf.close()
hf = open(hf_path,'w')
for i in range(len(lines)-1):
hf.write(lines[i+1])
hf.write(str)
hf.write("\n")
hf.close()
return count
def pre_process_cmd(str):
sm_temp_opts = re.findall(' -\w+',str)
sm_temp_opts = [f[1:] for f in sm_temp_opts]
small_opts = []
for opt in sm_temp_opts:
small_opts = small_opts + [opt[1:]]
bg_temp_opts = re.findall(' --\w+',str)
bg_temp_opts = [f[1:] for f in bg_temp_opts]
big_opts = []
for opt in bg_temp_opts:
big_opts = big_opts + [opt[2:]]
opts = []
for opt in small_opts:
opts = opts + list(opt)
opts = opts + big_opts
words = str.split(" ")
cmd = words[0]
args = []
for word in words:
if word not in sm_temp_opts+bg_temp_opts+[cmd]:
args = args+[word]
if len(args)==0:
args = ['']
return cmd,opts,args
def initalize_history():
hf_path = os.path.join(os.path.join(os.getcwd(),'logs'),'history.txt')
hf = open(hf_path,'r+')
count = 0
for line in hf:
count = count + 1
hf.close()
return hf_path,count