commit | author | age
|
9ecaa7
|
1 |
--- gamin-0.1.10/python/gamin.py.orig 2007-07-04 15:36:48.000000000 +0000 |
K |
2 |
+++ gamin-0.1.10/python/gamin.py 2023-01-25 17:45:14.040421915 +0000 |
|
3 |
@@ -36,7 +36,7 @@ |
|
4 |
|
|
5 |
def GaminErrmsg(err = None): |
|
6 |
if err == None: |
|
7 |
- err = _gamin.Errno() |
|
8 |
+ err = _gamin.Errno() |
|
9 |
if err == GAM_ARG: |
|
10 |
msg = "bad argument error" |
|
11 |
elif err == GAM_FILE: |
|
12 |
@@ -58,199 +58,199 @@ |
|
13 |
class GaminException(Exception): |
|
14 |
def __init__(self, value): |
|
15 |
Exception.__init__(self) |
|
16 |
- self.value = value |
|
17 |
- self.errno = GaminErrno() |
|
18 |
+ self.value = value |
|
19 |
+ self.errno = GaminErrno() |
|
20 |
|
|
21 |
def __str__(self): |
|
22 |
str = GaminErrmsg(self.errno) |
|
23 |
- if str != "": |
|
24 |
+ if str != "": |
|
25 |
return repr(self.value) + ': ' + str |
|
26 |
return repr(self.value) |
|
27 |
|
|
28 |
class WatchMonitor: |
|
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 |
|
34 |
- FAM API events.""" |
|
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 |
|
39 |
+ FAM API events.""" |
|
40 |
|
|
41 |
class WatchObject: |
|
42 |
- def __init__ (self, monitor, mon_no, path, dir, callback, data=None): |
|
43 |
- self.monitor = monitor |
|
44 |
- self.callback = callback |
|
45 |
- self.data = data |
|
46 |
- self.path = path |
|
47 |
- self.__mon_no = mon_no |
|
48 |
- if dir == 1: |
|
49 |
- ret = _gamin.MonitorDirectory(self.__mon_no, path, self); |
|
50 |
- if ret < 0: |
|
51 |
- raise(GaminException("Failed to monitor directory %s" % |
|
52 |
- (path))) |
|
53 |
- elif dir == 0: |
|
54 |
- ret = _gamin.MonitorFile(self.__mon_no, path, self); |
|
55 |
- if ret < 0: |
|
56 |
- raise(GaminException("Failed to monitor file %s" % |
|
57 |
- (path))) |
|
58 |
- elif dir == -1: |
|
59 |
- ret = _gamin.MonitorDebug(self.__mon_no, path, self); |
|
60 |
- if ret < 0: |
|
61 |
- raise(GaminException("Failed to debug %s" % |
|
62 |
- (path))) |
|
63 |
- self.__req_no = ret |
|
64 |
+ def __init__ (self, monitor, mon_no, path, dir, callback, data=None): |
|
65 |
+ self.monitor = monitor |
|
66 |
+ self.callback = callback |
|
67 |
+ self.data = data |
|
68 |
+ self.path = path |
|
69 |
+ self.__mon_no = mon_no |
|
70 |
+ if dir == 1: |
|
71 |
+ ret = _gamin.MonitorDirectory(self.__mon_no, path, self); |
|
72 |
+ if ret < 0: |
|
73 |
+ raise(GaminException("Failed to monitor directory %s" % |
|
74 |
+ (path))) |
|
75 |
+ elif dir == 0: |
|
76 |
+ ret = _gamin.MonitorFile(self.__mon_no, path, self); |
|
77 |
+ if ret < 0: |
|
78 |
+ raise(GaminException("Failed to monitor file %s" % |
|
79 |
+ (path))) |
|
80 |
+ elif dir == -1: |
|
81 |
+ ret = _gamin.MonitorDebug(self.__mon_no, path, self); |
|
82 |
+ if ret < 0: |
|
83 |
+ raise(GaminException("Failed to debug %s" % |
|
84 |
+ (path))) |
|
85 |
+ self.__req_no = ret |
|
86 |
|
|
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. |
|
91 |
- try: |
|
92 |
- if self.data != None: |
|
93 |
- self.callback (path, event, self.data) |
|
94 |
- else: |
|
95 |
- self.callback (path, event) |
|
96 |
- except: |
|
97 |
- import traceback |
|
98 |
- traceback.print_exc() |
|
99 |
- |
|
100 |
- if event == GAMAcknowledge: |
|
101 |
- try: |
|
102 |
- self.monitor.cancelled.remove(self) |
|
103 |
- except: |
|
104 |
- print "gamin failed to remove from cancelled" |
|
105 |
- pass |
|
106 |
- |
|
107 |
- def cancel(self): |
|
108 |
- ret = _gamin.MonitorCancel(self.__mon_no, self.__req_no); |
|
109 |
- if ret < 0: |
|
110 |
- raise(GaminException("Failed to stop monitor on %s" % |
|
111 |
- (self.path))) |
|
112 |
- try: |
|
113 |
- self.monitor.cancelled.append(self) |
|
114 |
- except: |
|
115 |
- print "gamin cancel() failed to add to cancelled" |
|
116 |
+ try: |
|
117 |
+ if self.data != None: |
|
118 |
+ self.callback (path, event, self.data) |
|
119 |
+ else: |
|
120 |
+ self.callback (path, event) |
|
121 |
+ except: |
|
122 |
+ import traceback |
|
123 |
+ traceback.print_exc() |
|
124 |
+ |
|
125 |
+ if event == GAMAcknowledge: |
|
126 |
+ try: |
|
127 |
+ self.monitor.cancelled.remove(self) |
|
128 |
+ except: |
|
129 |
+ print("gamin failed to remove from cancelled") |
|
130 |
+ pass |
|
131 |
+ |
|
132 |
+ def cancel(self): |
|
133 |
+ ret = _gamin.MonitorCancel(self.__mon_no, self.__req_no); |
|
134 |
+ if ret < 0: |
|
135 |
+ raise(GaminException("Failed to stop monitor on %s" % |
|
136 |
+ (self.path))) |
|
137 |
+ try: |
|
138 |
+ self.monitor.cancelled.append(self) |
|
139 |
+ except: |
|
140 |
+ print("gamin cancel() failed to add to cancelled") |
|
141 |
|
|
142 |
def __init__ (self): |
|
143 |
self.__no = _gamin.MonitorConnect() |
|
144 |
- if self.__no < 0: |
|
145 |
- raise(GaminException("Failed to connect to gam_server")) |
|
146 |
- self.objects = {} |
|
147 |
- self.__fd = _gamin.GetFd(self.__no) |
|
148 |
- if self.__fd < 0: |
|
149 |
- _gamin.MonitorClose(self.__no) |
|
150 |
- raise(GaminException("Failed to get file descriptor")) |
|
151 |
- self.cancelled = [] |
|
152 |
+ if self.__no < 0: |
|
153 |
+ raise(GaminException("Failed to connect to gam_server")) |
|
154 |
+ self.objects = {} |
|
155 |
+ self.__fd = _gamin.GetFd(self.__no) |
|
156 |
+ if self.__fd < 0: |
|
157 |
+ _gamin.MonitorClose(self.__no) |
|
158 |
+ raise(GaminException("Failed to get file descriptor")) |
|
159 |
+ self.cancelled = [] |
|
160 |
|
|
161 |
def __del__ (self): |
|
162 |
self.disconnect() |
|
163 |
|
|
164 |
def __raise_disconnected(): |
|
165 |
- raise(GaminException("Already disconnected")) |
|
166 |
+ raise(GaminException("Already disconnected")) |
|
167 |
|
|
168 |
def _debug_object(self, value, callback, data = None): |
|
169 |
if has_debug_api == 0: |
|
170 |
- return; |
|
171 |
+ return; |
|
172 |
|
|
173 |
if (self.__no < 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 |
|
179 |
- return obj |
|
180 |
+ self.objects["debug"] = obj |
|
181 |
+ return obj |
|
182 |
|
|
183 |
def disconnect(self): |
|
184 |
if (self.__no >= 0): |
|
185 |
- _gamin.MonitorClose(self.__no) |
|
186 |
- self.__no = -1; |
|
187 |
+ _gamin.MonitorClose(self.__no) |
|
188 |
+ self.__no = -1; |
|
189 |
|
|
190 |
def watch_directory(self, directory, callback, data = None): |
|
191 |
if (self.__no < 0): |
|
192 |
- self.__raise_disconnected(); |
|
193 |
+ self.__raise_disconnected(); |
|
194 |
directory = os.path.abspath(directory) |
|
195 |
|
|
196 |
obj = self.WatchObject(self, self.__no, directory, 1, callback, data) |
|
197 |
if self.objects.has_key(directory): |
|
198 |
- self.objects[directory].append(obj) |
|
199 |
- else: |
|
200 |
- self.objects[directory] = [obj] |
|
201 |
- return obj |
|
202 |
+ self.objects[directory].append(obj) |
|
203 |
+ else: |
|
204 |
+ self.objects[directory] = [obj] |
|
205 |
+ return obj |
|
206 |
|
|
207 |
def watch_file(self, file, callback, data = None): |
|
208 |
if (self.__no < 0): |
|
209 |
- self.__raise_disconnected(); |
|
210 |
+ self.__raise_disconnected(); |
|
211 |
file = os.path.abspath(file) |
|
212 |
|
|
213 |
obj = self.WatchObject(self, self.__no, file, 0, callback, data) |
|
214 |
if self.objects.has_key(file): |
|
215 |
- self.objects[file].append(obj) |
|
216 |
- else: |
|
217 |
- self.objects[file] = [obj] |
|
218 |
- return obj |
|
219 |
+ self.objects[file].append(obj) |
|
220 |
+ else: |
|
221 |
+ self.objects[file] = [obj] |
|
222 |
+ return obj |
|
223 |
|
|
224 |
def no_exists(self): |
|
225 |
if (self.__no < 0): |
|
226 |
- return |
|
227 |
- ret = _gamin.MonitorNoExists(self.__no) |
|
228 |
- return ret |
|
229 |
+ return |
|
230 |
+ ret = _gamin.MonitorNoExists(self.__no) |
|
231 |
+ return ret |
|
232 |
|
|
233 |
def stop_watch(self, path): |
|
234 |
if (self.__no < 0): |
|
235 |
- return |
|
236 |
+ return |
|
237 |
path = os.path.abspath(path) |
|
238 |
- try: |
|
239 |
- list = self.objects[path] |
|
240 |
- except: |
|
241 |
- raise(GaminException("Resource %s is not monitored" % (path))) |
|
242 |
- for obj in list: |
|
243 |
- obj.cancel() |
|
244 |
- self.objects[path] = [] |
|
245 |
- |
|
246 |
+ try: |
|
247 |
+ list = self.objects[path] |
|
248 |
+ except: |
|
249 |
+ raise(GaminException("Resource %s is not monitored" % (path))) |
|
250 |
+ for obj in list: |
|
251 |
+ obj.cancel() |
|
252 |
+ self.objects[path] = [] |
|
253 |
+ |
|
254 |
def get_fd(self): |
|
255 |
if (self.__no < 0): |
|
256 |
- self.__raise_disconnected(); |
|
257 |
+ self.__raise_disconnected(); |
|
258 |
return self.__fd |
|
259 |
|
|
260 |
def event_pending(self): |
|
261 |
if (self.__no < 0): |
|
262 |
- self.__raise_disconnected(); |
|
263 |
+ self.__raise_disconnected(); |
|
264 |
ret = _gamin.EventPending(self.__no); |
|
265 |
- if ret < 0: |
|
266 |
- raise(GaminException("Failed to check pending events")) |
|
267 |
- return ret |
|
268 |
+ if ret < 0: |
|
269 |
+ raise(GaminException("Failed to check pending events")) |
|
270 |
+ return ret |
|
271 |
|
|
272 |
def handle_one_event(self): |
|
273 |
if (self.__no < 0): |
|
274 |
- self.__raise_disconnected(); |
|
275 |
+ self.__raise_disconnected(); |
|
276 |
ret = _gamin.ProcessOneEvent(self.__no); |
|
277 |
- if ret < 0: |
|
278 |
- raise(GaminException("Failed to process one event")) |
|
279 |
- return ret |
|
280 |
+ if ret < 0: |
|
281 |
+ raise(GaminException("Failed to process one event")) |
|
282 |
+ return ret |
|
283 |
|
|
284 |
def handle_events(self): |
|
285 |
if (self.__no < 0): |
|
286 |
- self.__raise_disconnected(); |
|
287 |
+ self.__raise_disconnected(); |
|
288 |
ret = _gamin.ProcessEvents(self.__no); |
|
289 |
- if ret < 0: |
|
290 |
- raise(GaminException("Failed to process events")) |
|
291 |
- return ret |
|
292 |
+ if ret < 0: |
|
293 |
+ raise(GaminException("Failed to process events")) |
|
294 |
+ return ret |
|
295 |
|
|
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) |
|
300 |
mon = WatchMonitor() |
|
301 |
- print "watching current directory" |
|
302 |
+ print("watching current directory") |
|
303 |
mon.watch_directory(".", callback) |
|
304 |
import time |
|
305 |
time.sleep(1) |
|
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 |
|
311 |
if ret > 0: |
|
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") |
|
321 |
mon.stop_watch(".") |
|
322 |
- print "disconnecting" |
|
323 |
+ print("disconnecting") |
|
324 |
del mon |
|
325 |
|
|
326 |
if __name__ == '__main__': |