1 --- gamin-0.1.10/python/gamin.py.orig 2007-07-04 15:36:48.000000000 +0000
2 +++ gamin-0.1.10/python/gamin.py 2023-01-25 17:45:14.040421915 +0000
5 def GaminErrmsg(err = None):
10 msg = "bad argument error"
13 class GaminException(Exception):
14 def __init__(self, value):
15 Exception.__init__(self)
17 - self.errno = GaminErrno()
19 + self.errno = GaminErrno()
22 str = GaminErrmsg(self.errno)
25 return repr(self.value) + ': ' + str
26 return repr(self.value)
29 """This is a wrapper for a FAM connection. It uses a single connection
30 - to the gamin server, over a socket. Use get_fd() to get the file
31 - descriptor which allows to plug it in an usual event loop. The
32 - watch_directory(), watch_file() and stop_watch() are direct mapping
33 - to the FAM API. The event raised are also a direct mapping of the
35 + to the gamin server, over a socket. Use get_fd() to get the file
36 + descriptor which allows to plug it in an usual event loop. The
37 + watch_directory(), watch_file() and stop_watch() are direct mapping
38 + to the FAM API. The event raised are also a direct mapping of the
42 - def __init__ (self, monitor, mon_no, path, dir, callback, data=None):
43 - self.monitor = monitor
44 - self.callback = callback
47 - self.__mon_no = mon_no
49 - ret = _gamin.MonitorDirectory(self.__mon_no, path, self);
51 - raise(GaminException("Failed to monitor directory %s" %
54 - ret = _gamin.MonitorFile(self.__mon_no, path, self);
56 - raise(GaminException("Failed to monitor file %s" %
59 - ret = _gamin.MonitorDebug(self.__mon_no, path, self);
61 - raise(GaminException("Failed to debug %s" %
64 + def __init__ (self, monitor, mon_no, path, dir, callback, data=None):
65 + self.monitor = monitor
66 + self.callback = callback
69 + self.__mon_no = mon_no
71 + ret = _gamin.MonitorDirectory(self.__mon_no, path, self);
73 + raise(GaminException("Failed to monitor directory %s" %
76 + ret = _gamin.MonitorFile(self.__mon_no, path, self);
78 + raise(GaminException("Failed to monitor file %s" %
81 + ret = _gamin.MonitorDebug(self.__mon_no, path, self);
83 + raise(GaminException("Failed to debug %s" %
87 - def _internal_callback(self, path, event):
88 + def _internal_callback(self, path, event):
89 # it is very important here to catch all exception which may
90 # arise in the client callback code.
92 - if self.data != None:
93 - self.callback (path, event, self.data)
95 - self.callback (path, event)
98 - traceback.print_exc()
100 - if event == GAMAcknowledge:
102 - self.monitor.cancelled.remove(self)
104 - print "gamin failed to remove from cancelled"
108 - ret = _gamin.MonitorCancel(self.__mon_no, self.__req_no);
110 - raise(GaminException("Failed to stop monitor on %s" %
113 - self.monitor.cancelled.append(self)
115 - print "gamin cancel() failed to add to cancelled"
117 + if self.data != None:
118 + self.callback (path, event, self.data)
120 + self.callback (path, event)
123 + traceback.print_exc()
125 + if event == GAMAcknowledge:
127 + self.monitor.cancelled.remove(self)
129 + print("gamin failed to remove from cancelled")
133 + ret = _gamin.MonitorCancel(self.__mon_no, self.__req_no);
135 + raise(GaminException("Failed to stop monitor on %s" %
138 + self.monitor.cancelled.append(self)
140 + print("gamin cancel() failed to add to cancelled")
143 self.__no = _gamin.MonitorConnect()
145 - raise(GaminException("Failed to connect to gam_server"))
147 - self.__fd = _gamin.GetFd(self.__no)
149 - _gamin.MonitorClose(self.__no)
150 - raise(GaminException("Failed to get file descriptor"))
151 - self.cancelled = []
153 + raise(GaminException("Failed to connect to gam_server"))
155 + self.__fd = _gamin.GetFd(self.__no)
157 + _gamin.MonitorClose(self.__no)
158 + raise(GaminException("Failed to get file descriptor"))
159 + self.cancelled = []
164 def __raise_disconnected():
165 - raise(GaminException("Already disconnected"))
166 + raise(GaminException("Already disconnected"))
168 def _debug_object(self, value, callback, data = None):
169 if has_debug_api == 0:
174 - self.__raise_disconnected();
175 + self.__raise_disconnected();
176 obj = self.WatchObject(self, self.__no, value, -1, callback, data)
177 # persistency need to be insured
178 - self.objects["debug"] = obj
180 + self.objects["debug"] = obj
183 def disconnect(self):
185 - _gamin.MonitorClose(self.__no)
187 + _gamin.MonitorClose(self.__no)
190 def watch_directory(self, directory, callback, data = None):
192 - self.__raise_disconnected();
193 + self.__raise_disconnected();
194 directory = os.path.abspath(directory)
196 obj = self.WatchObject(self, self.__no, directory, 1, callback, data)
197 if self.objects.has_key(directory):
198 - self.objects[directory].append(obj)
200 - self.objects[directory] = [obj]
202 + self.objects[directory].append(obj)
204 + self.objects[directory] = [obj]
207 def watch_file(self, file, callback, data = None):
209 - self.__raise_disconnected();
210 + self.__raise_disconnected();
211 file = os.path.abspath(file)
213 obj = self.WatchObject(self, self.__no, file, 0, callback, data)
214 if self.objects.has_key(file):
215 - self.objects[file].append(obj)
217 - self.objects[file] = [obj]
219 + self.objects[file].append(obj)
221 + self.objects[file] = [obj]
227 - ret = _gamin.MonitorNoExists(self.__no)
230 + ret = _gamin.MonitorNoExists(self.__no)
233 def stop_watch(self, path):
237 path = os.path.abspath(path)
239 - list = self.objects[path]
241 - raise(GaminException("Resource %s is not monitored" % (path)))
244 - self.objects[path] = []
247 + list = self.objects[path]
249 + raise(GaminException("Resource %s is not monitored" % (path)))
252 + self.objects[path] = []
256 - self.__raise_disconnected();
257 + self.__raise_disconnected();
260 def event_pending(self):
262 - self.__raise_disconnected();
263 + self.__raise_disconnected();
264 ret = _gamin.EventPending(self.__no);
266 - raise(GaminException("Failed to check pending events"))
269 + raise(GaminException("Failed to check pending events"))
272 def handle_one_event(self):
274 - self.__raise_disconnected();
275 + self.__raise_disconnected();
276 ret = _gamin.ProcessOneEvent(self.__no);
278 - raise(GaminException("Failed to process one event"))
281 + raise(GaminException("Failed to process one event"))
284 def handle_events(self):
286 - self.__raise_disconnected();
287 + self.__raise_disconnected();
288 ret = _gamin.ProcessEvents(self.__no);
290 - raise(GaminException("Failed to process events"))
293 + raise(GaminException("Failed to process events"))
296 def run_unit_tests():
297 def callback(path, event):
298 - print "Got callback: %s, %s" % (path, event)
299 + print("Got callback: %s, %s") % (path, event)
301 - print "watching current directory"
302 + print("watching current directory")
303 mon.watch_directory(".", callback)
306 - print "fd: ", mon.get_fd()
307 + print("fd: "), mon.get_fd()
308 ret = mon.event_pending()
309 - print "pending: ", ret
310 + print("pending: "), ret
312 ret = mon.handle_one_event()
313 - print "processed %d event" % (ret)
314 - ret = mon.handle_events()
315 - print "processed %d remaining events" % (ret)
316 - print "stop watching current directory"
317 + print("processed %d event") % (ret)
318 + ret = mon.handle_events()
319 + print("processed %d remaining events") % (ret)
320 + print("stop watching current directory")
322 - print "disconnecting"
323 + print("disconnecting")
326 if __name__ == '__main__':