-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpython_voice_coding_plugin_caster_v1-0-0.py
421 lines (337 loc) · 15.9 KB
/
python_voice_coding_plugin_caster_v1-0-0.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
from dragonfly import (MappingRule, Choice, Dictation, Grammar, Repeat, StartApp, Function,RunCommand,FocusWindow)
from castervoice.lib import control
from castervoice.lib import settings
from castervoice.lib.actions import Key, Text
from castervoice.lib.context import AppContext
from castervoice.lib.merge.additions import IntegerRefST
from castervoice.lib.merge.state.short import R
from castervoice.lib.ctrl.mgr.rule_details import RuleDetails
#########################################################################################
import json
import os
import platform
import subprocess
#########################################################################################
local_settings = {
"show_command":False,
"force_rpc":False,
}
#########################################################################################
if settings.SETTINGS["miscellaneous"]["use_aenea"] or local_settings["force_rpc"]:
try:
import aenea
from jsonrpclib import ProtocolError
using_rpc = True
except:
using_rpc = False
else:
using_rpc = False
#########################################################################################
GRAMMAR_VERSION = (0,1,3)
#########################################################################################
def create_arguments(command,format,**kwargs):
p = {x:kwargs[x] for x in kwargs.keys() if x not in ['_node','_rule','_grammar']}
p["format"] = format
p["command"] = command
p["grammar_version"] = GRAMMAR_VERSION
return {"arg":p}
def validate_subl():
if platform.system() != 'Windows':
return "subl"
try:
subprocess.check_call(["subl", "-h"],stdout=subprocess.PIPE,stderr=subprocess.PIPE) # For testing purposes you can invalidate to trigger failure
return "subl"
except Exception as e:
try :
subprocess.check_call(["C:\\Program Files\\Sublime Text 3\\subl", "-h"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
print("Resorting to C:\\Program Files\\Sublime Text 3\\subl.exe")
return "C:\\Program Files\\Sublime Text 3\\subl"
except :
print("Sublime Text 3 `subl` executable was not in the Windows path")
if not os.path.isdir(r'C:\\Program Files\\Sublime Text 3'):
print("And there is no C:\\Program Files\\Sublime Text 3 directory to fall back to!")
else:
print("And it was not found under C:\\Program Files\\Sublime Text 3")
print("Please add `subl` to the path manually")
return "subl"
subl = validate_subl()
def send_sublime(c,data):
if local_settings["show_command"]:
print(c + " " + json.dumps(data))
RunCommand([subl,"-b", "--command",c + " " + json.dumps(data)],synchronous = True).execute()
def noob_send(command,format,**kwargs):
data = create_arguments(command,format,**kwargs)
if not using_rpc:
send_sublime("python_voice_coding_plugin", data)
else:
aenea.communications.server.python_voice_coding_plugin_aenea_send_sublime(c="python_voice_coding_plugin",data=data)
def lazy_value(c,f,**kwargs):
return R(Function(noob_send, command = c, format = f,**kwargs))
#########################################################################################
names={
"colors":{
"main":0,
"red":1,
"blue":2,
"green":3,
"yellow":4,
"orange":5,
},
"nth_ordinal_adjective":{
"first" : "first",
"second": "second",
"third": "third",
"fourth": "fourth",
"fifth": "fifth",
"sixth": "sixth",
"seventh": "seventh",
"eighth": "eighth",
"ninth":"ninth",
"last":"last",
"second last": "second last",
"third last": "third last",
"fourth last": "fourth last",
},
"vertical_direction":{
"(up|sauce|above)":"upwards",
"(down|dunce|below)":"downwards",
}
}
ARGUMENT_LIKE_INFORMATION = "(argument <argument_index>|keyword <keyword_index>|keyword value <keyword_value_index>|entire keyword <entire_keyword_index>|<caller> [<sub_index>]|entire call)"
#########################################################################################
class PythonVoiceCodingPluginRule(MappingRule):
mapping = {
"[smart] paste [<color>] back [with <surrounding_punctuation>]":
lazy_value("paste_back",1),
"[smart] paste <color> on <color2> [<color3> [ [and] <color4>]]":
lazy_value("paste_back",2),
# alternative rule
"smart alternative <alternative_index>":
lazy_value("alternative",1),
"smart <color>":
lazy_value("alternative",2),
"smart <color> [<color2> [<color3> [[and] <color4>]]]":
lazy_value("alternative",3),
"edit <color> [<color2> [<color3> [[and] <color4>]]]":
lazy_value("alternative",3,operation = "edit"),
# delete
"[smart] delete <color> [<color2> [<color3> [[and] <color4>]]]":
lazy_value("delete_alternatives",1),
# swap
"[smart] swap [<color>] back":
lazy_value("swap_back",1),
"[smart] swap <color> with <color2>":
lazy_value("swap_back",2),
# utilities
"[smart] back initial":
lazy_value("select_back",1),
"smart back":
lazy_value("select_back",2),
"smart here":
lazy_value("remember_here",1),
# sub indexing rule
"[(smart|<operation>)] [<nth>] part <sub_index>":
# "[(smart|<operation>)] [<nth>] inner [<nth2>] part <sub_index>":
lazy_value("select_part",1),
"[(smart|<operation>)] [<nth>] part <sub_index> until (<sub_index2>|the end)":
# "[(smart|<operation>)] [<nth>] inner [<nth2>] part <sub_index> until (<sub_index2>|the end)":
lazy_value("select_part",2),
"[(smart|<operation>)] ([<nth>] any|any <nth2>) part [<sub_index>]":
# "[(smart|<operation>)] [<nth>] any [<nth2>] part [<sub_index>]":
lazy_value("select_part",3),
"[(smart|<operation>)] ([<nth>] every|every <nth2>) part [<sub_index>]":
# "[(smart|<operation>)] [<nth>] every [<nth2>] part [<sub_index>]":
lazy_value("select_part",4),
# argument rule
"[(smart|<operation>)] [<nth>] " + ARGUMENT_LIKE_INFORMATION:
lazy_value("argument",1),
"[(smart|<operation>)] <vertical_direction> [<ndir>] [<nth>] " + ARGUMENT_LIKE_INFORMATION:
lazy_value("argument",2),
"[(smart|<operation>)] [<nth>] inside [<level_index>] " + ARGUMENT_LIKE_INFORMATION:
lazy_value("argument",3),
"[(smart|<operation>)] inside [<level_index>] <nth> " + ARGUMENT_LIKE_INFORMATION:
lazy_value("argument",4),
"[(smart|<operation>)] outer [<level_index>] [<nth>] " + ARGUMENT_LIKE_INFORMATION:
lazy_value("argument",5),
# big roi rule
"(smart|<operation>) <big_roi> [<sub_index>]":
lazy_value("big_roi",1),
"[(smart|<operation>)] <nth> <big_roi> [<sub_index>]":
lazy_value("big_roi",2),
"[(smart|<operation>)] <vertical_direction> [<ndir>] <big_roi> [<sub_index>]":
lazy_value("big_roi",3),
"[smart] <vertical_direction> [<ndir>] <block> [<nth>] <big_roi> [<sub_index>]":
lazy_value("big_roi",4),
# item rule
"[smart] item <item_index>":
lazy_value("insert_item",1),
"[smart] (item|items) (all| <item_index> until (<item_index2>| the end))":
lazy_value("insert_item",2),
"[smart] (item|items) <item_index> <item_index2> [ and <item_index3>]":
lazy_value("insert_item",3),
# collect rule
"[smart] collect <collectable>":
lazy_value("collect_indexable",1),
# variable rule
"[smart] variable <item_index> [[and] <item_index2> [and <item_index3>]]":
lazy_value("collect_variable",2),
"[smart] (variables all|variable <item_index> until (<item_index2>| the end))":
lazy_value("collect_variable",3),
# parameter rule
"[smart] [<vertical_direction> [<ndir>]] parameter <item_index> [and <item_index2> [and <item_index3>]]":
lazy_value("collect_parameter",2),
"[smart] [<vertical_direction> [<ndir>]] (parameters all| parameter <item_index> until (<item_index2>| the end))":
lazy_value("collect_parameter",3),
# "[smart] [<vertical_direction> [<ndir>]] key parameter <item_index> [ and <item_index2> [and <item_index3>]]":
# lazy_value("collect_parameter",2,experimental = "True"),
# "[smart] [<vertical_direction> [<ndir>]] key (parameters all| parameter <item_index> until (<item_index2>| the end))":
# lazy_value("collect_parameter",3,experimental = "True"),
}
extras = [
IntegerRefST("argument_index", 0, 10),
IntegerRefST("keyword_index", 1, 10),
IntegerRefST("entire_keyword_index", 1, 10),
IntegerRefST("keyword_value_index", 1, 10),
IntegerRefST("alternative_index", 1, 10),
IntegerRefST("ndir",1,20),
IntegerRefST("level_index",1,10),
IntegerRefST("paste_back_index",0,10),
IntegerRefST("collect_index",1,30),
IntegerRefST("item_index",1,30),
IntegerRefST("item_index2",1,30),
IntegerRefST("item_index3",1,30),
IntegerRefST("item_index4",1,30),
IntegerRefST("sub_index",1,10),
IntegerRefST("sub_index2",1,10),
Choice("nth",names["nth_ordinal_adjective"]),
Choice("nth2",names["nth_ordinal_adjective"]),
Choice("vertical_direction",names["vertical_direction"]),
Choice("color", names["colors"]),
Choice("color2", names["colors"]),
Choice("color3", names["colors"]),
Choice("color4", names["colors"]),
Choice("big_roi",{
"if condition" : "if condition",
"else if condition" : "else if condition",
"while condition" : "while condition",
"with item" : "with clause",
"exception":"exception",
"exception name":"exception name",
"handler":"handler",
"if expression condition" : "if expression condition",
"if expression value" : "if expression body",
"if expression":"if expression",
"if expression else" : "if expression else",
"comprehension condition" : "comprehension condition",
"comprehension value" : "comprehension value",
"return value" : "return value",
"pass":"pass",
"break" : "break",
"continue" : "continue",
"assertion message" : "assertion message",
"assertion condition" : "assertion condition",
"exception raised" : "exception raised",
"raised cause": "raised cause",
"(assignment right| right)" : "assignment right",
"(assignment left| left)" : "assignment left",
"assignment [full]" : "assignment full",
"(expression statement|expression)" : "expression statement",
"import statement":"import statement",
"import value" : "import value",
"module" : "import module",
"iterator" : "iterator",
"iterable" : "iterable",
"function name": "definition name",
"function parameter": "definition parameter",
"parameter list": "definition parameter list",
"default value": "default value",
"lambda":"lambda",
"lambda body":"lambda body",
"class name": "class name",
"decorator":"decorator",
"base class":"base class",
# "same" : "same",
# "string" : "string",
# "integer literal" : "integer literal",
# "dictionary" : "dictionary",
# "list" : "list",
# "tuple" : "tuple",
# "set" : "set",
# "subscript" : "subscript",
# "subscript body" : "subscript body",
# "key" : "key",
# "lower" : "lower",
# "upper" : "upper",
# "step" : "step",
# "attribute" : "attribute",
# "comparison" : "comparison",
# "arithmetic" : "arithmetic",
# "boolean" : "boolean",
# "member": "member",
# "container": "container",
# "membership" : "membership",
# "left side" : "left side",
# "right side" : "right side",
# "middle" : "middle",
# "arithmetic left" : "arithmetic left" ,
# "arithmetic right" : "arithmetic right",
# "arithmetic middle" : "arithmetic middle",
# "boolean left" : "boolean left",
# "boolean right" : "boolean right",
# "boolean middle" : "boolean middle",
# "boolean and" : "boolean and" ,
# "boolean or" : "boolean or",
}
),
Choice("block",{
"(function|functions)" :"function",
}
),
Choice("collectable",{
"(variable|variables)":"variable",
"( parameter | parameters)":"parameter",
"imported value":"import value",
"module" : "module",
"function (name|names)":"function name",
"class name" : "class name",
"decorator" : "decorator",
}
),
Choice("surrounding_punctuation",{
"quotes": ("quotes","quotes"),
"experiment":('"\t/','\n\n\n"&' + r'\s'),
"thin quotes": ("'","'"),
"tickris": ("`","`"),
"prekris": ("(",")"),
"brax": ("[","]"),
"curly": ("{","}"),
"angle": ("<",">"),
"dot":(".","."),
"underscore": ("_","_"),
"(comma|,)": (",",","),
"ace":(" "," "),
# "truth comprehension":"$$ = [x for x in $$ if x]",
# "force list": "$$ if isinstance($$,list) else [$$] ",
# "truth": "$$ = $$ if $$ else ",
# "key value":"quotes$$quotes:$$",
}
),
Choice("operation",{
"paste": "paste",
"delete":"delete",
"swap": "swap",
"edit": "edit",
}
),
Choice("caller",{
"caller": "caller",
}
),
]
defaults = {
"ndir":1,
"level_index":0,
}
#---------------------------------------------------------------------------
def get_rule():
return PythonVoiceCodingPluginRule, RuleDetails(name="python voice coding plugin", executable="sublime_text", title="Sublime Text")