-
Notifications
You must be signed in to change notification settings - Fork 780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
port from resource.open to resource.watch (Talon v0.4) #1199
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,20 @@ | ||
from pathlib import Path | ||
from typing import Callable, IO | ||
import csv | ||
import os | ||
from pathlib import Path | ||
|
||
from talon import resource | ||
|
||
# NOTE: This method requires this module to be one folder below the top-level | ||
# community/knausj folder. | ||
SETTINGS_DIR = Path(__file__).parents[1] / "settings" | ||
SETTINGS_DIR.mkdir(exist_ok=True) | ||
|
||
if not SETTINGS_DIR.is_dir(): | ||
os.mkdir(SETTINGS_DIR) | ||
|
||
|
||
def get_list_from_csv( | ||
filename: str, headers: tuple[str, str], default: dict[str, str] = {} | ||
): | ||
"""Retrieves list from CSV""" | ||
path = SETTINGS_DIR / filename | ||
assert filename.endswith(".csv") | ||
|
||
if not path.is_file(): | ||
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]) | ||
CallbackT = Callable[[dict[str, str]], None] | ||
DecoratorT = Callable[[CallbackT], CallbackT] | ||
|
||
# Now read via resource to take advantage of talon's | ||
# ability to reload this script for us when the resource changes | ||
with resource.open(str(path), "r") as f: | ||
rows = list(csv.reader(f)) | ||
def read_csv_list(f: IO, headers: tuple[str, str]) -> dict[str, str]: | ||
rows = list(csv.reader(f)) | ||
|
||
# print(str(rows)) | ||
mapping = {} | ||
|
@@ -59,6 +44,40 @@ def get_list_from_csv( | |
|
||
return mapping | ||
|
||
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: | ||
assert filename.endswith(".csv") | ||
path = SETTINGS_DIR / filename | ||
write_csv_defaults(path, headers, default) | ||
|
||
def decorator(fn: CallbackT) -> CallbackT: | ||
@resource.watch(str(path)) | ||
def on_update(f): | ||
knausj85 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data = read_csv_list(f, headers) | ||
fn(data) | ||
|
||
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] = {} | ||
): | ||
"""Retrieves list from CSV""" | ||
assert filename.endswith(".csv") | ||
path = SETTINGS_DIR / filename | ||
write_csv_defaults(path, headers, default) | ||
|
||
# Now read via resource to take advantage of talon's | ||
# ability to reload this script for us when the resource changes | ||
with resource.open(str(path), "r") as f: | ||
return read_csv_list(f, headers) | ||
Comment on lines
+68
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to make |
||
|
||
def append_to_csv(filename: str, rows: dict[str, str]): | ||
path = SETTINGS_DIR / filename | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer earlier return for simplicity