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