-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathcsv_overrides.py
More file actions
329 lines (271 loc) · 10.4 KB
/
Copy pathcsv_overrides.py
File metadata and controls
329 lines (271 loc) · 10.4 KB
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
from datetime import datetime
from pathlib import Path
from typing import Optional
from talon import Context, Module, actions, app, fs
from .conventions import get_cursorless_list_name
SPOKEN_FORM_HEADER = "Spoken form"
CURSORLESS_IDENTIFIER_HEADER = "Cursorless identifier"
mod = Module()
cursorless_settings_directory = mod.setting(
"cursorless_settings_directory",
type=str,
default="cursorless-settings",
desc="The directory to use for cursorless settings csvs relative to talon user directory",
)
def init_csv_and_watch_changes(
filename: str,
default_values: dict[str, dict],
extra_ignored_values: list[str] = None,
allow_unknown_values: bool = False,
default_list_name: Optional[str] = None,
headers: list[str] = [SPOKEN_FORM_HEADER, CURSORLESS_IDENTIFIER_HEADER],
ctx: Context = Context(),
no_update_file: bool = False,
):
"""
Initialize a cursorless settings csv, creating it if necessary, and watch
for changes to the csv. Talon lists will be generated based on the keys of
`default_values`. For example, if there is a key `foo`, there will be a
list created called `user.cursorless_foo` that will contain entries from
the original dict at the key `foo`, updated according to customization in
the csv at
actions.path.talon_user() / "cursorless-settings" / filename
Note that the settings directory location can be customized using the
`user.cursorless_settings_directory` setting.
Args:
filename (str): The name of the csv file to be placed in
`cursorles-settings` dir
default_values (dict[str, dict]): The default values for the lists to
be customized in the given csv
extra_ignored_values list[str]: Don't throw an exception if any of
these appear as values; just ignore them and don't add them to any list
allow_unknown_values bool: If unknown values appear, just put them in the list
default_list_name Optional[str]: If unknown values are allowed, put any
unknown values in this list
no_update_file Optional[bool]: Set this to `TRUE` to indicate that we should
not update the csv. This is used generally in case there was an issue coming up with the default set of values so we don't want to persist those to disk
"""
if extra_ignored_values is None:
extra_ignored_values = []
file_path = get_full_path(filename)
super_default_values = get_super_values(default_values)
file_path.parent.mkdir(parents=True, exist_ok=True)
def on_watch(path, flags):
if file_path.match(path):
current_values, has_errors = read_file(
file_path,
headers,
super_default_values.values(),
extra_ignored_values,
allow_unknown_values,
)
update_dicts(
default_values,
current_values,
extra_ignored_values,
allow_unknown_values,
default_list_name,
ctx,
)
fs.watch(file_path.parent, on_watch)
if file_path.is_file():
current_values = update_file(
file_path,
headers,
super_default_values,
extra_ignored_values,
allow_unknown_values,
no_update_file,
)
update_dicts(
default_values,
current_values,
extra_ignored_values,
allow_unknown_values,
default_list_name,
ctx,
)
else:
if not no_update_file:
create_file(file_path, headers, super_default_values)
update_dicts(
default_values,
super_default_values,
extra_ignored_values,
allow_unknown_values,
default_list_name,
ctx,
)
def unsubscribe():
fs.unwatch(file_path.parent, on_watch)
return unsubscribe
def is_removed(value: str):
return value.startswith("-")
def update_dicts(
default_values: dict[str, dict],
current_values: dict,
extra_ignored_values: list[str],
allow_unknown_values: bool,
default_list_name: Optional[str],
ctx: Context,
):
# Create map with all default values
results_map = {}
for list_name, dict in default_values.items():
for key, value in dict.items():
results_map[value] = {"key": key, "value": value, "list": list_name}
# Update result with current values
for key, value in current_values.items():
try:
results_map[value]["key"] = key
except KeyError:
if value in extra_ignored_values:
pass
elif allow_unknown_values:
results_map[value] = {
"key": key,
"value": value,
"list": default_list_name,
}
else:
raise
# Convert result map back to result list
results = {res["list"]: {} for res in results_map.values()}
for obj in results_map.values():
value = obj["value"]
key = obj["key"]
if not is_removed(key):
for k in key.split("|"):
if value == "pasteFromClipboard" and k.endswith(" to"):
# FIXME: This is a hack to work around the fact that the
# spoken form of the `pasteFromClipboard` action used to be
# "paste to", but now the spoken form is just "paste" and
# the two is part of the positional target. Users who had
# cursorless before this change would have "paste to" as
# their spoken form and so would need to say "paste to to".
k = k[:-3]
results[obj["list"]][k.strip()] = value
# Assign result to talon context list
for list_name, dict in results.items():
ctx.lists[get_cursorless_list_name(list_name)] = dict
def update_file(
path: Path,
headers: list[str],
default_values: dict,
extra_ignored_values: list[str],
allow_unknown_values: bool,
no_update_file: bool,
):
current_values, has_errors = read_file(
path,
headers,
default_values.values(),
extra_ignored_values,
allow_unknown_values,
)
current_identifiers = current_values.values()
missing = {}
for key, value in default_values.items():
if value not in current_identifiers:
missing[key] = value
if missing:
if has_errors or no_update_file:
print(
"NOTICE: New cursorless features detected, but refusing to update "
"csv due to errors. Please fix csv errors above and restart talon"
)
else:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
lines = [
f"# {timestamp} - New entries automatically added by cursorless",
*[create_line(key, missing[key]) for key in sorted(missing)],
]
with open(path, "a") as f:
f.write("\n\n" + "\n".join(lines))
print(f"New cursorless features added to {path.name}")
for key in sorted(missing):
print(f"{key}: {missing[key]}")
print(
"See release notes for more info: "
"https://github.com/cursorless-dev/cursorless/blob/main/CHANGELOG.md"
)
app.notify(f"🎉🎉 New cursorless features; see log")
return current_values
def create_line(*cells: str):
return ", ".join(cells)
def create_file(path: Path, headers: list[str], default_values: dict):
lines = [create_line(key, default_values[key]) for key in sorted(default_values)]
lines.insert(0, create_line(*headers))
lines.append("")
path.write_text("\n".join(lines))
def csv_error(path: Path, index: int, message: str, value: str):
"""Check that an expected condition is true
Note that we try to continue reading in this case so cursorless doesn't get bricked
Args:
path (Path): The path of the CSV (for error reporting)
index (int): The index into the file (for error reporting)
text (str): The text of the error message to report if condition is false
"""
print(f"ERROR: {path}:{index+1}: {message} '{value}'")
def read_file(
path: Path,
headers: list[str],
default_identifiers: list[str],
extra_ignored_values: list[str],
allow_unknown_values: bool,
):
with open(path) as f:
lines = list(f)
result = {}
used_identifiers = []
has_errors = False
seen_headers = False
expected_headers = create_line(*headers)
for i, raw_line in enumerate(lines):
line = raw_line.strip()
if not line or line.startswith("#"):
continue
if not seen_headers:
seen_headers = True
if line != expected_headers:
has_errors = True
csv_error(path, i, "Malformed header", line)
print(f"Expected '{expected_headers}'")
continue
parts = line.split(",")
if len(parts) != len(headers):
has_errors = True
csv_error(path, i, "Malformed csv entry", line)
continue
key = parts[0].strip()
value = parts[1].strip()
if (
value not in default_identifiers
and value not in extra_ignored_values
and not allow_unknown_values
):
has_errors = True
csv_error(path, i, "Unknown identifier", value)
continue
if value in used_identifiers:
has_errors = True
csv_error(path, i, "Duplicate identifier", value)
continue
result[key] = value
used_identifiers.append(value)
if has_errors:
app.notify("Cursorless settings error; see log")
return result, has_errors
def get_full_path(filename: str):
if not filename.endswith(".csv"):
filename = f"{filename}.csv"
user_dir = actions.path.talon_user()
settings_directory = Path(cursorless_settings_directory.get())
if not settings_directory.is_absolute():
settings_directory = user_dir / settings_directory
return (settings_directory / filename).resolve()
def get_super_values(values: dict[str, dict]):
result = {}
for dict in values.values():
result.update(dict)
return result