<s> import os <EOL> import time <EOL> import Queue <EOL> from watchdog . observers import Observer <EOL> from watchdog . events import FileSystemEventHandler , DirModifiedEvent <EOL> _Empty = Queue . Empty <EOL> class EventHandler ( FileSystemEventHandler ) : <EOL> def __init__ ( self , callback = None ) : <EOL> if callback is not None : <EOL> self . queue = None <EOL> self . callback = callback <EOL> else : <EOL> self . queue = Queue . Queue ( ) <EOL> self . callback = self . queue . put <EOL> def on_any_event ( self , event ) : <EOL> if not isinstance ( event , DirModifiedEvent ) : <EOL> item = ( time . time ( ) , event . event_type , event . src_path ) <EOL> if self . queue is not None : <EOL> self . queue . put ( item ) <EOL> else : <EOL> self . callback ( * item ) <EOL> class BasicWatcher ( object ) : <EOL> def __init__ ( self , paths , callback = None ) : <EOL> self . event_handler = EventHandler ( callback = callback ) <EOL> self . observer = Observer ( ) <EOL> for path in paths : <EOL> self . observer . schedule ( self . event_handler , path , recursive = True ) <EOL> self . observer . setDaemon ( True ) <EOL> def is_interesting ( self , time , event_type , path ) : <EOL> return True <EOL> def __iter__ ( self ) : <EOL> if self . event_handler . queue is None : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> item = self . event_handler . queue . get ( timeout = <NUM_LIT:1> ) <EOL> if self . is_interesting ( * item ) : <EOL> yield item <EOL> except _Empty : <EOL> pass <EOL> class Watcher ( BasicWatcher ) : <EOL> def __init__ ( self , env , output_path = None ) : <EOL> BasicWatcher . __init__ ( self , paths = [ env . root_path ] ) <EOL> self . env = env <EOL> self . output_path = output_path <EOL> def is_interesting ( self , time , event_type , path ) : <EOL> if self . env . is_uninteresting_source_name ( os . path . basename ( path ) ) : <EOL> return False <EOL> if self . output_path is not None and os . path . abspath ( path ) . startswith ( self . output_path ) : <EOL> return False <EOL> return True <EOL> def watch ( env ) : <EOL> """<STR_LIT>""" <EOL> watcher = Watcher ( env ) <EOL> watcher . observer . start ( ) <EOL> try : <EOL> for event in watcher : <EOL> yield event <EOL> except KeyboardInterrupt : <EOL> watcher . observer . stop ( ) </s>
