Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 24, 2023
1 parent 12d6bbb commit 5a92278
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
9 changes: 6 additions & 3 deletions core/create_spoken_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
EXPLODE_MAX_LEN = 3
FANCY_REGULAR_EXPRESSION = r"[A-Z]?[a-z]+|[A-Z]+(?![a-z])|[0-9]+"
SYMBOLS_REGEX = "|".join(re.escape(symbol) for symbol in set(symbol_key_words.values()))
FILE_EXTENSIONS_REGEX = r'^\b$'
FILE_EXTENSIONS_REGEX = r"^\b$"
file_extensions = {}


def update_regex():
global REGEX_NO_SYMBOLS
global REGEX_WITH_SYMBOLS
Expand All @@ -35,19 +36,21 @@ def update_regex():
"|".join([FANCY_REGULAR_EXPRESSION, FILE_EXTENSIONS_REGEX, SYMBOLS_REGEX])
)


update_regex()


@track_csv_list("file_extensions.csv", headers=("File extension", "Name"))
def on_extensions(values):
global FILE_EXTENSIONS_REGEX
global file_extensions
file_extensions = values
FILE_EXTENSIONS_REGEX = "|".join(
re.escape(file_extension.strip()) + "$"
for file_extension in values.values()
re.escape(file_extension.strip()) + "$" for file_extension in values.values()
)
update_regex()


REVERSE_PRONUNCIATION_MAP = {
**{str(value): key for key, value in digits_map.items()},
**{value: key for key, value in symbol_key_words.items()},
Expand Down
7 changes: 6 additions & 1 deletion core/file_extension/file_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@

ctx = Context()

@track_csv_list("file_extensions.csv", headers=("File extension", "Name"), default=_file_extensions_defaults)

@track_csv_list(
"file_extensions.csv",
headers=("File extension", "Name"),
default=_file_extensions_defaults,
)
def on_update(values):
ctx.lists["self.file_extension"] = values
4 changes: 3 additions & 1 deletion core/system_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def on_ready():

default_system_paths.update(onedrive_paths)

@track_csv_list("system_paths.csv", headers=("Path", "Spoken"), default=default_system_paths)
@track_csv_list(
"system_paths.csv", headers=("Path", "Spoken"), default=default_system_paths
)
def on_csv(system_paths):
ctx.lists["user.system_paths"] = system_paths

Expand Down
19 changes: 14 additions & 5 deletions core/user_settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
from typing import Callable, IO
import csv
import os
from pathlib import Path
from typing import IO, Callable

from talon import resource

Expand All @@ -10,9 +10,10 @@
SETTINGS_DIR = Path(__file__).parents[1] / "settings"
SETTINGS_DIR.mkdir(exist_ok=True)

CallbackT = Callable[[dict[str, str]], None]
CallbackT = Callable[[dict[str, str]], None]
DecoratorT = Callable[[CallbackT], CallbackT]


def read_csv_list(f: IO, headers: tuple[str, str]) -> dict[str, str]:
rows = list(csv.reader(f))

Expand Down Expand Up @@ -44,15 +45,21 @@ def read_csv_list(f: IO, headers: tuple[str, str]) -> dict[str, str]:

return mapping

def write_csv_defaults(path: Path, headers: tuple[str, str], default: dict[str, str]=None) -> None:

def write_csv_defaults(
path: Path, headers: tuple[str, str], default: dict[str, str] = None
) -> None:
if not path.is_file() and default is not None:
with open(path, "w", encoding="utf-8", newline="") as file:
writer = csv.writer(file)
writer.writerow(headers)
for key, value in default.items():
writer.writerow([key] if key == value else [value, key])

def track_csv_list(filename: str, headers: tuple[str, str], default: dict[str, str]=None) -> DecoratorT:

def track_csv_list(
filename: str, headers: tuple[str, str], default: dict[str, str] = None
) -> DecoratorT:
assert filename.endswith(".csv")
path = SETTINGS_DIR / filename
write_csv_defaults(path, headers, default)
Expand All @@ -65,6 +72,7 @@ def on_update(f):

return decorator


# NOTE: this is deprecated, use @track_csv_list instead.
def get_list_from_csv(
filename: str, headers: tuple[str, str], default: dict[str, str] = {}
Expand All @@ -79,6 +87,7 @@ def get_list_from_csv(
with resource.open(str(path), "r") as f:
return read_csv_list(f, headers)


def append_to_csv(filename: str, rows: dict[str, str]):
path = SETTINGS_DIR / filename
assert filename.endswith(".csv")
Expand Down
4 changes: 3 additions & 1 deletion lang/talon/talon.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def on_update_decls(decls):
"modes",
]:
l = getattr(decls, thing)
values = actions.user.create_spoken_forms_from_list(l.keys(), generate_subsequences=False)
values = actions.user.create_spoken_forms_from_list(
l.keys(), generate_subsequences=False
)
ctx_talon_lists.lists[f"user.talon_{thing}"] = values
# print(
# "List: {} \n {}".format(thing, str(ctx_talon_lists.lists[f"user.talon_{thing}"]))
Expand Down

0 comments on commit 5a92278

Please sign in to comment.