]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/python/database.c
python: Make lookup function available
[people/ms/libloc.git] / src / python / database.c
index 69399b3db888d4ff419c8d8d2f84e1b148119ab8..931b54d4040e3650cec27b265b8ff61ac065d24e 100644 (file)
@@ -22,6 +22,7 @@
 #include "locationmodule.h"
 #include "as.h"
 #include "database.h"
+#include "network.h"
 
 static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
        DatabaseObject* self = (DatabaseObject*)type->tp_alloc(type, 0);
@@ -100,6 +101,32 @@ static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) {
        Py_RETURN_NONE;
 }
 
+static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) {
+       struct loc_network* network = NULL;
+       const char* address = NULL;
+
+       if (!PyArg_ParseTuple(args, "s", &address))
+               return NULL;
+
+       // Try to retrieve a matching network
+       int r = loc_database_lookup_from_string(self->db, address, &network);
+
+       // We got a network
+       if (r == 0) {
+               PyObject* obj = new_network(&NetworkType, network);
+               loc_network_unref(network);
+
+               return obj;
+
+       // Nothing found
+       } else if (r == 1) {
+               Py_RETURN_NONE;
+       }
+
+       // Unexpected error
+       return NULL;
+}
+
 static struct PyMethodDef Database_methods[] = {
        {
                "get_as",
@@ -107,6 +134,12 @@ static struct PyMethodDef Database_methods[] = {
                METH_VARARGS,
                NULL,
        },
+       {
+               "lookup",
+               (PyCFunction)Database_lookup,
+               METH_VARARGS,
+               NULL,
+       },
        { NULL },
 };