chiark / gitweb /
systemd-python: add journal.get_catalog()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 15 Mar 2013 22:10:51 +0000 (18:10 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 20 Mar 2013 01:50:42 +0000 (21:50 -0400)
This one wraps sd_journal_get_catalog_from_message_id.
Thanks to Python namespacing, we can stick to a shorter name.

TODO
src/python-systemd/_reader.c
src/python-systemd/journal.py

diff --git a/TODO b/TODO
index 750540fb12797c1ca60f7a16eec49aad5b43623d..b44170024baf35ec53f3362a1c67a12db55ee3d6 100644 (file)
--- a/TODO
+++ b/TODO
@@ -581,7 +581,6 @@ Features:
 * drop cap bounding set in readahead and other services
 
 * systemd-python:
-   - export sd_journal_get_catalog_for_message_id (in systemd.id128)
    - allow reading of only select fields in systemd.journal._reader.Reader
    - export sd_journal_test_cursor in systemd.journal._reader.Reader
    - export sd_journal_get_usage in systemd.journal._reader.Reader
index a257757e127187404f6754a890678d70949b78ea..2feb091c26e5a5f9bf44cc932ecf90dd87d226b3 100644 (file)
@@ -50,6 +50,11 @@ static int set_error(int r, const char* path, const char* invalid_message) {
     return -1;
 }
 
+
+PyDoc_STRVAR(module__doc__,
+             "Class to reads the systemd journal similar to journalctl.");
+
+
 #if PY_MAJOR_VERSION >= 3
 static PyTypeObject MonotonicType;
 
@@ -657,6 +662,37 @@ static PyObject* Reader_get_catalog(Reader *self, PyObject *args)
 }
 
 
+PyDoc_STRVAR(get_catalog__doc__,
+             "get_catalog(id128) -> str\n\n"
+             "Retrieve a message catalog entry for the given id.\n"
+             "Wraps man:sd_journal_get_catalog_for_message_id(3).");
+static PyObject* get_catalog(PyObject *self, PyObject *args)
+{
+    int r;
+    char *id_ = NULL;
+    sd_id128_t id;
+    char _cleanup_free_ *msg = NULL;
+
+    assert(!self);
+    assert(args);
+
+    if (!PyArg_ParseTuple(args, "z", &id_))
+        return NULL;
+
+    r = sd_id128_from_string(id_, &id);
+    if (set_error(r, NULL, "Invalid id128"))
+        return NULL;
+
+    Py_BEGIN_ALLOW_THREADS
+    r = sd_journal_get_catalog_for_message_id(id, &msg);
+    Py_END_ALLOW_THREADS
+    if (set_error(r, NULL, NULL))
+        return NULL;
+
+    return unicode_FromString(msg);
+}
+
+
 PyDoc_STRVAR(data_threshold__doc__,
              "Threshold for field size truncation in bytes.\n\n"
              "Fields longer than this will be truncated to the threshold size.\n"
@@ -774,16 +810,19 @@ static PyTypeObject ReaderType = {
     PyType_GenericNew,                        /* tp_new */
 };
 
-#define SUMMARY \
-    "Module that reads the systemd journal similar to journalctl."
+static PyMethodDef methods[] = {
+        { "get_catalog", get_catalog, METH_VARARGS, get_catalog__doc__},
+        { NULL, NULL, 0, NULL }        /* Sentinel */
+};
 
 #if PY_MAJOR_VERSION >= 3
-static PyModuleDef _reader_module = {
+static PyModuleDef module = {
     PyModuleDef_HEAD_INIT,
     "_reader",
-    SUMMARY,
+    module__doc__,
     -1,
-    NULL, NULL, NULL, NULL, NULL
+    methods,
+    NULL, NULL, NULL, NULL
 };
 #endif
 
@@ -813,7 +852,7 @@ init_reader(void)
 #endif
 
 #if PY_MAJOR_VERSION >= 3
-    m = PyModule_Create(&_reader_module);
+    m = PyModule_Create(&module);
     if (m == NULL)
         return NULL;
 
@@ -822,7 +861,7 @@ init_reader(void)
         initialized = true;
     }
 #else
-    m = Py_InitModule3("_reader", NULL, SUMMARY);
+    m = Py_InitModule3("_reader", methods, module__doc__);
     if (m == NULL)
         return;
 #endif
index 146f59f220e1d14a072b1d513000ada835972c43..fee39c9e088edea5f6746ff5cda864acfda3650d 100644 (file)
@@ -34,7 +34,8 @@ from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
                     LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
 from ._journal import sendv, stream_fd
 from ._reader import (_Reader, NOP, APPEND, INVALIDATE,
-                      LOCAL_ONLY, RUNTIME_ONLY, SYSTEM_ONLY)
+                      LOCAL_ONLY, RUNTIME_ONLY, SYSTEM_ONLY,
+                      get_catalog)
 from . import id128 as _id128
 
 if _sys.version_info >= (3,):