-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
89 lines (81 loc) · 3.76 KB
/
Copy pathparser.py
File metadata and controls
89 lines (81 loc) · 3.76 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
from . import consts
from . import shell_utils
from . import python_utils
class shellParser():
def __init__(self):
self.pythonRunner = python_utils.pyRunner()
self.shellRunner = shell_utils.shellRunner()
def checkBashOrPython(self, user_in):
user_in = user_in.lstrip()
# echo
if user_in.split()[0] == consts.ECHO_CMD:
if len(user_in.split()) > 1:
cmd_split = user_in.split()
if cmd_split[1][0] == consts.PYTHON_VAR_DELIMETER:
return self.pythonRunner.get_var(cmd_split[1][1:])
else:
return self.shellRunner.get_bash_var(cmd_split[1][1:])
# Python var: '@'
elif user_in[0] == consts.PYTHON_VAR_DELIMETER:
if '$' in user_in:
user_in = self._replace_bash_vars(user_in)
if not user_in:
print("python input has a syntax error or env var does not exist")
return None
else:
user_in = user_in[1:]
return self.pythonRunner.run_python(user_in)
# Python single line: '>>>'
elif user_in.startswith(consts.PYTHON_SINGLE_LINE_INPUT_DELEMETER):
if '$' in user_in:
user_in = self._replace_bash_vars(user_in[2:])
print(user_in)
else:
user_in = user_in[3:]
return self.pythonRunner.run_python(user_in)
# Python multi line: '...'
elif user_in.startswith(consts.PYTHON_MULTI_LINE_INPUT_DELIMETER):
user_in = user_in[len(consts.PYTHON_MULTI_LINE_INPUT_DELIMETER):]
user_in = user_in[:-len(consts.PYTHON_MULTI_LINE_INPUT_DELIMETER)]
return self.pythonRunner.run_python(user_in)
# check for Python or bash scripts
elif user_in.startswith(consts.PYTHON_BASH_SCRIPT_DELIMETER):
return self.pythonRunner.run_py_script(self.script_formatter(user_in))
elif user_in.startswith(consts.SHELL_SCRIPT_DELIMETER) or user_in.startswith(consts.SHELL_SCRIPT_SH_DELIMETER) \
or user_in.startswith(consts.BASH_SCRIPT_DELIMETER):
return self.shellRunner.run_script(self.script_formatter(user_in))
# all other commands must be bash/shell
else:
return self.shellRunner.feed(user_in)
def _replace_bash_vars(self, user_in):
out, bash_var, emplace = '', '', True
for i in user_in:
if i == consts.BASH_VAR_DELIMETER:
if not emplace:
return None
emplace = False
elif emplace:
out += i
elif not emplace and (i == ',' or i == ';' or i == ')' or i == ':'):
bash_var = self.shellRunner.get_bash_var(bash_var.lstrip())
if not bash_var: return None
out += bash_var + i
emplace = True
elif not emplace:
bash_var += i
if not emplace:
return None
return out[1::]
def script_formatter(self, user_in):
input_split = user_in.rstrip().split(' ')
user_output = ''
for word in input_split:
# check if we need to switch out variable
if word != consts.PYTHON_BASH_SCRIPT_DELIMETER:
if word.startswith(consts.PYTHON_VAR_DELIMETER):
user_output += self.pythonRunner.get_var(word[1:]) + ' '
elif word.startswith(consts.BASH_VAR_DELIMETER):
user_output += self.shellRunner.get_bash_var(word[1:]) + ' '
else:
user_output += word + ' '
return user_output.rstrip()