From e7c2ea69728106430045b3c2b13398f5512ef772 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 20 Oct 2024 18:27:32 +0200 Subject: [PATCH] Data improvements --- obsidian_import/__init__.py | 7 ++++++- obsidian_import/obsidian.py | 13 +++++++++---- test/test_obsidian_format.py | 1 + 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/obsidian_import/__init__.py b/obsidian_import/__init__.py index ee775a4..035ab23 100644 --- a/obsidian_import/__init__.py +++ b/obsidian_import/__init__.py @@ -99,6 +99,10 @@ def import_step_counts_csv(vault: ObsidianVault, rows: Rows) -> int: return num_updated +def escape_for_obsidian_link(link: str) -> str: + return link.replace(':', ' ').replace('/', ' ').replace(' ', ' ') + + def import_watched_series_csv(vault: ObsidianVault, rows: Rows) -> int: verb = 'Watched' @@ -114,6 +118,7 @@ def import_watched_series_csv(vault: ObsidianVault, rows: Rows) -> int: del rows def map_to_event(sample: RealizedActivitySample) -> Event: + noun = escape_for_obsidian_link(sample.single_label_with_category('series.name')) comment = '{} Episode {}: *{}*'.format( sample.single_label_with_category('season.name'), sample.single_label_with_category('episode.index'), @@ -123,7 +128,7 @@ def import_watched_series_csv(vault: ObsidianVault, rows: Rows) -> int: return Event(sample.start_at.astimezone(expected_tz).replace(second=0,microsecond=0).time(), sample.end_at.astimezone(expected_tz).replace(second=0,microsecond=0).time(), verb, - sample.single_label_with_category('series.name'), + noun, comment, ) diff --git a/obsidian_import/obsidian.py b/obsidian_import/obsidian.py index be2085b..d9b3e5b 100644 --- a/obsidian_import/obsidian.py +++ b/obsidian_import/obsidian.py @@ -20,10 +20,15 @@ StatisticKey = str class Event: start_time: datetime.time | None end_time: datetime.time | None - verb: str - subject: str + verb: str | None + subject: str | None comment: str + def __post_init__(self): + if self.subject: + assert ':' not in self.subject + assert '/' not in self.subject + @dataclasses.dataclass(frozen=True) class FileContents: @@ -226,8 +231,8 @@ def format_event_string(event: Event) -> str: RE_TIME = r'(\d\d:\d\d(?::\d\d(?:\.\d+?))?)' RE_VERB = r'(\w+(?:ed|te))' -RE_LINK_MD = r'\[([^\]]*)\]\(?:[^)]*\)' -RE_LINK_WIKI = r'\[\[([^\]]*)\]\]' +RE_LINK_MD = r'\[([^\]:/]*)\]\(?:[^)]*\)' +RE_LINK_WIKI = r'\[\[([^\]:/]*)\]\]' RE_TIME_FORMAT = RE_TIME + r'(?:\s*\-\s*' + RE_TIME + r')?' diff --git a/test/test_obsidian_format.py b/test/test_obsidian_format.py index 86909bf..4b0e2d7 100644 --- a/test/test_obsidian_format.py +++ b/test/test_obsidian_format.py @@ -10,6 +10,7 @@ EXAMPLES = [ obsidian.Event(datetime.time(20, 0, 0), datetime.time(22, 0, 0), "Watched", "Tom and Jerry", "on the *Television*"), obsidian.Event(None, None, None, None, "Took a walk"), + obsidian.Event(None, None, None, None, "Watched [[Cyberpunk: Edgerunners]]."), ] @pytest.mark.parametrize("event", EXAMPLES)