1
0

Improved path parsing

This commit is contained in:
Jon Michael Aanes 2024-11-24 17:49:00 +01:00
parent 086611909e
commit 47761eb4d7
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
3 changed files with 21 additions and 5 deletions

View File

@ -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)

View File

@ -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')?'

View File

@ -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)