summaryrefslogtreecommitdiff
path: root/systemd/journal.py
diff options
context:
space:
mode:
authorSteven Hiscocks <steven@hiscocks.me.uk>2013-04-14 20:55:08 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2015-07-05 14:19:19 -0400
commit6238295ba705b0e2aba4326d33527c762328f79e (patch)
tree446a3b99da1540df97b486945393d28272980269 /systemd/journal.py
parent81d7790578f22673e0567a6a7cd37408248f7d8c (diff)
downloadpython-systemd-6238295ba705b0e2aba4326d33527c762328f79e.tar.gz
python-systemd: Reader return special fields and _Reader changes
Changes to _Reader make it match closer to C API, by removing `get_next` and `get_previous`. A `get_all` method added, which returns dictionary of fields using C API SD_JOURNAL_FOREACH_DATA macro, which can be used in conjunction with `next`. _Reader `get`, `next`, `get_{realtime,monotonic,cursor}` and new `previous` methods are made private. This is so the traversal and getting of journal fields can be made transparent in the python interface. Reader now solely implements `get_next` and `get_previous`, returning a standard dictionary (future: other mapping types?) with all standard and special fields through the converters. This makes the output the same as journalctl json/export format output. Iterator methods also moved to Reader, as they do not function as intend with changes to _Reader. These changes also mean that more optimised journal interfaces can be made more easily from _Reader, by avoiding getting of unrequired fields by using the `_get` method, and avoiding field conversions.
Diffstat (limited to 'systemd/journal.py')
-rw-r--r--systemd/journal.py45
1 files changed, 42 insertions, 3 deletions
diff --git a/systemd/journal.py b/systemd/journal.py
index 48f57ac..6c740b0 100644
--- a/systemd/journal.py
+++ b/systemd/journal.py
@@ -176,6 +176,25 @@ class Reader(_Reader):
result[key] = self._convert_field(key, value)
return result
+ def __iter__(self):
+ """Part of iterator protocol.
+ Returns self.
+ """
+ return self
+
+ if _sys.version_info >= (3,):
+ def __next__(self):
+ """Part of iterator protocol.
+ Returns self.get_next().
+ """
+ return self.get_next()
+ else:
+ def next(self):
+ """Part of iterator protocol.
+ Returns self.get_next().
+ """
+ return self.get_next()
+
def add_match(self, *args, **kwargs):
"""Add one or more matches to the filter journal log entries.
All matches of different field are combined in a logical AND,
@@ -190,15 +209,35 @@ class Reader(_Reader):
super(Reader, self).add_match(arg)
def get_next(self, skip=1):
- """Return the next log entry as a dictionary of fields.
+ """Return the next log entry as a mapping type, currently
+ a standard dictionary of fields.
Optional skip value will return the `skip`\-th log entry.
Entries will be processed with converters specified during
Reader creation.
"""
- return self._convert_entry(
- super(Reader, self).get_next(skip))
+ if super(Reader, self)._next(skip):
+ entry = super(Reader, self)._get_all()
+ if entry:
+ entry['__REALTIME_TIMESTAMP'] = self._get_realtime()
+ entry['__MONOTONIC_TIMESTAMP'] = self._get_monotonic()
+ entry['__CURSOR'] = self._get_cursor()
+ return self._convert_entry(entry)
+ return dict()
+
+ def get_previous(self, skip=1):
+ """Return the previous log entry as a mapping type,
+ currently a standard dictionary of fields.
+
+ Optional skip value will return the -`skip`\-th log entry.
+
+ Entries will be processed with converters specified during
+ Reader creation.
+
+ Equivilent to get_next(-skip).
+ """
+ return self.get_next(-skip)
def query_unique(self, field):
"""Return unique values appearing in the journal for given `field`.