fix(vault): the watcher ignores obsidian and lock files before it wakes
This commit is contained in:
1 parent
c120953b02
commit
e58153628c
2 files changed
+88
-2
No files matched your search
@@ -19,12 +19,14 @@ from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from watchfiles import awatch
|
||||
from watchfiles import DefaultFilter, awatch
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import Iterable
|
||||
|
||||
__all__ = ["Change", "VaultWatch", "WatchRules"]
|
||||
from watchfiles import Change as FileChange
|
||||
|
||||
__all__ = ["Change", "VaultFilter", "VaultWatch", "WatchRules"]
|
||||
|
||||
_log = logging.getLogger("beaver_gateway.vault.watch")
|
||||
|
||||
@@ -74,6 +76,26 @@ class _Seen:
|
||||
count: int
|
||||
|
||||
|
||||
IGNORE_DIRS = (".obsidian", ".trash", ".git")
|
||||
"""Housekeeping directories whose churn never means an edit of the vault."""
|
||||
|
||||
IGNORE_NAMES = (".sync.lock", "*.lock", "*.tmp", "*~")
|
||||
"""Lock and scratch file names, matched against the entity name."""
|
||||
|
||||
|
||||
class VaultFilter(DefaultFilter):
|
||||
"""Drops housekeeping paths inside ``awatch``, before the loop is woken."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__(ignore_dirs=(*DefaultFilter.ignore_dirs, *IGNORE_DIRS))
|
||||
|
||||
def __call__(self, change: FileChange, path: str) -> bool:
|
||||
name = Path(path).name
|
||||
if any(fnmatch.fnmatchcase(name, pattern) for pattern in IGNORE_NAMES):
|
||||
return False
|
||||
return super().__call__(change, path)
|
||||
|
||||
|
||||
class VaultWatch:
|
||||
def __init__(
|
||||
self, root: Path, rules: WatchRules, *, tz: str = "UTC", debounce: float = 4.0
|
||||
@@ -98,6 +120,7 @@ class VaultWatch:
|
||||
_log.info("watching %s (%d files in snapshot)", self.root, len(self._seen))
|
||||
async for changes in awatch(
|
||||
self.root,
|
||||
watch_filter=VaultFilter(),
|
||||
debounce=int(self._debounce * 1000),
|
||||
stop_event=self._stop,
|
||||
ignore_permission_denied=True,
|
||||
|
||||
Reference in new issue
Block a user