def queue_events(self, timeout): with self._lock: dir_changes, nbytes = read_directory_changes(self._directory_handle, self._buffer, self.watch.is_recursive) last_renamed_src_path = '' for action, src_path in get_FILE_NOTIFY_INFORMATION(dir_changes, nbytes): src_path = absolute_path(os.path.join(self.watch.path, src_path)) if action == FILE_ACTION_RENAMED_OLD_NAME: last_renamed_src_path = src_path elif action == FILE_ACTION_RENAMED_NEW_NAME: dest_path = src_path src_path = last_renamed_src_path if os.path.isdir(dest_path): event = DirMovedEvent(src_path, dest_path) if self.watch.is_recursive: time.sleep(WATCHDOG_TRAVERSE_MOVED_DIR_DELAY) for sub_moved_event in event.sub_moved_events(): self.queue_event(sub_moved_event) self.queue_event(event) else: self.queue_event(FileMovedEvent(src_path, dest_path)) elif os.path.isdir(src_path): event = DIR_ACTION_EVENT_MAP[action](src_path) if isinstance(event, DirCreatedEvent): time.sleep(WATCHDOG_TRAVERSE_MOVED_DIR_DELAY) sub_events = _generate_sub_created_events_for(src_path) for sub_created_event in sub_events: self.queue_event(sub_created_event) self.queue_event(event) else: self.queue_event(FILE_ACTION_EVENT_MAP[action](src_path))
def queue_events(self, timeout): with self._lock: dir_changes, nbytes = read_directory_changes( self._directory_handle, self._buffer, self.watch.is_recursive) last_renamed_src_path = "" for action, src_path in get_FILE_NOTIFY_INFORMATION( dir_changes, nbytes): src_path = absolute_path( os.path.join(self.watch.path, src_path)) if action == FILE_ACTION_RENAMED_OLD_NAME: last_renamed_src_path = src_path elif action == FILE_ACTION_RENAMED_NEW_NAME: dest_path = src_path src_path = last_renamed_src_path if os.path.isdir(dest_path): event = DirMovedEvent(src_path, dest_path) if self.watch.is_recursive: # HACK: We introduce a forced delay before # traversing the moved directory. This will read # only file movement that finishes within this # delay time. time.sleep(WATCHDOG_TRAVERSE_MOVED_DIR_DELAY) # The following block of code may not # obtain moved events for the entire tree if # the I/O is not completed within the above # delay time. So, it's not guaranteed to work. # TODO: Come up with a better solution, possibly # a way to wait for I/O to complete before # queuing events. for sub_moved_event in event.sub_moved_events( ): self.queue_event(sub_moved_event) self.queue_event(event) else: self.queue_event( FileMovedEvent(src_path, dest_path)) else: if os.path.isdir(src_path): event = DIR_ACTION_EVENT_MAP[action](src_path) if isinstance(event, DirCreatedEvent): # If a directory is moved from outside the watched folder to inside it # we only get a created directory event out of it, not any events for its children # so use the same hack as for file moves to get the child events time.sleep(WATCHDOG_TRAVERSE_MOVED_DIR_DELAY) sub_events = _generate_sub_created_events_for( src_path) for sub_created_event in sub_events: self.queue_event(sub_created_event) self.queue_event(event) else: self.queue_event( FILE_ACTION_EVENT_MAP[action](src_path))
def queue_events(self, timeout): with self._lock: dir_changes, nbytes = read_directory_changes(self._directory_handle, self._buffer, self.watch.is_recursive) last_renamed_src_path = "" for action, src_path in get_FILE_NOTIFY_INFORMATION(dir_changes, nbytes): src_path = absolute_path(os.path.join(self.watch.path, src_path)) if action == FILE_ACTION_RENAMED_OLD_NAME: continue last_renamed_src_path = src_path elif action == FILE_ACTION_RENAMED_NEW_NAME: continue dest_path = src_path src_path = last_renamed_src_path if os.path.isdir(dest_path): event = DirMovedEvent(src_path, dest_path) if self.watch.is_recursive: # HACK: We introduce a forced delay before # traversing the moved directory. This will read # only file movement that finishes within this # delay time. time.sleep(WATCHDOG_TRAVERSE_MOVED_DIR_DELAY) # The following block of code may not # obtain moved events for the entire tree if # the I/O is not completed within the above # delay time. So, it's not guaranteed to work. # TODO: Come up with a better solution, possibly # a way to wait for I/O to complete before # queuing events. for sub_moved_event in event.sub_moved_events(): self.queue_event(sub_moved_event) self.queue_event(event) else: self.queue_event(FileMovedEvent(src_path, dest_path)) else: if os.path.isdir(src_path): continue event = DIR_ACTION_EVENT_MAP[action](src_path) if isinstance(event, DirCreatedEvent): # If a directory is moved from outside the watched folder to inside it # we only get a created directory event out of it, not any events for its children # so use the same hack as for file moves to get the child events time.sleep(WATCHDOG_TRAVERSE_MOVED_DIR_DELAY) sub_events = _generate_sub_created_events_for(src_path) for sub_created_event in sub_events: self.queue_event(sub_created_event) self.queue_event(event) else: self.queue_event(FILE_ACTION_EVENT_MAP[action](src_path))
def queue_events(self, timeout): with self._lock: dir_changes, nbytes = read_directory_changes( self._directory_handle, self._buffer, self.watch.is_recursive) last_renamed_src_path = "" for action, src_path in get_FILE_NOTIFY_INFORMATION( dir_changes, nbytes): src_path = absolute_path( os.path.join(self.watch.path, src_path)) if action == FILE_ACTION_RENAMED_OLD_NAME: last_renamed_src_path = src_path elif action == FILE_ACTION_RENAMED_NEW_NAME: dest_path = src_path src_path = last_renamed_src_path if os.path.isdir(src_path): event = DirMovedEvent(src_path, dest_path) if self.watch.is_recursive: # HACK: We introduce a forced delay before # traversing the moved directory. This will read # only file movement that finishes within this # delay time. time.sleep(WATCHDOG_TRAVERSE_MOVED_DIR_DELAY) # The following block of code may not # obtain moved events for the entire tree if # the I/O is not completed within the above # delay time. So, it's not guaranteed to work. # TODO: Come up with a better solution, possibly # a way to wait for I/O to complete before # queuing events. for sub_moved_event in event.sub_moved_events( ): self.queue_event(sub_moved_event) self.queue_event(event) else: self.queue_event( FileMovedEvent(src_path, dest_path)) else: if os.path.isdir(src_path): action_event_map = DIR_ACTION_EVENT_MAP else: action_event_map = FILE_ACTION_EVENT_MAP self.queue_event(action_event_map[action](src_path))
def queue_events(self, timeout): with self._lock: dir_changes, nbytes = read_directory_changes(self._directory_handle, self._buffer, self.watch.is_recursive) last_renamed_src_path = "" for action, src_path in get_FILE_NOTIFY_INFORMATION(dir_changes, nbytes): src_path = absolute_path(os.path.join(self.watch.path, src_path)) if action == FILE_ACTION_RENAMED_OLD_NAME: last_renamed_src_path = src_path elif action == FILE_ACTION_RENAMED_NEW_NAME: dest_path = src_path src_path = last_renamed_src_path if os.path.isdir(dest_path): event = DirMovedEvent(src_path, dest_path) if self.watch.is_recursive: # HACK: We introduce a forced delay before # traversing the moved directory. This will read # only file movement that finishes within this # delay time. time.sleep(WATCHDOG_TRAVERSE_MOVED_DIR_DELAY) # The following block of code may not # obtain moved events for the entire tree if # the I/O is not completed within the above # delay time. So, it's not guaranteed to work. # TODO: Come up with a better solution, possibly # a way to wait for I/O to complete before # queuing events. for sub_moved_event in event.sub_moved_events(): self.queue_event(sub_moved_event) self.queue_event(event) else: self.queue_event(FileMovedEvent(src_path, dest_path)) else: if os.path.isdir(src_path): action_event_map = DIR_ACTION_EVENT_MAP else: action_event_map = FILE_ACTION_EVENT_MAP self.queue_event(action_event_map[action](src_path))