11#!/usr/bin/env python3
2- import datetime
2+ import os
33import subprocess
4-
5- from inotify .adapters import InotifyTree
4+ from datetime import datetime
5+ from pathlib import Path
6+ from time import sleep
67
78
89def build_docs ():
@@ -15,54 +16,53 @@ def refresh_assets():
1516 )
1617
1718
18- def watch_for_changes (debug = False ):
19- i = InotifyTree ("docs" )
19+ def watch_for_changes ():
20+ """Watch for changes using watchdog."""
21+ from watchdog .events import FileSystemEvent , FileSystemEventHandler
22+ from watchdog .observers import Observer
2023
21- IGNORED_EVENTS = ["IN_CLOSE_WRITE" , "IN_CLOSE_NOWRITE" , "IN_ACCESS" , "IN_OPEN" ]
22- IGNORED_PATHS = ["docs/_build" ]
24+ class MyHandler (FileSystemEventHandler ):
25+ def __init__ (self ):
26+ super ().__init__ ()
27+ self .last_build = datetime .now ()
2328
24- last_build = None
29+ def on_modified (self , event : FileSystemEvent ):
30+ cwd = os .getcwd ()
31+ file_path = Path (event .src_path )
2532
26- for event in i .event_gen (yield_nones = False ):
27- (_ , type_names , path , filename ) = event
33+ IGNORED_PATHS = ["docs/_build" ]
2834
29- ignore = False
30- for ignored_event_type in IGNORED_EVENTS :
31- if ignored_event_type in type_names :
32- ignore = True
33- break
35+ if event .is_directory :
36+ return
3437
35- for ignored_path in IGNORED_PATHS :
36- if path .startswith (ignored_path ):
37- ignore = True
38- break
38+ if file_path .name .isdigit ():
39+ return
3940
40- if (
41- filename .endswith (".swx" )
42- or filename .endswith (".swp" )
43- or filename .endswith ("~" )
44- ):
45- ignore = True
41+ if (
42+ file_path . name .endswith (".swx" )
43+ or file_path . name .endswith (".swp" )
44+ or file_path . name .endswith ("~" )
45+ ):
46+ return
4647
47- if ignore :
48- continue
48+ for ignored_path in IGNORED_PATHS :
49+ ignored_path = os .path .join (cwd , ignored_path )
50+ if str (file_path ).startswith (ignored_path ):
51+ return
4952
50- if last_build :
51- # If build has been triggered in the last 50 ms, skip
52- delta = (datetime .datetime .now () - last_build ).microseconds / 1000
53- if delta < 50 :
54- continue
53+ build_docs ()
54+ refresh_assets ()
5555
56- build_docs ()
57- refresh_assets ()
58- last_build = datetime .datetime .now ()
56+ event_handler = MyHandler ()
57+ observer = Observer ()
58+ observer .schedule (event_handler , path = "docs" , recursive = True )
59+ observer .start ()
5960
60- if debug :
61- print (
62- "PATH=[{}] FILENAME=[{}] EVENT_TYPES={}" .format (
63- path , filename , type_names
64- )
65- )
61+ try :
62+ while True :
63+ sleep (1 )
64+ except KeyboardInterrupt :
65+ observer .stop ()
6666
6767
6868if __name__ == "__main__" :
0 commit comments