2015-06-26 01:36:36 +01:00
|
|
|
#!/usr/bin/python2
|
2014-03-28 15:44:13 +00:00
|
|
|
|
2009-07-27 11:45:34 +01:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import threading
|
|
|
|
import time
|
2013-03-14 00:24:32 +00:00
|
|
|
from gi.repository import GLib
|
2009-07-27 11:45:34 +01:00
|
|
|
|
|
|
|
try:
|
2010-09-23 10:03:18 +01:00
|
|
|
import pyinotify
|
|
|
|
hasInotify = True
|
2009-07-27 11:45:34 +01:00
|
|
|
except ImportError:
|
2010-09-23 10:03:18 +01:00
|
|
|
hasInotify = False
|
2009-07-27 11:45:34 +01:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
|
2009-07-27 11:45:34 +01:00
|
|
|
if hasInotify:
|
2019-02-11 13:54:20 +00:00
|
|
|
|
2010-09-23 10:03:18 +01:00
|
|
|
class FileMonitor(object):
|
2019-02-11 13:54:20 +00:00
|
|
|
def __init__(self):
|
2010-09-23 10:03:18 +01:00
|
|
|
self.monitorId = 0
|
|
|
|
self.wm = pyinotify.WatchManager()
|
|
|
|
self.wdds = {}
|
|
|
|
self.callbacks = {}
|
|
|
|
self.notifier = pyinotify.ThreadedNotifier(self.wm, self.fileChanged)
|
2019-02-11 13:54:20 +00:00
|
|
|
self.notifier.setDaemon(True)
|
2010-09-23 10:03:18 +01:00
|
|
|
self.notifier.start()
|
|
|
|
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def addMonitor(self, filename, callback, args = None):
|
2010-09-23 10:05:48 +01:00
|
|
|
try:
|
|
|
|
mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY
|
2019-02-11 13:54:20 +00:00
|
|
|
mId = self.wm.add_watch(filename, mask, rec = True)[filename]
|
2010-09-23 10:05:48 +01:00
|
|
|
if mId >= 0:
|
2019-02-11 13:54:20 +00:00
|
|
|
self.callbacks[mId] = (callback, args)
|
|
|
|
except:
|
2016-05-21 17:11:36 +01:00
|
|
|
mId = 0
|
2010-09-23 10:03:18 +01:00
|
|
|
return mId
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def removeMonitor(self, monitorId):
|
2010-09-23 10:03:18 +01:00
|
|
|
if monitorId in self.callbacks:
|
2019-02-11 13:54:20 +00:00
|
|
|
self.wm.rm_watch(monitorId)
|
2010-09-23 10:03:18 +01:00
|
|
|
del self.callbacks[monitorId]
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def fileChanged(self, event):
|
2010-09-23 10:03:18 +01:00
|
|
|
if event.wd in self.callbacks:
|
2019-02-11 13:54:20 +00:00
|
|
|
#print event.path
|
2010-09-23 10:03:18 +01:00
|
|
|
callback = self.callbacks[event.wd]
|
|
|
|
if callback[1]:
|
2019-02-11 13:54:20 +00:00
|
|
|
GLib.idle_add(callback[0], callback[1])
|
2010-09-23 10:03:18 +01:00
|
|
|
else:
|
2019-02-11 13:54:20 +00:00
|
|
|
GLib.idle_add(callback[0])
|
2009-07-27 11:45:34 +01:00
|
|
|
|
|
|
|
else:
|
2010-09-23 10:03:18 +01:00
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
class _MonitoredFile(object):
|
|
|
|
def __init__(self, filename, callback, monitorId, args):
|
2010-09-23 10:03:18 +01:00
|
|
|
self.filename = filename
|
|
|
|
self.callback = callback
|
|
|
|
self.monitorId = monitorId
|
|
|
|
self.args = args
|
2019-02-11 13:54:20 +00:00
|
|
|
self.exists = os.path.exists(self.filename)
|
2010-09-23 10:03:18 +01:00
|
|
|
if self.exists:
|
2019-02-11 13:54:20 +00:00
|
|
|
self.mtime = os.stat(filename).st_mtime
|
2010-09-23 10:03:18 +01:00
|
|
|
else:
|
|
|
|
self.mtime = 0
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def hasChanged(self):
|
|
|
|
if os.path.exists(self.filename):
|
2010-09-23 10:03:18 +01:00
|
|
|
if not self.exists:
|
|
|
|
self.exists = True
|
2019-02-11 13:54:20 +00:00
|
|
|
self.mtime = os.stat(self.filename).st_mtime
|
2010-09-23 10:03:18 +01:00
|
|
|
return True
|
|
|
|
else:
|
2019-02-11 13:54:20 +00:00
|
|
|
mtime = os.stat(self.filename).st_mtime
|
2010-09-23 10:03:18 +01:00
|
|
|
if mtime != self.mtime:
|
|
|
|
self.mtime = mtime
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
if self.exists:
|
|
|
|
self.exists = False
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
class MonitorThread(threading.Thread):
|
|
|
|
def __init__(self, monitor):
|
2019-02-11 13:54:20 +00:00
|
|
|
threading.Thread.__init__(self)
|
2010-09-23 10:03:18 +01:00
|
|
|
self.monitor = monitor
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while(1):
|
|
|
|
self.monitor.checkFiles()
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
class FileMonitor(object):
|
2019-02-11 13:54:20 +00:00
|
|
|
def __init__(self):
|
2010-09-23 10:03:18 +01:00
|
|
|
self.monitorId = 0
|
|
|
|
self.monitoredFiles = []
|
2019-02-11 13:54:20 +00:00
|
|
|
self.monitorThread = MonitorThread(self)
|
|
|
|
self.monitorThread.setDaemon(True)
|
2010-09-23 10:03:18 +01:00
|
|
|
self.monitorThread.start()
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def addMonitor(self, filename, callback, args = None):
|
2010-09-23 10:03:18 +01:00
|
|
|
self.monitorId += 1
|
2019-02-11 13:54:20 +00:00
|
|
|
self.monitoredFiles.append(_MonitoredFile(filename, callback, self.monitorId, args))
|
2010-09-23 10:03:18 +01:00
|
|
|
return self.monitorId
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def removeMonitor(self, monitorId):
|
2010-09-23 10:03:18 +01:00
|
|
|
for monitored in self.monitoredFiles:
|
|
|
|
if monitorId == monitored.monitorId:
|
2019-02-11 13:54:20 +00:00
|
|
|
self.monitoredFiles.remove(monitored)
|
2010-09-23 10:03:18 +01:00
|
|
|
break
|
|
|
|
|
2019-02-11 13:54:20 +00:00
|
|
|
def checkFiles(self):
|
2010-09-23 10:03:18 +01:00
|
|
|
for monitored in self.monitoredFiles:
|
|
|
|
if monitored.hasChanged():
|
|
|
|
if monitored.args:
|
2019-02-11 13:54:20 +00:00
|
|
|
GLib.idle_add(monitored.callback, monitored.args)
|
2010-09-23 10:03:18 +01:00
|
|
|
else:
|
2019-02-11 13:54:20 +00:00
|
|
|
GLib.idle_add(monitored.callback)
|
2009-07-27 11:45:34 +01:00
|
|
|
|
|
|
|
monitor = FileMonitor()
|