home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / pycentral / deskbar-applet / site-packages / deskbar / Watcher.py < prev   
Encoding:
Python Source  |  2006-08-29  |  1.5 KB  |  63 lines

  1. """
  2. Helper classes to monitor directories/files for changes using gnomevfs
  3. """
  4.  
  5. import traceback
  6. import gnomevfs
  7. import gobject, gtk
  8.  
  9. class Watcher(gobject.GObject):
  10.     __gsignals__ = {
  11.         "changed" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [gobject.TYPE_STRING]),
  12.     }
  13.     def __init__(self):
  14.         gobject.GObject.__init__(self)
  15.         self.watched = {}
  16.         self.monitor_type = gnomevfs.MONITOR_FILE
  17.         
  18.     def add(self, args):
  19.         if not type(args) is list:
  20.             args = [args]
  21.  
  22.         for name in args:
  23.             if name == "":
  24.                 continue
  25.                 
  26.             if not name in self.watched:
  27.                 try:
  28.                     self.watched[name] = gnomevfs.monitor_add(name, self.monitor_type, self._on_change)
  29.                 except Exception, msg:
  30.                     traceback.print_exc()
  31.                     self.watched[name] = 0
  32.     
  33.     def remove(self, args):
  34.         if not type(args) is list:
  35.             args = [args]
  36.             
  37.         for name in args:
  38.             if name in self.watched:
  39.                 if self.watched[name] != 0:
  40.                     gnomevfs.monitor_cancel(self.watched[name])
  41.                 del self.watched[name]
  42.     
  43.     def remove_all(self):
  44.         self.remove(self.watched.keys())
  45.                 
  46.     def _on_change(self, monitor, changed, event):
  47.         if event == gnomevfs.MONITOR_EVENT_CHANGED or event == gnomevfs.MONITOR_EVENT_CREATED:
  48.             gobject.idle_add(self.emit, 'changed', gnomevfs.get_local_path_from_uri(changed))
  49.  
  50. class FileWatcher(Watcher):
  51.     def __init__(self):
  52.         Watcher.__init__(self)
  53.  
  54. class DirWatcher(Watcher):
  55.     def __init__(self):
  56.         Watcher.__init__(self)
  57.         self.monitor_type = gnomevfs.MONITOR_DIRECTORY
  58.  
  59. if gtk.pygtk_version < (2,8,0):
  60.     gobject.type_register(Watcher)
  61.     gobject.type_register(FileWatcher)
  62.     gobject.type_register(DirWatcher)
  63.