1
1
import csv
2
+ import json
2
3
from collections .abc import Container
3
4
from datetime import datetime
4
5
from pathlib import Path
@@ -40,7 +41,7 @@ def init_csv_and_watch_changes(
40
41
headers : list [str ] = [SPOKEN_FORM_HEADER , CURSORLESS_IDENTIFIER_HEADER ],
41
42
ctx : Context = Context (),
42
43
no_update_file : bool = False ,
43
- pluralize_lists : Optional [ list [str ] ] = [],
44
+ pluralize_lists : list [str ] = [],
44
45
):
45
46
"""
46
47
Initialize a cursorless settings csv, creating it if necessary, and watch
@@ -73,13 +74,21 @@ def init_csv_and_watch_changes(
73
74
extra_ignored_values = []
74
75
75
76
file_path = get_full_path (filename )
77
+ output_file_path = get_output_path (filename )
76
78
super_default_values = get_super_values (default_values )
77
79
78
80
file_path .parent .mkdir (parents = True , exist_ok = True )
79
81
80
82
check_for_duplicates (filename , default_values )
81
83
create_default_vocabulary_dicts (default_values , pluralize_lists )
82
84
85
+ try :
86
+ output_file_path .parent .mkdir (parents = True , exist_ok = True )
87
+ except Exception :
88
+ error_message = f"Error creating spoken form dir { output_file_path .parent } "
89
+ print (error_message )
90
+ app .notify (error_message )
91
+
83
92
def on_watch (path , flags ):
84
93
if file_path .match (path ):
85
94
current_values , has_errors = read_file (
@@ -96,6 +105,7 @@ def on_watch(path, flags):
96
105
allow_unknown_values ,
97
106
default_list_name ,
98
107
pluralize_lists ,
108
+ output_file_path ,
99
109
ctx ,
100
110
)
101
111
@@ -117,6 +127,7 @@ def on_watch(path, flags):
117
127
allow_unknown_values ,
118
128
default_list_name ,
119
129
pluralize_lists ,
130
+ output_file_path ,
120
131
ctx ,
121
132
)
122
133
else :
@@ -129,6 +140,7 @@ def on_watch(path, flags):
129
140
allow_unknown_values ,
130
141
default_list_name ,
131
142
pluralize_lists ,
143
+ output_file_path ,
132
144
ctx ,
133
145
)
134
146
@@ -175,6 +187,7 @@ def update_dicts(
175
187
allow_unknown_values : bool ,
176
188
default_list_name : Optional [str ],
177
189
pluralize_lists : list [str ],
190
+ output_file_path : Path ,
178
191
ctx : Context ,
179
192
):
180
193
# Create map with all default values
@@ -201,6 +214,7 @@ def update_dicts(
201
214
202
215
# Convert result map back to result list
203
216
results = {res ["list" ]: {} for res in results_map .values ()}
217
+ output_file_dict = {}
204
218
for obj in results_map .values ():
205
219
value = obj ["value" ]
206
220
key = obj ["key" ]
@@ -215,15 +229,24 @@ def update_dicts(
215
229
# their spoken form and so would need to say "paste to to".
216
230
k = k [:- 3 ]
217
231
results [obj ["list" ]][k .strip ()] = value
232
+ output_file_dict [value ] = k .strip ()
218
233
219
234
# Assign result to talon context list
220
235
assign_lists_to_context (ctx , results , pluralize_lists )
221
236
237
+ with open (output_file_path , "w" ) as out :
238
+ try :
239
+ out .write (json .dumps (output_file_dict ))
240
+ except Exception :
241
+ error_message = f"Error writing spoken form json { output_file_path } "
242
+ print (error_message )
243
+ app .notify (error_message )
244
+
222
245
223
246
def assign_lists_to_context (
224
247
ctx : Context ,
225
248
results : dict ,
226
- pluralize_lists : list [str ],
249
+ pluralize_lists : Optional [ list [str ] ],
227
250
):
228
251
for list_name , dict in results .items ():
229
252
list_singular_name = get_cursorless_list_name (list_name )
@@ -373,17 +396,24 @@ def read_file(
373
396
return result , has_errors
374
397
375
398
376
- def get_full_path (filename : str ):
377
- if not filename .endswith (".csv" ):
378
- filename = f"{ filename } .csv"
399
+ def get_full_path (output_file_path : str ):
400
+ if not output_file_path .endswith (".csv" ):
401
+ output_file_path = f"{ output_file_path } .csv"
379
402
380
403
user_dir : Path = actions .path .talon_user ()
381
404
settings_directory = Path (cursorless_settings_directory .get ())
382
405
383
406
if not settings_directory .is_absolute ():
384
407
settings_directory = user_dir / settings_directory
385
408
386
- return (settings_directory / filename ).resolve ()
409
+ return (settings_directory / output_file_path ).resolve ()
410
+
411
+
412
+ def get_output_path (output_file_path : str ):
413
+ if output_file_path .endswith (".csv" ):
414
+ output_file_path = output_file_path [:- 4 ]
415
+
416
+ return Path .home () / ".cursorless" / "spokenForms" / f"{ output_file_path } .json"
387
417
388
418
389
419
def get_super_values (values : dict [str , dict [str , str ]]):
0 commit comments