-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
226 lines (194 loc) · 7.24 KB
/
main.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
import re
import pexpect
from prompt_toolkit.shortcuts import get_input
from prompt_toolkit.history import History
from prompt_toolkit.contrib.completers import WordCompleter
from pygments.lexers import IokeLexer
from pygments.style import Style
from pygments.token import Token
from pygments.styles.default import DefaultStyle
IOKE = '/usr/bin/env ioke'
IOKE_VOCAB = ['method'] # TODO
IOKE_PROMPT = 'iik> '
IOKE_DEBUG = r'dbg:([0-9]+?)([:]\w+?)?> '
IOKE_EVAL = '+> '
# TODO: colors
OUT_PROMPT = "Out [{}]: {}"
ioke_completer = WordCompleter(IOKE_VOCAB)
class ExitRepl(Exception):
pass
class DocumentStyle(Style):
styles = {
# completition menu
Token.Menu.Completions.Completion.Current: 'bg:#00aaaa #000000',
Token.Menu.Completions.Completion: 'bg:#008888 #ffffff',
Token.Menu.Completions.ProgressButton: 'bg:#003333',
Token.Menu.Completions.ProgressBar: 'bg:#00aaaa',
# prompt
Token.Prefix: '#884444',
Token.OpenBracket: '#00aa00',
Token.CloseBracket: '#00aa00',
Token.LineNum: '#BB0000',
Token.Collon: '#884444',
# condition
Token.ConditionPrefix: '#BB0000',
Token.Sharp: '#884444',
Token.ConditionNumber: '#BB0000',
Token.Separator: '#FFFFFF',
Token.ChooseRestart: '#FFFFFF underline',
# restart
Token.Restart: '#0000FF',
Token.ArgsArrow: '#FFFFFF',
}
styles.update(DefaultStyle.styles)
class IokeShell(object):
""" Provides comunication with external Ioke process.
"""
command_prompt = IOKE_PROMPT
debug_prompt = IOKE_DEBUG
prompts = [IOKE_PROMPT, IOKE_DEBUG]
last_input = ""
def __init__(self, ioke_command=IOKE, spawner=pexpect.spawnu):
self._process = None
self._spawner = spawner
self._ioke_command = ioke_command
def start(self):
self._process = self._spawner(self._ioke_command)
self._process.setecho(False)
@property
def current_prompt(self):
prompt = self._process.expect(self.prompts)
return self.prompts[prompt]
@property
def output(self):
evalued = ''
stdout = self._process.before
if IOKE_EVAL in stdout:
stdout, _, evalued = stdout.partition(IOKE_EVAL)
evalued = evalued.strip()
stdout = stdout.replace(self.last_input, "").strip()
return stdout, evalued
@property
def debug_level(self):
return self._process.match.groups()[0]
@property
def restart(self):
return self._process.match.groups()[1]
def execute(self, expression):
self.last_input = expression
self._process.sendline(expression)
# TODO:
# - Out [n]: prompt after restart with multiple returns
# - Repl banner
# - Vi/Emacs mode
# - Up Arrow History Search - https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/examples/up-arrow-partial-string-matching.py
# - System commands - https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/examples/system-prompt.py
# - Multiline mode - https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/examples/get-multiline-input.py
# - Keybinding for multiline mode - https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/examples/get-password-with-toggle-display-shortcut.py
# - Persistent history - https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/examples/persistent-history.py
# - Toolbar - https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/examples/bottom-toolbar.py
class Repl(object):
""" Main REPL implementation.
"""
def __init__(self):
self._ioke = IokeShell()
self._history = History()
self.prompt = None
self._input_text = None
self.line_num = 1
self.condition = False
self.debug_level = 1
self.restart = None
def _run_subprocess(self):
self._ioke.start()
def main_loop(self):
self._run_subprocess()
while True:
try:
self.process_io()
except ExitRepl:
break
print("Goodbye")
def get_in_prompt_tokens(self, cli):
return [
(Token.Prefix, 'In '),
(Token.OpenBracket, '['),
(Token.LineNum, str(self.line_num)),
(Token.CloseBracket, ']'),
(Token.Collon, ': '),
]
def get_condition_prompt_tokens(self, cli):
return [
(Token.ConditionPrefix, 'ERR! '),
(Token.Sharp, '#'),
(Token.ConditionNumber, str(self.debug_level)),
(Token.Separator, ' - '),
(Token.ChooseRestart, 'Choose a restart'),
(Token.Collon, ': ')
]
def get_restart_prompt_tokens(self, cli):
return [
(Token.Restart, ' {}'.format(self.restart)),
(Token.ArgArrow, ' => '),
]
def execute(self, text):
self._ioke.execute(text)
try:
return self._ioke.current_prompt
except pexpect.EOF as err:
raise ExitRepl(err)
def inc_line(self, n=1):
self.line_num += n
def process_io(self):
text = self._input_text
line_num = self.line_num
try:
if not self.condition:
text = get_input(
get_prompt_tokens=self.get_in_prompt_tokens, lexer=IokeLexer,
history=self._history, completer=ioke_completer, style=DocumentStyle)
else:
if self.restart is not None:
text = get_input(
get_prompt_tokens=self.get_restart_prompt_tokens,
style=DocumentStyle)
else:
text = get_input(
get_prompt_tokens=self.get_condition_prompt_tokens,
style=DocumentStyle)
self._input_text = text
if self.prompt is None:
self.prompt = self._ioke.current_prompt
except (EOFError, pexpect.EOF) as err:
raise ExitRepl(err)
except KeyboardInterrupt:
self._input_text = text = ""
print("**Keyboard interrupt**")
print("Enter ctrl-d to exit")
else:
if text:
self.prompt = prompt = self.execute(text)
if prompt == IOKE_PROMPT:
self.condition = False
printed, result = self._ioke.output
if printed:
print(printed)
if result != 'nil':
print(OUT_PROMPT.format(line_num, result))
print()
elif prompt == IOKE_DEBUG:
self.condition = True
options, _ = self._ioke.output
self.debug_level = self._ioke.debug_level
self.restart = self._ioke.restart
options = options.strip()
if options:
print(options)
# TODO: Format and colorise traceback/options
self.inc_line()
if __name__ == '__main__':
repl = Repl()
repl.main_loop()