chiark / gitweb /
systemd-python: export sd_j_get_fd, sd_j_reliable_fd, sd_j_close
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 Mar 2013 05:35:28 +0000 (00:35 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 Mar 2013 05:45:56 +0000 (00:45 -0500)
sd_journal_get_fd(j) is called j.fileno(), for compatiblity with
Python conventions for file-like objects.

More importantly, those new .seek_head() and .seek_tail() do not
call .get_next(). This is better, if one wants to skip before
retrieving an entry.

src/python-systemd/_reader.c
src/python-systemd/docs/journal.rst

index e5733f0bcaeec0f0a3bb92abe017950eeaebbe8c..c435dadecfab5c5fcd6a1a849bd4c1c1783c5f38 100644 (file)
@@ -124,6 +124,47 @@ static int Reader_init(Reader *self, PyObject *args, PyObject *keywds)
     return set_error(r, path, "Invalid flags or path");
 }
 
     return set_error(r, path, "Invalid flags or path");
 }
 
+PyDoc_STRVAR(Reader_fileno__doc__,
+             "fileno() -> int\n\n"
+             "Get a file descriptor to poll for changes in the journal.\n"
+             "This method invokes sd_journal_get_fd().\n"
+             "See man:sd_journal_get_fd(3).");
+static PyObject* Reader_fileno(Reader *self, PyObject *args)
+{
+    int r;
+    r = sd_journal_get_fd(self->j);
+    set_error(r, NULL, NULL);
+    if (r < 0)
+        return NULL;
+    return long_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_reliable_fd__doc__,
+             "reliable_fd() -> bool\n\n"
+             "Returns True iff the journal can be polled reliably.\n"
+             "This method invokes sd_journal_reliable_fd().\n"
+             "See man:sd_journal_reliable_fd(3).");
+static PyObject* Reader_reliable_fd(Reader *self, PyObject *args)
+{
+    int r;
+    r = sd_journal_reliable_fd(self->j);
+    set_error(r, NULL, NULL);
+    if (r < 0)
+        return NULL;
+    return PyBool_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_close__doc__,
+             "reliable_fd() -> None\n\n"
+             "Free resources allocated by this Reader object.\n"
+             "This method invokes sd_journal_close().\n"
+             "See man:sd_journal_close(3).");
+static PyObject* Reader_close(Reader *self, PyObject *args)
+{
+    sd_journal_close(self->j);
+    Py_RETURN_NONE;
+}
+
 PyDoc_STRVAR(Reader_get_next__doc__,
              "get_next([skip]) -> dict\n\n"
              "Return dictionary of the next log entry. Optional skip value will\n"
 PyDoc_STRVAR(Reader_get_next__doc__,
              "get_next([skip]) -> dict\n\n"
              "Return dictionary of the next log entry. Optional skip value will\n"
@@ -613,6 +654,9 @@ static PyGetSetDef Reader_getseters[] = {
 };
 
 static PyMethodDef Reader_methods[] = {
 };
 
 static PyMethodDef Reader_methods[] = {
+    {"fileno",          (PyCFunction) Reader_fileno, METH_NOARGS, Reader_fileno__doc__},
+    {"reliable_fd",     (PyCFunction) Reader_reliable_fd, METH_NOARGS, Reader_reliable_fd__doc__},
+    {"close",           (PyCFunction) Reader_close, METH_NOARGS, Reader_close__doc__},
     {"get_next",        (PyCFunction) Reader_get_next, METH_VARARGS, Reader_get_next__doc__},
     {"get_previous",    (PyCFunction) Reader_get_previous, METH_VARARGS, Reader_get_previous__doc__},
     {"add_match",       (PyCFunction) Reader_add_match, METH_VARARGS|METH_KEYWORDS, Reader_add_match__doc__},
     {"get_next",        (PyCFunction) Reader_get_next, METH_VARARGS, Reader_get_next__doc__},
     {"get_previous",    (PyCFunction) Reader_get_previous, METH_VARARGS, Reader_get_previous__doc__},
     {"add_match",       (PyCFunction) Reader_add_match, METH_VARARGS|METH_KEYWORDS, Reader_add_match__doc__},
index faa270746d9d38670c794452ef550d31dc67ddfa..9dc495ffdb685ff5b331f822598b82e2a6183366 100644 (file)
@@ -27,6 +27,22 @@ Accessing the Journal
 
 .. autoattribute:: systemd.journal.DEFAULT_CONVERTERS
 
 
 .. autoattribute:: systemd.journal.DEFAULT_CONVERTERS
 
+Example: polling for journal events
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This example shows that journal events can be waited for (using
+e.g. `poll`). This makes it easy to integrate Reader in an external
+event loop:
+
+  >>> import select
+  >>> from systemd import journal
+  >>> j = journal.Reader()
+  >>> j.seek_tail()
+  >>> p = select.poll()
+  >>> p.register(j, select.POLLIN)
+  >>> p.poll()
+  [(3, 1)]
+  >>> j.get_next()
 
 
 Journal access types
 
 
 Journal access types