chiark / gitweb /
systemd-python: wrap sd_login_monitor
[elogind.git] / src / python-systemd / _reader.c
index 05993b33cd2644c79fb6f814ae566917270f9354..c4c4fdfe1d19ddc29a2c4b3ab8c027997739ee42 100644 (file)
@@ -30,6 +30,7 @@
 #include "pyutil.h"
 #include "macro.h"
 #include "util.h"
+#include "build.h"
 
 typedef struct {
     PyObject_HEAD
@@ -37,20 +38,6 @@ typedef struct {
 } Reader;
 static PyTypeObject ReaderType;
 
-static int set_error(int r, const char* path, const char* invalid_message) {
-    if (r >= 0)
-        return r;
-    if (r == -EINVAL && invalid_message)
-        PyErr_SetString(PyExc_ValueError, invalid_message);
-    else if (r == -ENOMEM)
-        PyErr_SetString(PyExc_MemoryError, "Not enough memory");
-    else {
-        errno = -r;
-        PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
-    }
-    return -1;
-}
-
 
 PyDoc_STRVAR(module__doc__,
              "Class to reads the systemd journal similar to journalctl.");
@@ -176,7 +163,7 @@ PyDoc_STRVAR(Reader_get_timeout__doc__,
              "Returns a timeout value for usage in poll(), the time since the\n"
              "epoch of clock_gettime(2) in microseconds, or None if no timeout\n"
              "is necessary.\n\n"
-             "The return value must be converted to a relative timeout in \n"
+             "The return value must be converted to a relative timeout in\n"
              "milliseconds if it is to be used as an argument for poll().\n"
              "See man:sd_journal_get_timeout(3) for further discussion.");
 static PyObject* Reader_get_timeout(Reader *self, PyObject *args)
@@ -212,19 +199,7 @@ static PyObject* Reader_get_timeout_ms(Reader *self, PyObject *args)
     if (r < 0)
         return NULL;
 
-    if (t == (uint64_t) -1)
-        return PyLong_FromLong(-1);
-    else {
-        struct timespec ts;
-        uint64_t n;
-        int msec;
-
-        clock_gettime(CLOCK_MONOTONIC, &ts);
-        n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
-        msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
-
-        return PyLong_FromLong(msec);
-    }
+    return absolute_timeout(t);
 }
 
 
@@ -286,11 +261,7 @@ PyDoc_STRVAR(Reader___exit____doc__,
              "Closes the journal.\n");
 static PyObject* Reader___exit__(Reader *self, PyObject *args)
 {
-    assert(self);
-
-    sd_journal_close(self->j);
-    self->j = NULL;
-    Py_RETURN_NONE;
+    return Reader_close(self, args);
 }
 
 
@@ -567,7 +538,10 @@ static PyObject* Reader_add_match(Reader *self, PyObject *args, PyObject *keywds
 
 PyDoc_STRVAR(Reader_add_disjunction__doc__,
              "add_disjunction() -> None\n\n"
-             "Inserts a logical OR between matches added before and afterwards.");
+             "Inserts a logical OR between matches added since previous\n"
+             "add_disjunction() or add_conjunction() and the next\n"
+             "add_disjunction() or add_conjunction().\n\n"
+             "See man:sd_journal_add_disjunction(3) for explanation.");
 static PyObject* Reader_add_disjunction(Reader *self, PyObject *args)
 {
     int r;
@@ -579,6 +553,23 @@ static PyObject* Reader_add_disjunction(Reader *self, PyObject *args)
 }
 
 
+PyDoc_STRVAR(Reader_add_conjunction__doc__,
+             "add_conjunction() -> None\n\n"
+             "Inserts a logical AND between matches added since previous\n"
+             "add_disjunction() or add_conjunction() and the next\n"
+             "add_disjunction() or add_conjunction().\n\n"
+             "See man:sd_journal_add_disjunction(3) for explanation.");
+static PyObject* Reader_add_conjunction(Reader *self, PyObject *args)
+{
+    int r;
+    r = sd_journal_add_conjunction(self->j);
+    set_error(r, NULL, NULL);
+    if (r < 0)
+        return NULL;
+    Py_RETURN_NONE;
+}
+
+
 PyDoc_STRVAR(Reader_flush_matches__doc__,
              "flush_matches() -> None\n\n"
              "Clear all current match filters.");
@@ -980,6 +971,7 @@ static PyMethodDef Reader_methods[] = {
     {"_get_monotonic",  (PyCFunction) Reader_get_monotonic, METH_NOARGS, Reader_get_monotonic__doc__},
     {"add_match",       (PyCFunction) Reader_add_match, METH_VARARGS|METH_KEYWORDS, Reader_add_match__doc__},
     {"add_disjunction", (PyCFunction) Reader_add_disjunction, METH_NOARGS, Reader_add_disjunction__doc__},
+    {"add_conjunction", (PyCFunction) Reader_add_conjunction, METH_NOARGS, Reader_add_conjunction__doc__},
     {"flush_matches",   (PyCFunction) Reader_flush_matches, METH_NOARGS, Reader_flush_matches__doc__},
     {"seek_head",       (PyCFunction) Reader_seek_head, METH_NOARGS, Reader_seek_head__doc__},
     {"seek_tail",       (PyCFunction) Reader_seek_tail, METH_NOARGS, Reader_seek_tail__doc__},
@@ -1105,7 +1097,8 @@ init_reader(void)
         PyModule_AddIntConstant(m, "INVALIDATE", SD_JOURNAL_INVALIDATE) ||
         PyModule_AddIntConstant(m, "LOCAL_ONLY", SD_JOURNAL_LOCAL_ONLY) ||
         PyModule_AddIntConstant(m, "RUNTIME_ONLY", SD_JOURNAL_RUNTIME_ONLY) ||
-        PyModule_AddIntConstant(m, "SYSTEM_ONLY", SD_JOURNAL_SYSTEM_ONLY)) {
+        PyModule_AddIntConstant(m, "SYSTEM_ONLY", SD_JOURNAL_SYSTEM_ONLY) ||
+        PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
 #if PY_MAJOR_VERSION >= 3
         Py_DECREF(m);
         return NULL;