/usr/share/doc/gamin
NameSizeModeActions
AUTHORS1530644editdlrm
callbacks.gif45140644editdlrm
ChangeLog832560644editdlrm
client_server.gif46240644editdlrm
config.html68830644editdlrm
contacts.html53580644editdlrm
debug.html73730644editdlrm
debugging.txt12880644editdlrm
devel.html49680644editdlrm
differences.html67690644editdlrm
downloads.html42000644editdlrm
FAQ.html39630644editdlrm
gamin.html323850644editdlrm
index.html81040644editdlrm
internals.html54200644editdlrm
NEWS97100644editdlrm
news.html144560644editdlrm
overview.html53240644editdlrm
python.html62920644editdlrm
README15160644editdlrm
security.html67680644editdlrm
server_structs.gif88800644editdlrm
socket.txt16300644editdlrm
TODO26540644editdlrm
using.html40960644editdlrm
Edit: /usr/share/doc/gamin/python.html (6292B)
Python bindings

Gamin the File Alteration Monitor

Python bindings

Main Menu
Related links

Starting with release 0.0.21, gamin comes with Python bindings. Use "import gamin" to load the module. It exports the main class "WatchMonitor" which handle one connection to the gam_server. Once an instance of the class has been created you can use the watch_directory and watch_file methods to register objects to monitors, and provide the callback to be called when events are generated. Like the FAM API, the python binding API is passive, i.e. one need  to  monitor the file descriptor provided with the get_fd() method to detect events, and call handle_one_event() (blocking) or handle_events() (non-blocking) to process incoming events and get the callbacks appropriately. You can also use the event_pending() method to detect if handle_one_event() would block or not.
Since a short example is worth a thousand words, here is a small session from the python shell:

>>> import gamin
>>>
>>> def callback(path, event):
...     print "Got callback: %s, %s" % (path, event)
...
>>> mon = gamin.WatchMonitor()
>>> mon.watch_directory(".", callback)
<gamin.WatchObject instance at 0xb7f7b56c>
>>> mon.event_pending()
1
>>> mon.handle_one_event()
Got callback: /u/veillard/temp, 8
1
>>> mon.handle_events()
Got callback: bar1, 8
Got callback: foo1, 8
Got callback: /u/veillard/temp, 9
3
>>> mon.stop_watch(".")
>>> del mon

The corresponding standalone code follows:

#!/usr/bin/env python

import gamin
import time

def callback(path, event):
    print "Got callback: %s, %s" % (path, event)

mon = gamin.WatchMonitor()
mon.watch_directory(".", callback)
time.sleep(1)
ret = mon.event_pending()
if ret > 0:
    ret = mon.handle_one_event()
    ret = mon.handle_events()
mon.stop_watch(".")
del mon

Note the addition of the sleep timeout, it is needed because due to the round trip between the client and the gam_server, events may not be immediately available after the monitor creation to the client.

Daniel Veillard