Reindentation

This commit is contained in:
Clement Lefebvre 2010-09-23 10:03:18 +01:00
parent 15b6b0100c
commit 4d7d61217c

View File

@ -5,116 +5,116 @@ import time
import gobject import gobject
try: try:
import pyinotify import pyinotify
hasInotify = True hasInotify = True
except ImportError: except ImportError:
hasInotify = False hasInotify = False
if hasInotify: if hasInotify:
class FileMonitor(object): class FileMonitor(object):
def __init__( self ): def __init__( self ):
self.monitorId = 0 self.monitorId = 0
self.wm = pyinotify.WatchManager() self.wm = pyinotify.WatchManager()
self.wdds = {} self.wdds = {}
self.callbacks = {} self.callbacks = {}
self.notifier = pyinotify.ThreadedNotifier(self.wm, self.fileChanged) self.notifier = pyinotify.ThreadedNotifier(self.wm, self.fileChanged)
self.notifier.setDaemon( True ) self.notifier.setDaemon( True )
self.notifier.start() self.notifier.start()
def addMonitor( self, filename, callback, args = None ): def addMonitor( self, filename, callback, args = None ):
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY
mId = self.wm.add_watch( filename, mask, rec = True)[filename] mId = self.wm.add_watch( filename, mask, rec = True)[filename]
if mId >= 0: if mId >= 0:
self.callbacks[mId] = ( callback, args ) self.callbacks[mId] = ( callback, args )
return mId
def removeMonitor( self, monitorId ):
if monitorId in self.callbacks:
self.wm.rm_watch( monitorId )
del self.callbacks[monitorId]
def fileChanged(self, event ):
if event.wd in self.callbacks:
# print event.path
callback = self.callbacks[event.wd]
if callback[1]:
gobject.idle_add( callback[0], callback[1] )
else:
gobject.idle_add( callback[0] )
return mId
def removeMonitor( self, monitorId ):
if monitorId in self.callbacks:
self.wm.rm_watch( monitorId )
del self.callbacks[monitorId]
def fileChanged(self, event ):
if event.wd in self.callbacks:
# print event.path
callback = self.callbacks[event.wd]
if callback[1]:
gobject.idle_add( callback[0], callback[1] )
else:
gobject.idle_add( callback[0] )
else: else:
class _MonitoredFile( object ): class _MonitoredFile( object ):
def __init__( self, filename, callback, monitorId, args ): def __init__( self, filename, callback, monitorId, args ):
self.filename = filename self.filename = filename
self.callback = callback self.callback = callback
self.monitorId = monitorId self.monitorId = monitorId
self.args = args self.args = args
self.exists = os.path.exists( self.filename ) self.exists = os.path.exists( self.filename )
if self.exists: if self.exists:
self.mtime = os.stat( filename ).st_mtime self.mtime = os.stat( filename ).st_mtime
else: else:
self.mtime = 0 self.mtime = 0
def hasChanged( self ): def hasChanged( self ):
if os.path.exists( self.filename ): if os.path.exists( self.filename ):
if not self.exists: if not self.exists:
self.exists = True self.exists = True
self.mtime = os.stat( self.filename ).st_mtime self.mtime = os.stat( self.filename ).st_mtime
return True return True
else: else:
mtime = os.stat( self.filename ).st_mtime mtime = os.stat( self.filename ).st_mtime
if mtime != self.mtime: if mtime != self.mtime:
self.mtime = mtime self.mtime = mtime
return True return True
else: else:
if self.exists: if self.exists:
self.exists = False self.exists = False
return True return True
return False return False
class MonitorThread(threading.Thread): class MonitorThread(threading.Thread):
def __init__(self, monitor): def __init__(self, monitor):
threading.Thread.__init__ ( self ) threading.Thread.__init__ ( self )
self.monitor = monitor self.monitor = monitor
def run(self): def run(self):
while(1): while(1):
self.monitor.checkFiles() self.monitor.checkFiles()
time.sleep(1) time.sleep(1)
class FileMonitor(object): class FileMonitor(object):
def __init__( self ): def __init__( self ):
self.monitorId = 0 self.monitorId = 0
self.monitoredFiles = [] self.monitoredFiles = []
self.monitorThread = MonitorThread( self ) self.monitorThread = MonitorThread( self )
self.monitorThread.setDaemon( True ) self.monitorThread.setDaemon( True )
self.monitorThread.start() self.monitorThread.start()
def addMonitor( self, filename, callback, args = None ): def addMonitor( self, filename, callback, args = None ):
self.monitorId += 1 self.monitorId += 1
self.monitoredFiles.append( _MonitoredFile( filename, callback, self.monitorId, args ) ) self.monitoredFiles.append( _MonitoredFile( filename, callback, self.monitorId, args ) )
return self.monitorId return self.monitorId
def removeMonitor( self, monitorId ): def removeMonitor( self, monitorId ):
for monitored in self.monitoredFiles: for monitored in self.monitoredFiles:
if monitorId == monitored.monitorId: if monitorId == monitored.monitorId:
self.monitoredFiles.remove( monitored ) self.monitoredFiles.remove( monitored )
break break
def checkFiles( self ): def checkFiles( self ):
for monitored in self.monitoredFiles: for monitored in self.monitoredFiles:
if monitored.hasChanged(): if monitored.hasChanged():
if monitored.args: if monitored.args:
gobject.idle_add( monitored.callback, monitored.args ) gobject.idle_add( monitored.callback, monitored.args )
else: else:
gobject.idle_add( monitored.callback ) gobject.idle_add( monitored.callback )
monitor = FileMonitor() monitor = FileMonitor()