]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
python: Implement lookup function for countries
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 15 Oct 2019 15:21:21 +0000 (15:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 15 Oct 2019 15:21:21 +0000 (15:21 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/database.c

index 48023b07c482d6755751a8f393acdef2984e68bf..ea476c807da591e572fed635f4def3d69b400685 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "locationmodule.h"
 #include "as.h"
+#include "country.h"
 #include "database.h"
 #include "network.h"
 
@@ -120,6 +121,24 @@ static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) {
        return NULL;
 }
 
+static PyObject* Database_get_country(DatabaseObject* self, PyObject* args) {
+       const char* country_code = NULL;
+
+       if (!PyArg_ParseTuple(args, "s", &country_code))
+               return NULL;
+
+       struct loc_country* country;
+       int r = loc_database_get_country(self->db, &country, country_code);
+       if (r) {
+               Py_RETURN_NONE;
+       }
+
+       PyObject* obj = new_country(&CountryType, country);
+       loc_country_unref(country);
+
+       return obj;
+}
+
 static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) {
        struct loc_network* network = NULL;
        const char* address = NULL;
@@ -231,6 +250,12 @@ static struct PyMethodDef Database_methods[] = {
                METH_VARARGS,
                NULL,
        },
+       {
+               "get_country",
+               (PyCFunction)Database_get_country,
+               METH_VARARGS,
+               NULL,
+       },
        {
                "lookup",
                (PyCFunction)Database_lookup,