changeset 8606:1c5752dabf76

inotify: use a decorator instead of dispatching calls
author Nicolas Dumazet <nicdumz.commits@gmail.com>
date Fri, 22 May 2009 10:26:56 +0900
parents ed2d9bdbfad2
children e17eba98c789
files hgext/inotify/server.py
diffstat 1 files changed, 21 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/inotify/server.py	Fri May 22 09:57:53 2009 +0900
+++ b/hgext/inotify/server.py	Fri May 22 10:26:56 2009 +0900
@@ -415,6 +415,22 @@
             self.statcache.pop(wpath, None)
             raise
 
+    def eventaction(code):
+        def decorator(f):
+            def wrapper(self, wpath):
+                if code == 'm' and wpath in self.lastevent and \
+                    self.lastevent[wpath] in 'cm':
+                    return
+                self.lastevent[wpath] = code
+                self.timeout = 250
+
+                f(self, wpath)
+
+            wrapper.func_name = f.func_name
+            return wrapper
+        return decorator
+
+    @eventaction('c')
     def created(self, wpath):
         if wpath == '.hgignore':
             self.update_hgignore()
@@ -425,6 +441,7 @@
         except OSError:
             pass
 
+    @eventaction('m')
     def modified(self, wpath):
         if wpath == '.hgignore':
             self.update_hgignore()
@@ -436,6 +453,7 @@
         except OSError:
             pass
 
+    @eventaction('d')
     def deleted(self, wpath):
         if wpath == '.hgignore':
             self.update_hgignore()
@@ -446,21 +464,6 @@
 
         self.deletefile(wpath, self.repo.dirstate[wpath])
 
-    def work(self, wpath, evt):
-        try:
-            if evt == 'c':
-                self.created(wpath)
-            elif evt == 'm':
-                if wpath in self.lastevent and self.lastevent[wpath] in 'cm':
-                    return
-                self.modified(wpath)
-            elif evt == 'd':
-                self.deleted(wpath)
-
-            self.lastevent[wpath] = evt
-        finally:
-            self.timeout = 250
-
     def process_create(self, wpath, evt):
         if self.ui.debugflag:
             self.ui.note(_('%s event: created %s\n') %
@@ -469,7 +472,7 @@
         if evt.mask & inotify.IN_ISDIR:
             self.scan(wpath)
         else:
-            self.work(wpath, 'c')
+            self.created(wpath)
 
     def process_delete(self, wpath, evt):
         if self.ui.debugflag:
@@ -482,7 +485,7 @@
                 self.deletefile(join(wpath, wfn), '?')
             self.scan(wpath)
         else:
-            self.work(wpath, 'd')
+            self.deleted(wpath)
 
     def process_modify(self, wpath, evt):
         if self.ui.debugflag:
@@ -490,7 +493,7 @@
                          (self.event_time(), wpath))
 
         if not (evt.mask & inotify.IN_ISDIR):
-            self.work(wpath, 'm')
+            self.modified(wpath)
 
     def process_unmount(self, evt):
         self.ui.warn(_('filesystem containing %s was unmounted\n') %