1
0

Test formatting within ObsidianVault

This commit is contained in:
Jon Michael Aanes 2024-11-17 17:09:11 +01:00
parent 43627f2aa7
commit 6d91e54ced
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
3 changed files with 68 additions and 12 deletions

View File

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

View File

@ -0,0 +1,6 @@
---
aliases:
- My day
---
# My day

View File

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