11import os
22import sys
3- import time
43
54from django .contrib .staticfiles .finders import get_finders
65from django .core .files .storage import FileSystemStorage
109from static_precompiler .settings import STATIC_ROOT
1110from static_precompiler .utils import get_compilers
1211
13- try :
14- from watchdog .events import FileSystemEventHandler
15- from watchdog .observers import Observer
16- WATCHDOG_AVAILABLE = True
17- except ImportError :
18- WATCHDOG_AVAILABLE = False
19-
2012
2113def get_scanned_dirs ():
2214 dirs = set ([STATIC_ROOT ])
@@ -28,35 +20,6 @@ def get_scanned_dirs():
2820 return sorted (dirs )
2921
3022
31- if WATCHDOG_AVAILABLE :
32- class EventHandler (FileSystemEventHandler ):
33-
34- def __init__ (self , scanned_dir , verbosity , compilers ):
35- self .scanned_dir = scanned_dir
36- self .verbosity = verbosity
37- self .compilers = compilers
38- super (EventHandler , self ).__init__ ()
39-
40- def on_any_event (self , e ):
41- if e .is_directory or e .event_type not in ("created" , "modified" ):
42- return
43- path = e .src_path [len (self .scanned_dir ):]
44- if path .startswith ("/" ):
45- path = path [1 :]
46- for compiler in self .compilers :
47- if compiler .is_supported (path ):
48- if self .verbosity > 1 :
49- if e .event_type == "created" :
50- print ("Created: '{0}'" .format (path ))
51- else :
52- print ("Modified: '{0}'" .format (path ))
53- try :
54- compiler .handle_changed_file (path )
55- except (StaticCompilationError , ValueError ) as e :
56- print (e )
57- break
58-
59-
6023class Command (NoArgsCommand ):
6124
6225 help = "Compile static files."
@@ -104,26 +67,5 @@ def handle_noargs(self, **options):
10467 break
10568
10669 if options ["watch" ]:
107- if not WATCHDOG_AVAILABLE :
108- sys .exit ("You should install watchdog in order to use --watch." )
109-
110- print ("Watching directories:" )
111- for scanned_dir in scanned_dirs :
112- print (scanned_dir )
113- print ("\n Press Control+C to exit.\n " )
114-
115- observer = Observer ()
116-
117- for scanned_dir in scanned_dirs :
118- handler = EventHandler (scanned_dir , verbosity , compilers )
119- observer .schedule (handler , path = scanned_dir , recursive = True )
120-
121- observer .start ()
122-
123- try :
124- while True :
125- time .sleep (1 )
126- except KeyboardInterrupt :
127- observer .stop ()
128-
129- observer .join ()
70+ from static_precompiler .watch import watch_dirs
71+ watch_dirs (scanned_dirs , verbosity )
0 commit comments