-
Notifications
You must be signed in to change notification settings - Fork 6
/
commands.py
478 lines (362 loc) · 11.4 KB
/
commands.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
"""
Basic tools to convert back and forth between json expected or sent by the Lean
server and python objets. Mostly based on the TypeScript version.
Anything whose name contains Request is meant to be sent to the Lean server after
conversion by its to_json method.
Anything whose name contains Response is meant to be built from some json sent
by the Lean server by the parse_response function at the bottom of this file.
Everything else in this file are intermediate objects that will be contained in
response objects.
"""
from dataclasses import dataclass
from typing import Optional, List, NewType, ClassVar, Union, Type
from enum import Enum
import json
def dict_to_dataclass(cls, dic: dict):
dic = {k: dic[k] for k in cls.__dataclass_fields__ if k in dic}
return cls(**dic)
class Request:
command: ClassVar[str]
expect_response: ClassVar[bool]
def __post_init__(self):
self.seq_num = 0
def to_json(self) -> str:
dic = self.__dict__.copy()
dic['command'] = self.command
return json.dumps(dic)
class Response:
response: ClassVar[str]
@staticmethod
def parse_response(data: str) -> Union['AllMessagesResponse', 'CurrentTasksResponse', 'OkResponse', 'ErrorResponse']:
dic = json.loads(data)
response = dic.pop('response')
for cls in [AllMessagesResponse, CurrentTasksResponse, OkResponse, ErrorResponse]:
if response == cls.response: # type: ignore
return cls.from_dict(dic) # type: ignore
raise ValueError("Couldn't parse response string.")
Severity = Enum('Severity', 'information warning error')
@dataclass
class Message:
file_name: str
severity: Severity
caption: str
text: str
pos_line: int
pos_col: int
end_pos_line: Optional[int] = None
end_pos_col: Optional[int] = None
@classmethod
def from_dict(cls, dic):
dic['severity'] = getattr(Severity, dic['severity'])
return dict_to_dataclass(cls, dic)
@dataclass
class AllMessagesResponse(Response):
response = 'all_messages'
msgs: List[Message]
@classmethod
def from_dict(cls, dic):
return cls([Message.from_dict(msg) for msg in dic['msgs']])
@dataclass
class Task:
file_name: str
pos_line: int
pos_col: int
end_pos_line: int
end_pos_col: int
desc: str
@dataclass
class CurrentTasksResponse(Response):
response = 'current_tasks'
is_running: bool
tasks: List[Task]
cur_task: Optional[Task] = None
@classmethod
def from_dict(cls, dic):
dic['tasks'] = [dict_to_dataclass(Task, task) for task in dic.pop('tasks')]
return dict_to_dataclass(cls, dic)
@dataclass
class ErrorResponse(Response):
response = 'error'
message: str
seq_num: Optional[int] = None
@classmethod
def from_dict(cls, dic):
return dict_to_dataclass(cls, dic)
@dataclass
class CommandResponse(Response):
"""
Parent class for all 'ok' responses directly tied to a specific request.
"""
command: ClassVar[str]
response = 'ok'
seq_num: int
@classmethod
def from_dict(cls, dic):
return dict_to_dataclass(cls, dic)
@dataclass
class OkResponse(Response):
"""
Intermediate representation of a CommandResponse which can be constructed from the
JSON alone. It can later be converted to a CommandResponse.
"""
response = 'ok'
seq_num: int
data: dict
@classmethod
def from_dict(cls, dic) -> 'OkResponse':
return OkResponse(seq_num=dic['seq_num'], data=dic)
def to_command_response(self, command: str) -> CommandResponse:
response_types: List[Type[CommandResponse]] = [
CompleteResponse, InfoResponse, HoleCommandsResponse, SyncResponse,
SearchResponse, AllHoleCommandsResponse, HoleResponse, RoiResponse
]
for cls in response_types:
if cls.command == command:
self.data['seq_num'] = self.seq_num
return cls.from_dict(self.data)
raise ValueError("Couldn't parse response string.")
@dataclass
class SyncRequest(Request):
command = 'sync'
expect_response = True
file_name: str
content: Optional[str] = None
def to_json(self):
dic = self.__dict__.copy()
dic['command'] = 'sync'
if dic['content'] is None:
dic.pop('content')
return json.dumps(dic)
@dataclass
class SyncResponse(CommandResponse):
command = 'sync'
message: Optional[str] = None
@dataclass
class CompleteRequest(Request):
command = 'complete'
expect_response = True
file_name: str
line: int
column: int
skip_completions: bool = False
@dataclass
class Source:
line: Optional[int] = None
column: Optional[int] = None
file: Optional[str] = None
@dataclass
class CompletionCandidate:
text: str
type_: Optional[str] = None
tactic_params: Optional[str] = None
doc: Optional[str] = None
source: Optional[Source] = None
@classmethod
def from_dict(cls, dic):
dic['type_'] = dic.pop('type')
if 'source' in dic:
dic['source'] = dict_to_dataclass(Source, dic.pop('source'))
return dict_to_dataclass(cls, dic)
@dataclass
class CompleteResponse(CommandResponse):
command = 'complete'
prefix: Optional[str] = None
completions: Optional[List[CompletionCandidate]] = None
@classmethod
def from_dict(cls, dic):
if 'completions' in dic:
dic['completions'] = [CompletionCandidate.from_dict(cdt)
for cdt in dic.pop('completions')]
return dict_to_dataclass(cls, dic)
@dataclass
class InfoRequest(Request):
command = 'info'
expect_response = True
file_name: str
line: int
column: int
GoalState = NewType('GoalState', str)
@dataclass
class InfoRecord:
full_id: Optional[str] = None
text: Optional[str] = None
type_: Optional[str] = None
doc: Optional[str] = None
source: Optional[Source] = None
state: Optional[GoalState] = None
tactic_param_idx: Optional[int] = None
tactic_params: Optional[List[str]] = None
@classmethod
def from_dict(cls, dic):
if 'full-id' in dic:
dic['full_id'] = dic.pop('full-id')
if 'type' in dic:
dic['type_'] = dic.pop('type')
if 'source' in dic:
dic['source'] = dict_to_dataclass(Source, dic.pop('source'))
return dict_to_dataclass(cls, dic)
@dataclass
class InfoResponse(CommandResponse):
command = 'info'
record: Optional[InfoRecord] = None
@classmethod
def from_dict(cls, dic):
if 'record' in dic:
dic['record'] = InfoRecord.from_dict(dic.pop('record'))
return dict_to_dataclass(cls, dic)
@dataclass
class SearchRequest(Request):
command = 'search'
expect_response = True
query: str
@dataclass
class SearchItem:
text: str
type_: str
source: Optional[Source] = None
doc: Optional[str] = None
@classmethod
def from_dict(cls, dic):
dic['type_'] = dic.pop('type')
if 'source' in dic:
dic['source'] = dict_to_dataclass(Source, dic.pop('source'))
return dict_to_dataclass(cls, dic)
@dataclass
class SearchResponse(CommandResponse):
command = 'search'
results: List[SearchItem]
@classmethod
def from_dict(cls, dic):
dic['results'] = [SearchItem.from_dict(si)
for si in dic.pop('results')]
return dict_to_dataclass(cls, dic)
@dataclass
class HoleCommandsRequest(Request):
command = 'hole_commands'
expect_response = True
file_name: str
line: int
column: int
@dataclass
class HoleCommandAction:
name: str
description: str
@dataclass
class Position:
line: int
column: int
@dataclass
class HoleCommands:
file: str
start: Position
end: Position
results: List[HoleCommandAction]
@classmethod
def from_dict(cls, dic):
dic['results'] = [dict_to_dataclass(HoleCommandAction, hc)
for hc in dic.pop('results')]
dic['start'] = dict_to_dataclass(Position, dic.pop('start'))
dic['end'] = dict_to_dataclass(Position, dic.pop('end'))
return dict_to_dataclass(cls, dic)
@dataclass
class HoleCommandsResponse(CommandResponse):
command = 'hole_commands'
message: Optional[str] = None
file: Optional[str] = None
start: Optional[Position] = None
end: Optional[Position] = None
results: Optional[List[HoleCommandAction]] = None
@classmethod
def from_dict(cls, dic):
if 'results' in dic:
dic['results'] = [dict_to_dataclass(HoleCommandAction, hc)
for hc in dic.pop('results')]
dic['start'] = dict_to_dataclass(Position, dic.pop('start'))
dic['end'] = dict_to_dataclass(Position, dic.pop('end'))
return dict_to_dataclass(cls, dic)
@dataclass
class AllHoleCommandsRequest(Request):
command = 'all_hole_commands'
expect_response = True
file_name: str
@dataclass
class AllHoleCommandsResponse(CommandResponse):
command = 'all_hole_commands'
holes: List[HoleCommands]
@classmethod
def from_dict(cls, dic):
dic['holes'] = [HoleCommands.from_dict(hole)
for hole in dic.pop('holes')]
return dict_to_dataclass(cls, dic)
@dataclass
class HoleRequest(Request):
command = 'hole'
expect_response = True
file_name: str
line: int
column: int
action: str
@dataclass
class HoleReplacementAlternative:
code: str
description: str
@dataclass
class HoleReplacements:
file: str
start: Position
end: Position
alternatives: List[HoleReplacementAlternative]
@classmethod
def from_dict(cls, dic):
dic['alternatives'] = [dict_to_dataclass(HoleReplacementAlternative, alt)
for alt in dic.pop('alternatives')]
dic['start'] = dict_to_dataclass(Position, dic.pop('start'))
dic['end'] = dict_to_dataclass(Position, dic.pop('end'))
return dict_to_dataclass(cls, dic)
@dataclass
class HoleResponse(CommandResponse):
command = 'hole'
replacements: Optional[HoleReplacements] = None
message: Optional[str] = None
@classmethod
def from_dict(cls, dic):
if 'replacements' in dic:
dic['replacements'] = HoleReplacements.from_dict(
dic.pop('replacements'))
return dict_to_dataclass(cls, dic)
CheckingMode = Enum('CheckingMode',
'nothing visible-lines visible-lines-and-above visible-files open-files')
@dataclass
class RoiRange:
begin_line: int
end_line: int
@dataclass
class FileRoi:
file_name: str
ranges: List[RoiRange]
def to_dict(self):
return {'file_name': self.file_name,
'ranges': [rr.__dict__ for rr in self.ranges] }
@dataclass
class RoiRequest(Request):
command = 'roi'
expect_response = True
mode: CheckingMode
files: List[FileRoi]
def to_json(self) -> str:
dic = self.__dict__.copy()
dic['command'] = 'roi'
dic['mode'] = dic['mode'].name
dic['files'] = [fileroi.to_dict() for fileroi in dic['files']]
return json.dumps(dic)
@dataclass
class RoiResponse(CommandResponse):
command = 'roi'
@dataclass
class SleepRequest(Request):
command = 'sleep'
expect_response = False
@dataclass
class LongSleepRequest(Request):
command = 'long_sleep'
expect_response = False