-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
140 lines (120 loc) · 4.6 KB
/
Copy pathrunner.py
File metadata and controls
140 lines (120 loc) · 4.6 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
import os
import cmd
import readline
import sys
from . import consts, parser
from sultan.api import Sultan
from termcolor import colored, cprint
def display_welcome():
cprint(' Welcome to pyshell!\n Useage:', 'cyan', attrs=['bold'])
cprint(' Echo python variable with character @', 'cyan', attrs=['bold'])
cprint(" Write single-line python code start with '>>>', if it is in multi-line start with '...'\n", 'cyan',
attrs=['bold'])
def get_prefix():
prefix = os.getcwd()
if 'VIRTUAL_ENV' in os.environ:
prefix = '(' + os.environ.get('VIRTUAL_ENV').split('/')[-1] + ')' + prefix
return prefix + ': '
def main():
display_welcome()
cmd = CmdParse(parser.shellParser())
cmd.cmdloop()
class CmdParse(cmd.Cmd):
def __init__(self, shell_parser):
super().__init__(completekey='tab', stdin=None, stdout=None)
self.shell_parser = shell_parser
self.multi_line = False
self.line_buffer = ''
prompt = colored(get_prefix(), 'cyan')
def do_exit(self, arg):
if not self.multi_line:
sys.exit(0)
def default(self, line):
if self.multi_line:
self.line_buffer += line + "\n"
else:
output = self.shell_parser.checkBashOrPython(line)
if output != None and output != 0 and output != 1:
print(output)
self.prompt = colored(get_prefix(), 'cyan')
def do_py(self, arg):
if self.multi_line:
self.line_buffer = '...' + self.line_buffer + '...'
self.shell_parser.checkBashOrPython(self.line_buffer)
self.multi_line = False
self.prompt = colored(get_prefix(), 'cyan')
self.line_buffer = ''
readline.parse_and_bind("tab: complete")
return
self.multi_line = True
self.old_completer = readline.get_completer()
readline.parse_and_bind("tab: tab-insert")
self.prompt = colored("python mode >>>", 'green', attrs=['bold'])
def parseline(self, line):
"""Parse the line into a command name and a string containing
the arguments. Returns a tuple containing (command, args, line).
'command' and 'args' may be None if the line couldn't be parsed.
"""
if not line:
return None, None, line
elif line[0] == '?':
line = 'help ' + line[1:]
elif line[0] == '!':
if hasattr(self, 'do_shell'):
line = 'shell ' + line[1:]
else:
return None, None, line
i, n = 0, len(line)
while i < n and line[i] in self.identchars: i = i+1
cmd, arg = line[:i], line[i:].strip()
return cmd, arg, line
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(None)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
if __name__ == "__main__":
main()