From 6d91e54cede7e5f289ddd7d60922392248d9e585 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 17 Nov 2024 17:09:11 +0100 Subject: [PATCH] Test formatting within ObsidianVault --- obsidian_import/obsidian.py | 32 +++++++++++++++++----------- test/daily-template-file.md | 6 ++++++ test/test_obsidian_vault.py | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 test/daily-template-file.md create mode 100644 test/test_obsidian_vault.py diff --git a/obsidian_import/obsidian.py b/obsidian_import/obsidian.py index 90ca967..1624651 100644 --- a/obsidian_import/obsidian.py +++ b/obsidian_import/obsidian.py @@ -58,20 +58,24 @@ MIDNIGHT = datetime.time(0, 0, 0) class ObsidianVault: - def __init__(self, vault_path: Path, read_only: bool = 'silent'): + def __init__(self, vault_path: Path, read_only: bool = 'silent', allow_invalid_vault=False): self.vault_path = vault_path - - assert (self.vault_path / '.obsidian').exists(), 'Not an Obsidian Vault' - - with open(self.vault_path / '.obsidian' / 'daily-notes.json') as f: - daily_notes_config = json.load(f) - self.daily_folder = daily_notes_config['folder'] - self.path_format = daily_notes_config['format'] - self.template_file_path = daily_notes_config['template'] self.read_only = read_only - self.internal_file_text_cache: dict[Path, CachedFile] = {} + if not allow_invalid_vault: + assert (self.vault_path / '.obsidian').exists(), 'Not an Obsidian Vault' + + try: + with open(self.vault_path / '.obsidian' / 'daily-notes.json') as f: + daily_notes_config = json.load(f) + self.daily_folder = daily_notes_config['folder'] + self.path_format = daily_notes_config['format'] + self.template_file_path = daily_notes_config['template'] + except FileNotFoundError: + if not allow_invalid_vault: + assert False, 'Missing daily notes configuration!' + def get_statistic( self, date: datetime.date, @@ -153,11 +157,15 @@ class ObsidianVault: events.sort(key=lambda x: x.verb or '') events.sort(key=lambda x: x.start_time or x.end_time or MIDNIGHT) block_events = '\n'.join('- ' + format_event_string(e) for e in events) - self._save_file_text_to_cache(self._date_file_path(date), FILE_FORMAT.format( + + post = frontmatter.Post( + content=FILE_FORMAT.format( blocks_pre_events=blocks_pre_events, blocks_post_events=blocks_post_events, block_events=block_events, - ).strip().encode('utf8')) + ).strip(), metadata=contents.frontmatter) + + self._save_file_text_to_cache(self._date_file_path(date), frontmatter.dumps(post).encode('utf8')) def _save_file_text_to_cache(self, path: Path, text: bytes) -> None: if path not in self.internal_file_text_cache: diff --git a/test/daily-template-file.md b/test/daily-template-file.md new file mode 100644 index 0000000..cd6c4d2 --- /dev/null +++ b/test/daily-template-file.md @@ -0,0 +1,6 @@ +--- +aliases: +- My day +--- + +# My day diff --git a/test/test_obsidian_vault.py b/test/test_obsidian_vault.py new file mode 100644 index 0000000..6b5673d --- /dev/null +++ b/test/test_obsidian_vault.py @@ -0,0 +1,42 @@ +import datetime + +from pathlib import Path +import pytest + +from obsidian_import import obsidian + +EXAMPLES = [ + obsidian.Event( + datetime.time(12, 0, 0), + datetime.time(12, 0, 0), + 'Ate', + 'Lunch', + 'instantly', + ), + 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]].'), +] + + +def test_write_internally(): + vault = obsidian.ObsidianVault(Path('test'), read_only=True, allow_invalid_vault=True) + vault.daily_folder = Path('daily') + vault.path_format = 'YYYY-MM-DD' + vault.template_file_path = Path('daily-template-file.md') + + vault.add_events(datetime.date(2020,1,1), EXAMPLES) + assert len(vault.internal_file_text_cache) == 2 + + assert vault.internal_file_text_cache[Path('test/daily-template-file.md')].data.startswith(b'---\n') + + expected_path = Path('test/daily/2020-01-01.md') + assert expected_path in vault.internal_file_text_cache + + assert vault.internal_file_text_cache[expected_path].data.startswith(b'---\n')