-
Notifications
You must be signed in to change notification settings - Fork 308
/
watcher.py
32 lines (22 loc) · 984 Bytes
/
watcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from __future__ import annotations
import os
from pathlib import Path
from typing import Any
from typing import TYPE_CHECKING
import watchfiles
from lektor.utils import get_cache_dir
if TYPE_CHECKING:
from lektor.environment import Environment
def watch_project(env: Environment, output_path: str | Path, **kwargs: Any):
watch_paths = [env.root_path, *env.theme_paths]
ignore_paths = [os.path.abspath(p) for p in (get_cache_dir(), output_path)]
watch_filter = WatchFilter(env, ignore_paths=ignore_paths)
return watchfiles.watch(*watch_paths, watch_filter=watch_filter, **kwargs)
class WatchFilter(watchfiles.DefaultFilter):
def __init__(self, env: Environment, **kwargs: Any):
super().__init__(**kwargs)
self.env = env
def __call__(self, change: watchfiles.Change, path: str) -> bool:
if self.env.is_uninteresting_source_name(os.path.basename(path)):
return False
return super().__call__(change, path)