本文整理汇总了Python中pyinotify.Notifier.read_events方法的典型用法代码示例。如果您正苦于以下问题:Python Notifier.read_events方法的具体用法?Python Notifier.read_events怎么用?Python Notifier.read_events使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyinotify.Notifier
的用法示例。
在下文中一共展示了Notifier.read_events方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def run(self):
self.pclose = PClose(self.path)
PC = self.pclose
# only watch these events
mask = IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
# watch manager instance
wm = WatchManager()
notifier = Notifier(wm, PC)
print 'monitoring of %s started' % self.path
added_flag = False
# read and process events
while True:
try:
if not added_flag:
# on first iteration, add a watch on path:
# watch path for events handled by mask.
wm.add_watch(self.path, mask)
added_flag = True
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
# ...until c^c signal
print 'stop monitoring...'
# stop monitoring
notifier.stop()
break
except Exception, err:
# otherwise keep on watching
print err
示例2: run
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def run(self, location='.'):
transport = get_transport(location)
root = transport.local_abspath('.')
new_dirs = set('.')
relpaths = set('.')
while relpaths:
relpath = relpaths.pop()
paths = transport.list_dir(relpath)
for path in paths:
st = transport.stat(relpath + '/' + path)
if S_ISDIR(st.st_mode):
if path != '.bzr':
new_dirs.add(relpath + '/' + path)
relpaths.add(relpath + '/' + path)
# gather all dirs
wm = WatchManager()
added_flag = False
handler = ProcessClose()
handler._bzr_wm = wm
notifier = Notifier(wm, handler)
# read and process events
try:
while True:
if new_dirs:
for path in new_dirs:
wm.add_watch(root + '/' + path, dir_mask)
new_dirs = set()
notifier.process_events()
if notifier.check_events():
notifier.read_events()
finally:
notifier.stop()
示例3: QNotifier
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
class QNotifier(QThread):
def __init__(self, wm, processor):
self.event_queue = list()
self._processor = processor
self.notifier = Notifier(wm,
NinjaProcessEvent(self.event_queue.append))
self.notifier.coalesce_events(True)
self.keep_running = True
QThread.__init__(self)
def run(self):
while self.keep_running:
try:
self.notifier.process_events()
except OSError:
pass # OSError: [Errno 2] No such file or directory happens
e_dict = {}
while len(self.event_queue):
e_type, e_path = self.event_queue.pop(0)
e_dict.setdefault(e_path, []).append(e_type)
keys = e_dict.keys()
while len(keys):
key = keys.pop(0)
event = e_dict.pop(key)
if (ADDED in event) and (DELETED in event):
event = [e for e in event if e not in (ADDED, DELETED)]
for each_event in event:
self._processor(each_event, key)
if self.notifier.check_events():
self.notifier.read_events()
self.notifier.stop()
示例4: _reloader_inotify
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def _reloader_inotify(fnames, interval=None):
#: Mutated by inotify loop when changes occur.
changed = [False]
# Setup inotify watches
from pyinotify import WatchManager, EventsCodes, Notifier
wm = WatchManager()
mask = "IN_DELETE_SELF IN_MOVE_SELF IN_MODIFY IN_ATTRIB".split()
mask = reduce(lambda m, a: m | getattr(EventsCodes, a), mask, 0)
def signal_changed(event):
if changed[0]:
return
_log('info', ' * Detected change in %r, reloading' % event.path)
changed[:] = [True]
for fname in fnames:
wm.add_watch(fname, mask, signal_changed)
# ... And now we wait...
notif = Notifier(wm)
try:
while not changed[0]:
notif.process_events()
if notif.check_events(timeout=interval):
notif.read_events()
# TODO Set timeout to something small and check parent liveliness
finally:
notif.stop()
sys.exit(3)
示例5: TrackState
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
class TrackState(State, ProcessEvent):
mask = (EventsCodes.OP_FLAGS['IN_DELETE']
| EventsCodes.OP_FLAGS['IN_CLOSE_WRITE'])
def __init__(self, *args, **kwargs):
State.__init__(self, *args, **kwargs)
self.wm = WatchManager()
self.notifier = Notifier(self.wm, self)
def process_IN_CLOSE_WRITE(self, event):
debug("IN_CLOSE_WRITE: %r" % (event,))
path = os.path.join(event.path, event.name)
if os.path.isfile(path) and self.acceptable(path):
self.upload(self.filename_to_File(path))
def process_IN_DELETE(self, event):
debug("IN_DELETE: %r" % (event,))
path = os.path.join(event.path, event.name)
if self.acceptable(path):
self.delete(self.filename_to_File(path))
def run(self):
for f in self.files:
f = os.path.abspath(f)
self.wm.add_watch(f, self.mask, rec=True, auto_add=True)
try:
while True:
self.notifier.process_events()
if self.notifier.check_events(100):
self.notifier.read_events()
sleep(0)
except KeyboardInterrupt:
self.notifier.stop()
示例6: Watcher
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
class Watcher(object):
def __init__(self, incoming_path, logger=None):
self.incoming_path = incoming_path
self.logger = logger or logging.getLogger('Watcher')
self.wm = WatchManager()
mask = IN_CLOSE_WRITE # watched events
self.event_processor = WatcherProcesEvent(logger=self.logger)
self.notifier = Notifier(self.wm, self.event_processor)
wdd = self.wm.add_watch(self.incoming_path, mask, rec=True)
def loop(self):
"""
Check for inotify events and process then when found.
Note, check_events() below is blocking and the loop will not complete
until an event that is being listened for occurs.
"""
if self.notifier.check_events():
# read notified events and enqeue them
self.notifier.read_events()
# process the queue of events
self.notifier.process_events()
def stop(self):
# destroy the inotify's instance on this interrupt (stop monitoring)
self.notifier.stop()
示例7: main
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def main(argv):
if len(argv) == 1:
print "Usage: %s [file]" % argv[0]
return 1
path = argv[1]
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s,%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s",
datefmt="%H:%M:%S",
filename=None
# filename = "/tmp/filemon.log"
)
logging.debug("Init Watcher (%s)" % path)
class PClose(ProcessEvent):
def process_default(self, event):
logging.info("%s: %s: %s" % (getusers(event.pathname), event.pathname, str(event.maskname)))
wm = WatchManager()
notifier = Notifier(wm, PClose())
wm.add_watch(path, EventsCodes.ALL_FLAGS["ALL_EVENTS"], rec=True)
logging.info("Waiting for updates")
while True:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
return 0
示例8: run
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def run(self):
"""Create the inotify WatchManager and generate FileSysEvents"""
# Only capture events that git cares about
self._wmgr = WatchManager()
if self._is_pyinotify_08x():
notifier = Notifier(self._wmgr, FileSysEvent(self),
timeout=self._timeout)
else:
notifier = Notifier(self._wmgr, FileSysEvent(self))
dirs_seen = set()
added_flag = False
# self._abort signals app termination. The timeout is a tradeoff
# between fast notification response and waiting too long to exit.
while not self._abort:
if not added_flag:
self._watch_directory(self._path)
# Register files/directories known to git
for filename in self._git.ls_files().splitlines():
directory = os.path.dirname(filename)
self._watch_directory(directory)
added_flag = True
notifier.process_events()
if self._is_pyinotify_08x():
check = notifier.check_events()
else:
check = notifier.check_events(timeout=self._timeout)
if check:
notifier.read_events()
notifier.stop()
示例9: run
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def run(self):
"""Create the inotify WatchManager and generate FileSysEvents"""
if utils.is_win32():
self.run_win32()
return
# Only capture events that git cares about
self._wmgr = WatchManager()
if self._is_pyinotify_08x():
notifier = Notifier(self._wmgr, FileSysEvent(), timeout=self._timeout)
else:
notifier = Notifier(self._wmgr, FileSysEvent())
self._watch_directory(self._path)
# Register files/directories known to git
for filename in core.decode(self._git.ls_files()).splitlines():
filename = os.path.realpath(filename)
directory = os.path.dirname(filename)
self._watch_directory(directory)
# self._running signals app termination. The timeout is a tradeoff
# between fast notification response and waiting too long to exit.
while self._running:
if self._is_pyinotify_08x():
check = notifier.check_events()
else:
check = notifier.check_events(timeout=self._timeout)
if not self._running:
break
if check:
notifier.read_events()
notifier.process_events()
notifier.stop()
示例10: AutoLibraryUpdate
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
class AutoLibraryUpdate(EventPlugin):
PLUGIN_ID = "Automatic library update"
PLUGIN_NAME = _("Automatic Library Update")
PLUGIN_DESC = _("Keeps your library up to date with inotify. "
"Requires %s.") % "pyinotify"
# TODO: make a config option
USE_THREADS = True
event_handler = None
running = False
def enabled(self):
if not self.running:
wm = WatchManager()
self.event_handler = LibraryEvent(app.library)
# Choose event types to watch for
# FIXME: watch for IN_CREATE or for some reason folder copies
# are missed, --nickb
FLAGS = ['IN_DELETE', 'IN_CLOSE_WRITE',# 'IN_MODIFY',
'IN_MOVED_FROM', 'IN_MOVED_TO', 'IN_CREATE']
mask = reduce(lambda x, s: x | EventsCodes.ALL_FLAGS[s], FLAGS, 0)
if self.USE_THREADS:
print_d("Using threaded notifier")
self.notifier = ThreadedNotifier(wm, self.event_handler)
# Daemonize to ensure thread dies on exit
self.notifier.daemon = True
self.notifier.start()
else:
self.notifier = Notifier(wm, self.event_handler, timeout=100)
GLib.timeout_add(1000, self.unthreaded_callback)
for path in get_scan_dirs():
print_d('Watching directory %s for %s' % (path, FLAGS))
# See https://github.com/seb-m/pyinotify/wiki/
# Frequently-Asked-Questions
wm.add_watch(path, mask, rec=True, auto_add=True)
self.running = True
def unthreaded_callback(self):
"""Processes as much of the inotify events as allowed"""
assert self.notifier._timeout is not None, \
'Notifier must be constructed with a [short] timeout'
self.notifier.process_events()
# loop in case more events appear while we are processing
while self.notifier.check_events():
self.notifier.read_events()
self.notifier.process_events()
return True
# disable hook, stop the notifier:
def disabled(self):
if self.running:
self.running = False
if self.notifier:
print_d("Stopping inotify watch...")
self.notifier.stop()
示例11: LinuxFileSysMonitor
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
class LinuxFileSysMonitor(FileSysMonitor):
"""File system monitor thread"""
def __init__(self, name=None):
super(LinuxFileSysMonitor, self).__init__(name)
self.defaultMask = IN_DELETE | IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_MOVED_FROM
self.wm = WatchManager()
self.__lock = threading.Lock()
def addWatch(self, path, mask=None):
"""Add watch for path"""
super(LinuxFileSysMonitor, self).addWatch(path, mask)
if not mask:
mask = self.defaultMask
self.wm.add_watch(path, mask, auto_add=True, rec=True)
def run(self):
"""Thread entry"""
super(LinuxFileSysMonitor, self).run()
self.notifier = Notifier(self.wm, EventHandler(None, fsMonitor = self))
while not self.threadStop:
self.processMsg(1)
if self.notifier.check_events(1000):
self.notifier.read_events()
self.notifier.process_events()
def stop(self):
"""Stop watch"""
super(LinuxFileSysMonitor, self).stop()
self.notifier.stop()
示例12: startSentry
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def startSentry(sentry_paths):
"""
Sentry Runner
"""
notifier = Notifier(wm, do_event())
for path in sentry_paths:
wdd = wm.add_watch(sentry_paths[path].path, mask, rec=True)
print 'watching %s ' % sentry_paths[path].path
#wdd = wm.add_watch('/home/cmdln/sandbox/permissionminder/test', mask, rec=True)
while True:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break
except ConfigChange:
notifier.stop()
raise ConfigChange('Config Changed')
break
except:
#from pdb import set_trace;set_trace()
pass
示例13: Monitor
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def Monitor(path):
notifier = Notifier(wm, PTmp())
wdd = wm.add_watch(path, mask, rec=True)
while True:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break
示例14: fs_monitor
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def fs_monitor(path=need_remove_disk_file):
wm = WatchManager()
mask = FLAG['OP_FLAGS']['IN_CLOSE_WRITE'] | FLAG['OP_FLAGS']['IN_CLOSE_NOWRITE']
notifier = Notifier(wm, EventHandler())
wm.add_watch(path, mask, rec=True)
# print('now starting monitor %s' % path)
while True:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
示例15: newDev
# 需要导入模块: from pyinotify import Notifier [as 别名]
# 或者: from pyinotify.Notifier import read_events [as 别名]
def newDev(path):
notifier = Notifier(wm, PTmpCreate())
wdd = wm.add_watch(path, maskCreate, rec=True)
while True: # loop forever
time.sleep(0.1)
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
notifier.stop()
break