3
3
from collections .abc import Container
4
4
from datetime import datetime
5
5
from pathlib import Path
6
- from typing import Optional
6
+ from typing import Callable , Optional
7
7
8
8
from talon import Context , Module , actions , app , fs
9
9
32
32
"""
33
33
34
34
35
+ GetType = Callable [[str , str ], str ]
36
+
37
+
35
38
def init_csv_and_watch_changes (
36
39
filename : str ,
37
40
default_values : dict [str , dict [str , str ]],
@@ -42,6 +45,7 @@ def init_csv_and_watch_changes(
42
45
ctx : Context = Context (),
43
46
no_update_file : bool = False ,
44
47
pluralize_lists : list [str ] = [],
48
+ get_type : Optional [GetType ] = None ,
45
49
):
46
50
"""
47
51
Initialize a cursorless settings csv, creating it if necessary, and watch
@@ -106,6 +110,7 @@ def on_watch(path, flags):
106
110
default_list_name ,
107
111
pluralize_lists ,
108
112
output_file_path ,
113
+ get_type ,
109
114
ctx ,
110
115
)
111
116
@@ -128,6 +133,7 @@ def on_watch(path, flags):
128
133
default_list_name ,
129
134
pluralize_lists ,
130
135
output_file_path ,
136
+ get_type ,
131
137
ctx ,
132
138
)
133
139
else :
@@ -141,6 +147,7 @@ def on_watch(path, flags):
141
147
default_list_name ,
142
148
pluralize_lists ,
143
149
output_file_path ,
150
+ get_type ,
144
151
ctx ,
145
152
)
146
153
@@ -188,6 +195,7 @@ def update_dicts(
188
195
default_list_name : Optional [str ],
189
196
pluralize_lists : list [str ],
190
197
output_file_path : Path ,
198
+ get_type : Optional [GetType ],
191
199
ctx : Context ,
192
200
):
193
201
# Create map with all default values
@@ -214,29 +222,24 @@ def update_dicts(
214
222
215
223
# Convert result map back to result list
216
224
results = {res ["list" ]: {} for res in results_map .values ()}
217
- output_file_dict = {}
225
+ output_file_entries = []
218
226
for obj in results_map .values ():
219
227
value = obj ["value" ]
220
228
key = obj ["key" ]
221
- if not is_removed (key ):
222
- for k in key .split ("|" ):
223
- if value == "pasteFromClipboard" and k .endswith (" to" ):
224
- # FIXME: This is a hack to work around the fact that the
225
- # spoken form of the `pasteFromClipboard` action used to be
226
- # "paste to", but now the spoken form is just "paste" and
227
- # the "to" is part of the positional target. Users who had
228
- # cursorless before this change would have "paste to" as
229
- # their spoken form and so would need to say "paste to to".
230
- k = k [:- 3 ]
231
- results [obj ["list" ]][k .strip ()] = value
232
- output_file_dict [value ] = k .strip ()
229
+ spoken_forms = list (get_spoken_forms (value , key ))
230
+ for spoken_form in spoken_forms :
231
+ results [obj ["list" ]][spoken_form ] = value
232
+ if get_type is not None and (entry_type := get_type (obj ["list" ], value )):
233
+ output_file_entries .append (
234
+ {"type" : entry_type , "id" : value , "spokenForms" : spoken_forms }
235
+ )
233
236
234
237
# Assign result to talon context list
235
238
assign_lists_to_context (ctx , results , pluralize_lists )
236
239
237
240
with open (output_file_path , "w" ) as out :
238
241
try :
239
- out .write (json .dumps (output_file_dict ))
242
+ out .write (json .dumps ({ "version" : 0 , "entries" : output_file_entries } ))
240
243
except Exception :
241
244
error_message = f"Error writing spoken form json { output_file_path } "
242
245
print (error_message )
@@ -256,6 +259,21 @@ def assign_lists_to_context(
256
259
ctx .lists [list_plural_name ] = {pluralize (k ): v for k , v in dict .items ()}
257
260
258
261
262
+ def get_spoken_forms (id : str , spoken_form : str ):
263
+ if not is_removed (spoken_form ):
264
+ for k in spoken_form .split ("|" ):
265
+ if id == "pasteFromClipboard" and k .endswith (" to" ):
266
+ # FIXME: This is a hack to work around the fact that the
267
+ # spoken form of the `pasteFromClipboard` action used to be
268
+ # "paste to", but now the spoken form is just "paste" and
269
+ # the "to" is part of the positional target. Users who had
270
+ # cursorless before this change would have "paste to" as
271
+ # their spoken form and so would need to say "paste to to".
272
+ k = k [:- 3 ]
273
+
274
+ yield k .strip ()
275
+
276
+
259
277
def update_file (
260
278
path : Path ,
261
279
headers : list [str ],
0 commit comments