10
by Jason Conti
Adding MessageWidget to display the messages in a MessageWindow. Also includes lots of changes to MessageWindow, and LogWatcher.py, rewriting logtracker.py to use gio instead of inotify, allowing the use of signals to update about changes instead of a separate thread, which doesn't seem to be working. |
1 |
"""
|
2 |
LogWatcher.py
|
|
3 |
by Jason Conti
|
|
4 |
February 15, 2010
|
|
5 |
||
6 |
Watches a log file for changes.
|
|
7 |
||
8 |
Adapted from Watcher.py in the deskbar applet.
|
|
9 |
"""
|
|
10 |
||
11 |
import gio |
|
12 |
import glib |
|
13 |
import gobject |
|
11
by Jason Conti
Updated Notifications.py to use the new LogWatcher class. Currently still uses the listeners to notify when new messages arrive, should probably update to use glib signals. |
14 |
import gtk |
10
by Jason Conti
Adding MessageWidget to display the messages in a MessageWindow. Also includes lots of changes to MessageWindow, and LogWatcher.py, rewriting logtracker.py to use gio instead of inotify, allowing the use of signals to update about changes instead of a separate thread, which doesn't seem to be working. |
15 |
import os.path |
16 |
||
17 |
class LogWatcher(gobject.GObject): |
|
18 |
"""Tracks a log file for changes."""
|
|
19 |
__gsignals__ = { |
|
20 |
"changed": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [gobject.TYPE_STRING]) |
|
21 |
}
|
|
22 |
def __init__(self, path): |
|
23 |
gobject.GObject.__init__(self) |
|
24 |
||
25 |
self._path = os.path.abspath(path) |
|
26 |
self._offset = self.get_file_length() |
|
27 |
self._watcher = None |
|
28 |
||
29 |
def start(self): |
|
30 |
"""Starts watching the file."""
|
|
31 |
gfile = gio.File(path=self._path) |
|
32 |
self._watcher = gfile.monitor_file() |
|
33 |
self._watcher.connect("changed", self.on_changed) |
|
34 |
||
35 |
def stop(self): |
|
36 |
"""Removes the watch from the tracked file and stops the notifier."""
|
|
37 |
if self._watcher != None: |
|
38 |
self._watcher.cancel() |
|
39 |
del self._watcher |
|
40 |
self._watcher = None |
|
41 |
||
42 |
def get_file_length(self): |
|
43 |
"""Returns the length of the tracked file, returns 0 if there is an error,
|
|
44 |
assuming that the file may not have been created yet."""
|
|
45 |
try: |
|
46 |
f = open(self._path, "r") |
|
47 |
f.seek(0, os.SEEK_END) |
|
48 |
size = f.tell() |
|
49 |
f.close() |
|
50 |
return size |
|
51 |
except: |
|
52 |
return 0 |
|
53 |
||
54 |
def read_appended_text(self): |
|
55 |
"""Opens the tracked file, seeks to the last read offset, reads to EOF
|
|
56 |
and updates the last read offset."""
|
|
57 |
f = open(self._path, "r") |
|
58 |
f.seek(self._offset, os.SEEK_SET) |
|
59 |
text = f.read() |
|
60 |
self._offset = f.tell() |
|
61 |
f.close() |
|
62 |
return text |
|
63 |
||
64 |
def on_changed(self, monitor, file, other_file, event_type): |
|
65 |
"""Reads the appended text in the tracked file and sends it to the listeners."""
|
|
66 |
text = self.read_appended_text() |
|
67 |
if event_type == gio.FILE_MONITOR_EVENT_CHANGED: |
|
68 |
glib.idle_add(self.emit, "changed", text) |
|
69 |
||
70 |
||
71 |
if gtk.pygtk_version < (2, 8, 0): |
|
72 |
gobject.type_register(LogWatcher) |