Skip to content

Commit

Permalink
Implement inotify-only FileClosedEvent
Browse files Browse the repository at this point in the history
This is a slightly modernised and updated version of the PR
gorakhargosh#245.

Native event tracking issue for reference:

 * gorakhargosh#217
  • Loading branch information
chris-allan committed Apr 29, 2017
1 parent d33bee0 commit 3b95847
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/watchdog/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
EVENT_TYPE_DELETED = 'deleted'
EVENT_TYPE_CREATED = 'created'
EVENT_TYPE_MODIFIED = 'modified'
EVENT_TYPE_CLOSED = 'closed'


class FileSystemEvent(object):
Expand Down Expand Up @@ -240,6 +241,25 @@ def __repr__(self):
dest_path=self.dest_path))


class FileClosedEvent(FileSystemEvent):
"""
File system event representing file closure on the file system. (inotify
only)
"""

event_type = EVENT_TYPE_CLOSED

def __init__(self, src_path, closed_write):
super(FileClosedEvent, self).__init__(src_path)
self.closed_write = closed_write

def __repr__(self):
return ("<%(class_name)s: src_path=%(src_path)r, "
"closed_write=%(closed_write)r>"
) % (dict(class_name=self.__class__.__name__,
src_path=self.src_path,
closed_write=self.closed_write))

# Directory events.


Expand Down Expand Up @@ -325,6 +345,7 @@ def dispatch(self, event):
EVENT_TYPE_MOVED: self.on_moved,
EVENT_TYPE_CREATED: self.on_created,
EVENT_TYPE_DELETED: self.on_deleted,
EVENT_TYPE_CLOSED: self.on_closed,
}
event_type = event.event_type
_method_map[event_type](event)
Expand Down Expand Up @@ -374,6 +395,15 @@ def on_modified(self, event):
:class:`DirModifiedEvent` or :class:`FileModifiedEvent`
"""

def on_closed(self, event):
"""Called when a file or directory is closed (inotify only).
:param event:
Event representing file/directory modification.
:type event:
:class:`DirModifiedEvent` or :class:`FileModifiedEvent`
"""


class PatternMatchingEventHandler(FileSystemEventHandler):
"""
Expand Down Expand Up @@ -449,6 +479,7 @@ def dispatch(self, event):
EVENT_TYPE_MOVED: self.on_moved,
EVENT_TYPE_CREATED: self.on_created,
EVENT_TYPE_DELETED: self.on_deleted,
EVENT_TYPE_CLOSED: self.on_closed,
}
event_type = event.event_type
_method_map[event_type](event)
Expand Down Expand Up @@ -532,6 +563,7 @@ def dispatch(self, event):
EVENT_TYPE_MOVED: self.on_moved,
EVENT_TYPE_CREATED: self.on_created,
EVENT_TYPE_DELETED: self.on_deleted,
EVENT_TYPE_CLOSED: self.on_closed,
}
event_type = event.event_type
_method_map[event_type](event)
Expand Down Expand Up @@ -565,6 +597,12 @@ def on_modified(self, event):
what = 'directory' if event.is_directory else 'file'
logging.info("Modified %s: %s", what, event.src_path)

def on_close(self, event):
super(LoggingEventHandler, self).on_close(event)

what = 'directory' if event.is_directory else 'file'
logging.info("Closed %s: %s", what, event.src_path)


class LoggingFileSystemEventHandler(LoggingEventHandler):
"""
Expand Down
5 changes: 5 additions & 0 deletions src/watchdog/observers/inotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
FileModifiedEvent,
FileMovedEvent,
FileCreatedEvent,
FileClosedEvent,
generate_sub_moved_events,
generate_sub_created_events,
)
Expand Down Expand Up @@ -174,6 +175,10 @@ def queue_events(self, timeout, full_events=False):
cls = DirCreatedEvent if event.is_directory else FileCreatedEvent
self.queue_event(cls(src_path))
self.queue_event(DirModifiedEvent(os.path.dirname(src_path)))
elif (event.is_close_write or event.is_close_nowrite) \
and not event.is_directory:
cls = FileClosedEvent
self.queue_event(cls(event.src_path, event.is_close_write))

def _decode_path(self, path):
""" Decode path only if unicode string was passed to this emitter. """
Expand Down
1 change: 1 addition & 0 deletions src/watchdog/observers/inotify_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class InotifyConstants(object):
InotifyConstants.IN_MOVED_TO,
InotifyConstants.IN_CREATE,
InotifyConstants.IN_DELETE,
InotifyConstants.IN_CLOSE,
InotifyConstants.IN_DELETE_SELF,
InotifyConstants.IN_DONT_FOLLOW,
])
Expand Down

0 comments on commit 3b95847

Please sign in to comment.