Coverage for src/lektor_ng/watcher.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-08-08 00:39 +0000

1from __future__ import annotations 

2 

3import os 

4from collections.abc import Generator 

5from typing import TYPE_CHECKING, Any 

6 

7import watchfiles 

8 

9from lektor_ng.utils import get_cache_dir 

10 

11if TYPE_CHECKING: 

12 from _typeshed import StrPath 

13 from lektor.environment import Environment 

14 

15 

16def watch_project(env: Environment, output_path: StrPath, **kwargs: Any) -> Generator[set[watchfiles.FileChange]]: 

17 """Watch project source files for changes. 

18 

19 Returns an generator that yields sets of changes as they are noticed. 

20 

21 Changes to files within ``output_path`` are ignored, along with other files 

22 deemed not to be Lektor source files. 

23 

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) 

28 

29 return watchfiles.watch(*watch_paths, watch_filter=watch_filter, **kwargs) 

30 

31 

32class WatchFilter(watchfiles.DefaultFilter): 

33 def __init__(self, env: Environment, **kwargs: Any): 

34 super().__init__(**kwargs) 

35 self.env = env 

36 

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)