-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocol.py
400 lines (306 loc) · 10.1 KB
/
protocol.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# -*- coding: utf-8 -*-
import logging
import os.path
import platform
from lisp import cons, lbool, llist, lstring, read_lisp, symbol, write_lisp
import ulisp
__all__ = ['SwankProtocol']
class SwankProtocol(object):
"""Swank Protocol implementation for Python.
The most important function here is the dispatch function that
takes care of parsing lisp data to detect the correct method to
call and its arguments. Once the appropiate method is called it
also takes care of converting to python result to a lisp
expression to be returned to the client.
All other functions part of the Swank protocol wont do any Lisp
conversion or parsing. All of them get what they need in python
code and return Python results.
The read_lisp and write_lisp take care of doing proper conversions
for all datatypes.
"""
def __init__(self, socket, locals=None, prompt="Python> "):
self.locals = locals or {}
self.package = None
self.thread = True
self.id = 0
self.socket = socket
self.prompt = prompt
def dispatch(self, data):
"""Parses an :emacs-rex command an returns lisp response."""
command, form, package, thread, rid = read_lisp(data)
self.package = package;
self.thread = thread;
self.id = rid;
# form is the lisp espression
fn = form[0];
args = form[1:]
method_name = fn.replace(":", "_").replace("-", "_")
logging.debug(method_name)
logging.debug(args)
for i, arg in enumerate(args):
if hasattr(arg, 'unquote'):
args[i] = arg.unquote()
try:
response = [
symbol(":return"),
{":ok": getattr(self, method_name)(*args)},
self.id
]
except Exception as e:
return write_lisp([
symbol(":debug"), 0, 1,
[e, False],
[],
[],
self.id
])
lisp_response = write_lisp(response)
header = "{0:06x}".format(len(lisp_response))
return header + lisp_response
def indentation_update(self):
response = [symbol(":indentation-update"), [
cons("def", 1),
cons("class", 1),
cons("if", 1),
cons("else", 1),
cons("while", 1),
cons("for", 1),
cons("try", 1),
cons("except", 1),
cons("finally", 1)
]]
lisp_response = write_lisp(response)
header = "{0:06x}".format(len(lisp_response))
return header + lisp_response
def swank_connection_info(self):
"""Return connection info available"""
machine = platform.machine().upper()
version = platform.python_version()
pid = os.getpid()
host, ipaddr = self.socket.getsockname()
return llist([
symbol(':return'), llist([
symbol(':ok'), llist([
symbol(':pid'), pid, symbol(':style'), lbool(False),
symbol(':encoding'), llist([
symbol(':coding-systems'), llist([
lstring('utf-8-unix'),
lstring('iso-latin-1-unix')
])
]),
symbol(':lisp-implementation'), llist([
symbol(':type'), lstring('ULISP'),
symbol(':name'), lstring('ulisp'),
symbol(':version'), lstring(version),
symbol(':program'), lbool(False),
]),
symbol(':machine'), llist([
symbol(':instance'), lstring("{0} [{1}]".format(host,ipaddr)),
symbol(':type'), lstring(machine),
symbol(':version'), lstring(machine)
]),
symbol(':package'), llist([
symbol(':name'), lstring('python'),
symbol(':prompt'), self.prompt
]),
symbol(':version'), lstring('2012-07-13')
])
]), self.id
])
def swank_buffer_first_change(self, filename):
return lbool(False)
def swank_eval(self, string):
"""Eval string"""
string = string.replace('\n', ' ') #feed ulisp a single line to make things easier
string = string.rstrip()
if not string.endswith(')'): #a terminating ) will cause evaluation, otherwise we need a newline
string += '\n'
logging.debug('Swank eval: %s', string)
ulisp.send(string) #send the expression
result = ''
ulisp.receive_line()
while True:
prefix = ulisp.receive_until_space()
if prefix[-2:] == '> ':
# check for lines before the prompt
result = [x for x in prefix[:-2].split('\r\n') if len(x) > 0][0]
logging.debug('Swank eval returns "%s"', result)
return result
result = prefix + ulisp.receive_line()
def swank_interactive_eval(self, string):
return self.swank_eval(string)
def swank_interactive_eval_region(self, string):
return self.swank_eval(string)
def swank_pprint_eval(self, string):
return self.swank_eval(string)
def swank_simple_completions(self, string, trash):
pass
# try:
# import readline
# except ImportError:
# return [[], string]
# else:
# import rlcompleter
# readline.set_completer(rlcompleter.Completer().complete)
# completions = []
# try:
# i = 0
# while True:
# res = readline.get_completer()(string, i)
# if not res: break
# i += 1
# completions.append(res)
# except NameError:
# pass
# newinput = os.path.commonprefix(completions)
# return [[completions], newinput]
def swank_apropos_list_for_emacs(self):
pass
def swank_backtrace(self):
pass
def swank_commit_edited_value(self):
pass
def swank_compile_file_for_emacs(self):
pass
def swank_compile_multiple_strings_for_emacs(self):
pass
def swank_compile_string_for_emacs(self):
pass
def swank_create_server(self):
pass
def swank_debug_nth_thread(self):
pass
def swank_debugger_info_for_emacs(self):
pass
def swank_default_directory(self):
pass
def swank_describe_definition_for_emacs(self):
pass
def swank_describe_function(self):
pass
def swank_describe_symbol(self):
pass
def swank_disassemble_form(self):
pass
def swank_documentation_symbol(self):
pass
def swank_eval_and_grab_output(self, string):
return self.swank_eval(string)
def swank_eval_string_in_frame(self):
pass
def swank_find_definitions_for_emacs(self):
pass
def swank_flow_control_test(self):
pass
def swank_frame_locals_and_catch_tags(self):
pass
def swank_frame_package_name(self):
pass
def swank_frame_source_location(self):
pass
def swank_init_inspector(self):
pass
def swank_init_presentations(self):
pass
def swank_inspect_current_condition(self):
pass
def swank_inspect_frame_var(self):
pass
def swank_inspect_in_frame(self):
pass
def swank_inspect_nth_part(self):
pass
def swank_inspector_eval(self):
pass
def swank_inspector_history(self):
pass
def swank_inspector_pop(self):
pass
def swank_inspector_range(self):
pass
def swank_inspector_reinspect(self):
pass
def swank_kill_nth_thread(self):
pass
def swank_list_all_package_names(self):
pass
def swank_load_file(self):
pass
def swank_pprint_eval_string_in_frame(self):
pass
def swank_pprint_inspector_part(self):
pass
def swank_profile_by_substring(self):
pass
def swank_profile_package(self):
pass
def swank_profile_reset(self):
pass
def swank_quit_inspector(self):
pass
def swank_quit_thread_browser(self):
pass
def swank_re_evaluate_defvar(self):
pass
def swank_restart_frame(self):
pass
def swank_set_default_directory(self):
pass
def swank_sldb_abort(self):
pass
def swank_sldb_break(self):
pass
def swank_sldb_break_on_return(self):
pass
def swank_sldb_break_with_default_debugger(self):
pass
def swank_sldb_disassemble(self):
pass
def swank_sldb_next(self):
pass
def swank_sldb_out(self):
pass
def swank_sldb_return_from_frame(self):
pass
def swank_sldb_step(self):
pass
def swank_start_server(self):
pass
def swank_swank_compiler_macroexpand(self):
pass
def swank_swank_expand(self):
pass
def swank_swank_format_string_expand(self):
pass
def swank_swank_macroexpand(self):
pass
def swank_swank_macroexpand_all(self):
pass
def swank_swank_require(self, arg):
pass
def swank_throw_to_toplevel(self):
pass
def swank_toggle_break_on_signals(self):
pass
def swank_toggle_debug_on_swank_error(self):
pass
def swank_undefine_function(self):
pass
def swank_unintern_symbol(self):
pass
def swank_unprofile_all(self):
pass
def swank_untrace_all(self):
pass
def swank_update_indentation_information(self):
pass
def swank_value_for_editing(self):
pass
def swank_xref(self):
pass
def swank_xrefs(self):
pass
def swank_operator_arglist(self, a, b):
pass
def swank_autodoc(self, arg1, arg2, arg3):
pass