diff options
author | Sebastien Martini <seb@dbzteam.org> | 2009-11-11 18:13:24 +0100 |
---|---|---|
committer | Sebastien Martini <seb@dbzteam.org> | 2009-11-11 18:13:24 +0100 |
commit | add26a10a28661d88710c99d10a1cf2065c06d04 (patch) | |
tree | 881319888979d2ca84e63732ee1f4f809b52e292 | |
parent | 649429ae8af743d9c404c1cc219bd018ae073578 (diff) | |
download | pyinotify-add26a10a28661d88710c99d10a1cf2065c06d04.tar.gz |
Updated examples.
-rw-r--r-- | python2/examples/chain.py | 27 | ||||
-rw-r--r-- | python2/examples/stats.py | 12 | ||||
-rw-r--r-- | python2/examples/stats_threaded.py | 20 | ||||
-rw-r--r-- | python2/examples/transient_file.py | 10 | ||||
-rw-r--r-- | python2/examples/unicode.py | 13 |
5 files changed, 42 insertions, 40 deletions
diff --git a/python2/examples/chain.py b/python2/examples/chain.py index 9760414..5f110ee 100644 --- a/python2/examples/chain.py +++ b/python2/examples/chain.py @@ -1,8 +1,8 @@ # Example: monitors events and logs them into a log file. # -from pyinotify import * +import pyinotify -class Log(ProcessEvent): +class Log(pyinotify.ProcessEvent): def my_init(self, fileobj): """ Method automatically called from ProcessEvent.__init__(). Additional @@ -15,11 +15,11 @@ class Log(ProcessEvent): self._fileobj.write(str(event) + '\n') self._fileobj.flush() -class TrackModifications(ProcessEvent): +class TrackModifications(pyinotify.ProcessEvent): def process_IN_MODIFY(self, event): print 'IN_MODIFY' -class Empty(ProcessEvent): +class Empty(pyinotify.ProcessEvent): def my_init(self, msg): self._msg = msg @@ -27,13 +27,14 @@ class Empty(ProcessEvent): print self._msg -#log.setLevel(10) +# pyinotify.log.setLevel(10) fo = file('/var/log/pyinotify_log', 'w') -wm = WatchManager() -# It is important to pass named extra arguments like 'fileobj'. -notifier = Notifier(wm, - default_proc_fun=Empty(TrackModifications(Log(fileobj=fo)), - msg='outtee chained function')) -wm.add_watch('/tmp', ALL_EVENTS) -notifier.loop() -fo.close() +try: + wm = pyinotify.WatchManager() + # It is important to pass named extra arguments like 'fileobj'. + handler = Empty(TrackModifications(Log(fileobj=fo)), msg='Outer chained method') + notifier = pyinotify.Notifier(wm, default_proc_fun=handler) + wm.add_watch('/tmp', pyinotify.ALL_EVENTS) + notifier.loop() +finally: + fo.close() diff --git a/python2/examples/stats.py b/python2/examples/stats.py index 1e882ec..2e8c94b 100644 --- a/python2/examples/stats.py +++ b/python2/examples/stats.py @@ -1,8 +1,8 @@ # Example: prints statistics. # -from pyinotify import * +import pyinotify -class Identity(ProcessEvent): +class Identity(pyinotify.ProcessEvent): def process_default(self, event): # Does nothing, just to demonstrate how stuffs could trivially # be accomplished after having processed statistics. @@ -14,10 +14,10 @@ def on_loop(notifier): print repr(s_inst), '\n', s_inst, '\n' -wm = WatchManager() +wm = pyinotify.WatchManager() # Stats is a subclass of ProcessEvent provided by pyinotify # for computing basics statistics. -s = Stats() -notifier = Notifier(wm, default_proc_fun=Identity(s), read_freq=5) -wm.add_watch('/tmp/', ALL_EVENTS, rec=True, auto_add=True) +s = pyinotify.Stats() +notifier = pyinotify.Notifier(wm, default_proc_fun=Identity(s), read_freq=5) +wm.add_watch('/tmp/', pyinotify.ALL_EVENTS, rec=True, auto_add=True) notifier.loop(callback=on_loop) diff --git a/python2/examples/stats_threaded.py b/python2/examples/stats_threaded.py index 997dc6b..309f63a 100644 --- a/python2/examples/stats_threaded.py +++ b/python2/examples/stats_threaded.py @@ -1,32 +1,32 @@ # Example: prints statistics (threaded version). # import time -from pyinotify import * +import pyinotify # Do the same thing than stats.py but with a ThreadedNotifier's # instance. # This example illustrates the use of this class but the recommanded # implementation is whom of stats.py -class Identity(ProcessEvent): +class Identity(pyinotify.ProcessEvent): def process_default(self, event): # Does nothing, just to demonstrate how stuffs could be done # after having processed statistics. print 'Does nothing.' # Thread #1 -wm1 = WatchManager() -s1 = Stats() # Stats is a subclass of ProcessEvent -notifier1 = ThreadedNotifier(wm1, default_proc_fun=Identity(s1)) +wm1 = pyinotify.WatchManager() +s1 = pyinotify.Stats() # Stats is a subclass of ProcessEvent +notifier1 = pyinotify.ThreadedNotifier(wm1, default_proc_fun=Identity(s1)) notifier1.start() -wm1.add_watch('/tmp/', ALL_EVENTS, rec=True, auto_add=True) +wm1.add_watch('/tmp/', pyinotify.ALL_EVENTS, rec=True, auto_add=True) # Thread #2 -wm2 = WatchManager() -s2 = Stats() # Stats is a subclass of ProcessEvent -notifier2 = ThreadedNotifier(wm2, default_proc_fun=Identity(s2)) +wm2 = pyinotify.WatchManager() +s2 = pyinotify.Stats() # Stats is a subclass of ProcessEvent +notifier2 = pyinotify.ThreadedNotifier(wm2, default_proc_fun=Identity(s2)) notifier2.start() -wm2.add_watch('/var/log/', ALL_EVENTS, rec=False, auto_add=False) +wm2.add_watch('/var/log/', pyinotify.ALL_EVENTS, rec=False, auto_add=False) while True: try: diff --git a/python2/examples/transient_file.py b/python2/examples/transient_file.py index 7957717..3df2a97 100644 --- a/python2/examples/transient_file.py +++ b/python2/examples/transient_file.py @@ -1,9 +1,9 @@ # Example: monitors transient files. # # Run this code, then run transient_file.sh in another shell. -from pyinotify import * +import pyinotify -class ProcessTransientFile(ProcessEvent): +class ProcessTransientFile(pyinotify.ProcessEvent): def process_IN_MODIFY(self, event): # We have explicitely registered for this kind of event. @@ -18,9 +18,9 @@ class ProcessTransientFile(ProcessEvent): print 'default: ', event.maskname -wm = WatchManager() -notifier = Notifier(wm) +wm = pyinotify.WatchManager() +notifier = pyinotify.Notifier(wm) # In this case you must give the class object (ProcessTransientFile) # as last parameter not a class instance. -wm.watch_transient_file('/tmp/test1234', IN_MODIFY, ProcessTransientFile) +wm.watch_transient_file('/tmp/test1234', pyinotify.IN_MODIFY, ProcessTransientFile) notifier.loop() diff --git a/python2/examples/unicode.py b/python2/examples/unicode.py index 0a3d4c3..ec0d36f 100644 --- a/python2/examples/unicode.py +++ b/python2/examples/unicode.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- import os import sys -from pyinotify import * +import pyinotify + # create path #path = u'/tmp/test\u0444' @@ -11,11 +12,11 @@ path = unicode(path, sys.getfilesystemencoding()) if not os.path.isdir(path): os.mkdir(path) -log.setLevel(10) -wm = WatchManager() -notifier = Notifier(wm) +pyinotify.log.setLevel(10) +wm = pyinotify.WatchManager() +notifier = pyinotify.Notifier(wm) -wdd = wm.add_watch(path, IN_OPEN) -wm.update_watch(wdd[path], ALL_EVENTS) +wdd = wm.add_watch(path, pyinotify.IN_OPEN) +wm.update_watch(wdd[path], pyinotify.ALL_EVENTS) notifier.loop() |