11import csv
2+ import json
23from collections .abc import Container
34from datetime import datetime
45from pathlib import Path
@@ -40,7 +41,7 @@ def init_csv_and_watch_changes(
4041 headers : list [str ] = [SPOKEN_FORM_HEADER , CURSORLESS_IDENTIFIER_HEADER ],
4142 ctx : Context = Context (),
4243 no_update_file : bool = False ,
43- pluralize_lists : Optional [ list [str ] ] = [],
44+ pluralize_lists : list [str ] = [],
4445):
4546 """
4647 Initialize a cursorless settings csv, creating it if necessary, and watch
@@ -73,13 +74,21 @@ def init_csv_and_watch_changes(
7374 extra_ignored_values = []
7475
7576 file_path = get_full_path (filename )
77+ output_file_path = get_output_path (filename )
7678 super_default_values = get_super_values (default_values )
7779
7880 file_path .parent .mkdir (parents = True , exist_ok = True )
7981
8082 check_for_duplicates (filename , default_values )
8183 create_default_vocabulary_dicts (default_values , pluralize_lists )
8284
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+
8392 def on_watch (path , flags ):
8493 if file_path .match (path ):
8594 current_values , has_errors = read_file (
@@ -96,6 +105,7 @@ def on_watch(path, flags):
96105 allow_unknown_values ,
97106 default_list_name ,
98107 pluralize_lists ,
108+ output_file_path ,
99109 ctx ,
100110 )
101111
@@ -117,6 +127,7 @@ def on_watch(path, flags):
117127 allow_unknown_values ,
118128 default_list_name ,
119129 pluralize_lists ,
130+ output_file_path ,
120131 ctx ,
121132 )
122133 else :
@@ -129,6 +140,7 @@ def on_watch(path, flags):
129140 allow_unknown_values ,
130141 default_list_name ,
131142 pluralize_lists ,
143+ output_file_path ,
132144 ctx ,
133145 )
134146
@@ -175,6 +187,7 @@ def update_dicts(
175187 allow_unknown_values : bool ,
176188 default_list_name : Optional [str ],
177189 pluralize_lists : list [str ],
190+ output_file_path : Path ,
178191 ctx : Context ,
179192):
180193 # Create map with all default values
@@ -201,6 +214,7 @@ def update_dicts(
201214
202215 # Convert result map back to result list
203216 results = {res ["list" ]: {} for res in results_map .values ()}
217+ output_file_dict = {}
204218 for obj in results_map .values ():
205219 value = obj ["value" ]
206220 key = obj ["key" ]
@@ -215,15 +229,24 @@ def update_dicts(
215229 # their spoken form and so would need to say "paste to to".
216230 k = k [:- 3 ]
217231 results [obj ["list" ]][k .strip ()] = value
232+ output_file_dict [value ] = k .strip ()
218233
219234 # Assign result to talon context list
220235 assign_lists_to_context (ctx , results , pluralize_lists )
221236
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+
222245
223246def assign_lists_to_context (
224247 ctx : Context ,
225248 results : dict ,
226- pluralize_lists : list [str ],
249+ pluralize_lists : Optional [ list [str ] ],
227250):
228251 for list_name , dict in results .items ():
229252 list_singular_name = get_cursorless_list_name (list_name )
@@ -373,17 +396,24 @@ def read_file(
373396 return result , has_errors
374397
375398
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"
379402
380403 user_dir : Path = actions .path .talon_user ()
381404 settings_directory = Path (cursorless_settings_directory .get ())
382405
383406 if not settings_directory .is_absolute ():
384407 settings_directory = user_dir / settings_directory
385408
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"
387417
388418
389419def get_super_values (values : dict [str , dict [str , str ]]):
0 commit comments