/
Parser.py
142 lines (127 loc) · 4.46 KB
/
Parser.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
# David Crawford - Paraphrast
import master_dict
import os
import subprocess
import socket
import shlex
from prompt_toolkit.shortcuts import get_input
completer = None
windows_supressor = "2>nul"
env = 2
cwd = ""
header = ""
slash = "\\"
output_cmd = []
def Prompt():
cmd = ""
try:
cmd = get_input(header, completer=completer)
except EOFError:
return
except KeyboardInterrupt:
exit()
except Exception as e:
print("%s%s" % (header, e))
Parse(cmd)
def Parse(cmd):
global slash, output_cmd
output_cmd[:] = []
# Format header of each line. Repeats if nothing was entered
# TODO Change header based on current directory
while not cmd:
cmd = input(header)
# Seperate our user input into a list for easy calculations
# Needs try/catch because we don't want to crash if missing end quotes
try:
cmd = shlex.split(cmd)
except Exception as e:
print("%s%s" % (header, e))
return
# Checks if it's a one word statement and simply runs off of primary if so. Saves some time
if len(cmd) < 2:
# TODO calculate one line special cases in a seperate location
if cmd[0] == "cd..":
MoveUpCd()
return
for command, translated_command in master_dict.primary.items():
if command == cmd[0]:
# TODO Make OS check happen once and trigger everything automatically
if env == 2:
RunCommand("%s %s" % (translated_command, windows_supressor))
else:
RunCommand(translated_command)
return
# Otherwise we're going so start doing the more complicated work...
# Searches for the main command
for command, command_keys in master_dict.secondary.items():
if command == cmd[0]:
# Output the translated primary
output_cmd.append(master_dict.primary.get(command))
# Now we begin working through the parameters
for param in cmd[1:]:
# If our modifier is in the modifier list, then we want the corresponding modifier from its translation
if param in command_keys:
index = command_keys.index(param)
val = master_dict.secondary.get(output_cmd[0])
output_cmd.append(val[index])
# If it's not a valid parameter, it might be a special command
elif Special(cmd[0], param) == 0:
return
else:
output_cmd.append(param)
# Adds the supress error message argument
if env == 2:
output_cmd.append(windows_supressor)
RunCommand(output_cmd)
return
print("%s%s" % (header, "command not found"))
def Special(prim, sec):
for v, k in master_dict.special.items():
if prim == v:
try:
# Dynamically call the function indicated in our special dictionary
globals()[k](sec)
return 0
except Exception as e:
return 1
def RunCd(path):
global slash
# TODO continue to test that this is working properly
if path not in "..":
MoveDownCd(path)
else:
MoveUpCd()
def MoveDownCd(path):
global cwd, slash
cwd = cwd + slash + path
os.chdir(cwd)
def MoveUpCd():
global cwd, slash
path_temp = cwd.split(slash)
path_temp.pop()
cwd = slash.join(path_temp)
os.chdir(cwd)
# After everything has been properly assembled, we run it
def RunCommand(cmd):
try:
# TODO Find working way to use shell=False on windows
proc = subprocess.Popen(cmd, shell=True) if env == 2 else subprocess.Popen(cmd, shell=False)
proc.wait()
return True
except Exception as e:
print(e)
return False
# One time basic OS and user information checks to simulate the Unix terminal experience
def Awake():
global env, cwd, header, slash, completer
if (os.name == "posix"):
# Swaps the primary values
master_dict.primary = {v: k for k, v in master_dict.primary.items()}
env = 1
slash = "/"
#This is called after the flip so that the auto completer is accessing the latest dictionary
from Auto_Complete import SystemCompleter
completer = SystemCompleter()
cwd = os.getcwd()
machine_name = socket.gethostname().split(".")
header = "%s:~ %s$ " % (machine_name[0], os.getlogin())