Test formatting within ObsidianVault
This commit is contained in:
parent
43627f2aa7
commit
6d91e54ced
|
@ -58,20 +58,24 @@ MIDNIGHT = datetime.time(0, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
class ObsidianVault:
|
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
|
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.read_only = read_only
|
||||||
|
|
||||||
self.internal_file_text_cache: dict[Path, CachedFile] = {}
|
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(
|
def get_statistic(
|
||||||
self,
|
self,
|
||||||
date: datetime.date,
|
date: datetime.date,
|
||||||
|
@ -153,11 +157,15 @@ class ObsidianVault:
|
||||||
events.sort(key=lambda x: x.verb or '')
|
events.sort(key=lambda x: x.verb or '')
|
||||||
events.sort(key=lambda x: x.start_time or x.end_time or MIDNIGHT)
|
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)
|
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_pre_events=blocks_pre_events,
|
||||||
blocks_post_events=blocks_post_events,
|
blocks_post_events=blocks_post_events,
|
||||||
block_events=block_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:
|
def _save_file_text_to_cache(self, path: Path, text: bytes) -> None:
|
||||||
if path not in self.internal_file_text_cache:
|
if path not in self.internal_file_text_cache:
|
||||||
|
|
6
test/daily-template-file.md
Normal file
6
test/daily-template-file.md
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
aliases:
|
||||||
|
- My day
|
||||||
|
---
|
||||||
|
|
||||||
|
# My day
|
42
test/test_obsidian_vault.py
Normal file
42
test/test_obsidian_vault.py
Normal 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')
|
Loading…
Reference in New Issue
Block a user