From 47761eb4d7bf50782716f5b55955c4eca9460460 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 24 Nov 2024 17:49:00 +0100 Subject: [PATCH] Improved path parsing --- obsidian_import/__init__.py | 9 ++++++--- obsidian_import/obsidian.py | 8 ++++++-- test/test_obsidian_format.py | 9 +++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/obsidian_import/__init__.py b/obsidian_import/__init__.py index f0123cd..e779a8c 100644 --- a/obsidian_import/__init__.py +++ b/obsidian_import/__init__.py @@ -4,6 +4,7 @@ Sub-module for importing time-based data into Obsidian. """ import dataclasses +from zoneinfo import ZoneInfo import datetime from collections.abc import Iterator, Iterable from logging import getLogger @@ -130,9 +131,11 @@ def import_activity_sample_csv( if group_category is not None: samples = merge_adjacent_samples(list(samples), group_category) + timezone = ZoneInfo('Europe/Copenhagen') # TODO: Parameterize in an intelligent manner + samples_per_date: dict[datetime.date, list[RealizedActivitySample]] = {} for sample in samples: - date: datetime.date = sample.start_at.date() + date: datetime.date = sample.start_at.astimezone(timezone).date() samples_per_date.setdefault(date, []) samples_per_date[date].append(sample) del date, sample @@ -150,7 +153,7 @@ def import_activity_sample_csv( num_updated = 0 - for date, samples in samples_per_date.items(): + for date, samples in list(samples_per_date.items()): events = [map_to_event(sample) for sample in samples] was_updated = vault.add_events(date, events) @@ -235,8 +238,8 @@ def import_data(obsidian_path: Path, dry_run=True): num_updated = import_step_counts_csv(vault, rows) logger.info('Updated %d files', num_updated) + #import_played_games_csv_from_file(vault) import_watched_series_csv_from_file(vault) - import_played_games_csv_from_file(vault) num_dirty = len([f for f in vault.internal_file_text_cache.values() if f.is_dirty]) logger.info('dirty files in cache: %d', num_dirty) diff --git a/obsidian_import/obsidian.py b/obsidian_import/obsidian.py index 0b765fa..79a8a4f 100644 --- a/obsidian_import/obsidian.py +++ b/obsidian_import/obsidian.py @@ -166,7 +166,11 @@ class ObsidianVault: events.sort(key=lambda x: x.verb or '') date_sentinel = datetime.datetime(1900, 1, 1, 1, 1, 1, tzinfo=contents.timezone) events.sort(key=lambda x: x.start_time or x.end_time or date_sentinel) - block_events = '\n'.join('- ' + format_event_string(e, tz = contents.timezone) for e in events) + #print(events) + + formatted_events = ['- ' + format_event_string(e, tz = contents.timezone) for e in events] + formatted_events = list(dict.fromkeys(formatted_events)) + block_events = '\n'.join(formatted_events) post = frontmatter.Post( content=FILE_FORMAT.format( @@ -269,7 +273,7 @@ def format_event_string(event: Event, tz: ZoneInfo) -> 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_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 b85c666..45955ec 100644 --- a/test/test_obsidian_format.py +++ b/test/test_obsidian_format.py @@ -6,6 +6,15 @@ from obsidian_import import obsidian from .test_obsidian_vault import EXAMPLES, EXAMPLE_DATE, EXAMPLE_TIMEZONE + +def test_parse_event_string(): + formatted = '17:44 | Watched [[../../media/anime/Azumanga Daioh]]. Season 1 Episode 6: *Sports Fest*' + + event = obsidian.parse_event_string(formatted, EXAMPLE_DATE, EXAMPLE_TIMEZONE) + assert event is not None + assert event.subject == 'Azumanga Daioh' + assert event.start_time is not None + @pytest.mark.parametrize('event', EXAMPLES) def test_format_preserves_information(event: obsidian.Event): formatted = obsidian.format_event_string(event, EXAMPLE_TIMEZONE)