Coverage for src/lektor_ng/watcher.py: 100%
19 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-03 19:18 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-03 19:18 +0000
1from __future__ import annotations
3import os
4from collections.abc import Generator
5from typing import TYPE_CHECKING, Any
7import watchfiles
9from lektor_ng.utils import get_cache_dir
11if TYPE_CHECKING:
12 from _typeshed import StrPath
13 from lektor.environment import Environment
16def watch_project(env: Environment, output_path: StrPath, **kwargs: Any) -> Generator[set[watchfiles.FileChange]]:
17 """Watch project source files for changes.
19 Returns an generator that yields sets of changes as they are noticed.
21 Changes to files within ``output_path`` are ignored, along with other files
22 deemed not to be Lektor source files.
24 """
25 watch_paths = [env.root_path, env.project.project_file, *env.theme_paths]
26 ignore_paths = [os.path.abspath(p) for p in (get_cache_dir(), output_path)]
27 watch_filter = WatchFilter(env, ignore_paths=ignore_paths)
29 return watchfiles.watch(*watch_paths, watch_filter=watch_filter, **kwargs)
32class WatchFilter(watchfiles.DefaultFilter):
33 def __init__(self, env: Environment, **kwargs: Any):
34 super().__init__(**kwargs)
35 self.env = env
37 def __call__(self, change: watchfiles.Change, path: str) -> bool:
38 if self.env.is_uninteresting_source_name(os.path.basename(path)):
39 return False
40 return super().__call__(change, path)