<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> import time <EOL> import threading <EOL> from watchdog . utils . dirsnapshot import DirectorySnapshot , DirectorySnapshotDiff <EOL> from watchdog . observers . api import EventEmitter , BaseObserver , DEFAULT_OBSERVER_TIMEOUT , DEFAULT_EMITTER_TIMEOUT <EOL> from watchdog . events import DirMovedEvent , DirDeletedEvent , DirCreatedEvent , DirModifiedEvent , FileMovedEvent , FileDeletedEvent , FileCreatedEvent , FileModifiedEvent <EOL> class PollingEmitter ( EventEmitter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , event_queue , watch , timeout = DEFAULT_EMITTER_TIMEOUT ) : <EOL> EventEmitter . __init__ ( self , event_queue , watch , timeout ) <EOL> self . _snapshot = DirectorySnapshot ( watch . path , watch . is_recursive ) <EOL> self . _lock = threading . Lock ( ) <EOL> def on_thread_exit ( self ) : <EOL> self . _snapshot = None <EOL> def queue_events ( self , timeout ) : <EOL> with self . _lock : <EOL> time . sleep ( timeout ) <EOL> new_snapshot = DirectorySnapshot ( self . watch . path , self . watch . is_recursive ) <EOL> events = DirectorySnapshotDiff ( self . _snapshot , new_snapshot ) <EOL> self . _snapshot = new_snapshot <EOL> for src_path in events . files_deleted : <EOL> self . queue_event ( FileDeletedEvent ( src_path ) ) <EOL> for src_path in events . files_modified : <EOL> self . queue_event ( FileModifiedEvent ( src_path ) ) <EOL> for src_path in events . files_created : <EOL> self . queue_event ( FileCreatedEvent ( src_path ) ) <EOL> for src_path , dest_path in events . files_moved : <EOL> self . queue_event ( FileMovedEvent ( src_path , dest_path ) ) <EOL> for src_path in events . dirs_deleted : <EOL> self . queue_event ( DirDeletedEvent ( src_path ) ) <EOL> for src_path in events . dirs_modified : <EOL> self . queue_event ( DirModifiedEvent ( src_path ) ) <EOL> for src_path in events . dirs_created : <EOL> self . queue_event ( DirCreatedEvent ( src_path ) ) <EOL> for src_path , dest_path in events . dirs_moved : <EOL> self . queue_event ( DirMovedEvent ( src_path , dest_path ) ) <EOL> class PollingObserver ( BaseObserver ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , timeout = DEFAULT_OBSERVER_TIMEOUT ) : <EOL> BaseObserver . __init__ ( self , emitter_class = PollingEmitter , timeout = timeout ) </s>
