]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/python/database.c
Add license attribute to the database
[people/ms/libloc.git] / src / python / database.c
index 6d2a54193b818ff862f5b4e0cccbc474488dcb65..d41da47e52813c23ecbb94da3d27bae998f3c6d6 100644 (file)
 #include <loc/libloc.h>
 #include <loc/database.h>
 
+#include "locationmodule.h"
+#include "as.h"
 #include "database.h"
+#include "network.h"
 
 static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
-       // Create libloc context
-       struct loc_ctx* ctx;
-       int r = loc_new(&ctx);
-       if (r)
-               return NULL;
-
        DatabaseObject* self = (DatabaseObject*)type->tp_alloc(type, 0);
-       if (self) {
-               self->ctx = ctx;
-       }
 
        return (PyObject*)self;
 }
@@ -40,8 +34,8 @@ static void Database_dealloc(DatabaseObject* self) {
        if (self->db)
                loc_database_unref(self->db);
 
-       if (self->ctx)
-               loc_unref(self->ctx);
+       if (self->path)
+               free(self->path);
 
        Py_TYPE(self)->tp_free((PyObject* )self);
 }
@@ -52,13 +46,15 @@ static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs)
        if (!PyArg_ParseTuple(args, "s", &path))
                return -1;
 
+       self->path = strdup(path);
+
        // Open the file for reading
-       FILE* f = fopen(path, "r");
+       FILE* f = fopen(self->path, "r");
        if (!f)
                return -1;
 
        // Load the database
-       int r = loc_database_new(self->ctx, &self->db, f);
+       int r = loc_database_new(loc_ctx, &self->db, f);
        fclose(f);
 
        // Return on any errors
@@ -68,6 +64,10 @@ static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs)
        return 0;
 }
 
+static PyObject* Database_repr(DatabaseObject* self) {
+       return PyUnicode_FromFormat("<Database %s>", self->path);
+}
+
 static PyObject* Database_get_description(DatabaseObject* self) {
        const char* description = loc_database_get_description(self->db);
 
@@ -80,11 +80,94 @@ static PyObject* Database_get_vendor(DatabaseObject* self) {
        return PyUnicode_FromString(vendor);
 }
 
+static PyObject* Database_get_license(DatabaseObject* self) {
+       const char* license = loc_database_get_license(self->db);
+
+       return PyUnicode_FromString(license);
+}
+
+static PyObject* Database_get_created_at(DatabaseObject* self) {
+       time_t created_at = loc_database_created_at(self->db);
+
+       return PyLong_FromLong(created_at);
+}
+
+static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) {
+       struct loc_as* as = NULL;
+       uint32_t number = 0;
+
+       if (!PyArg_ParseTuple(args, "i", &number))
+               return NULL;
+
+       // Try to retrieve the AS
+       int r = loc_database_get_as(self->db, &as, number);
+
+       // We got an AS
+       if (r == 0) {
+               PyObject* obj = new_as(&ASType, as);
+               loc_as_unref(as);
+
+               return obj;
+
+       // Nothing found
+       } else if (r == 1) {
+               Py_RETURN_NONE;
+       }
+
+       // Unexpected error
+       return NULL;
+}
+
+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",
+               (PyCFunction)Database_get_as,
+               METH_VARARGS,
+               NULL,
+       },
+       {
+               "lookup",
+               (PyCFunction)Database_lookup,
+               METH_VARARGS,
+               NULL,
+       },
        { NULL },
 };
 
 static struct PyGetSetDef Database_getsetters[] = {
+       {
+               "created_at",
+               (getter)Database_get_created_at,
+               NULL,
+               NULL,
+               NULL,
+       },
        {
                "description",
                (getter)Database_get_description,
@@ -92,12 +175,19 @@ static struct PyGetSetDef Database_getsetters[] = {
                NULL,
                NULL,
        },
+       {
+               "license",
+               (getter)Database_get_license,
+               NULL,
+               NULL,
+               NULL,
+       },
        {
                "vendor",
                (getter)Database_get_vendor,
                NULL,
                NULL,
-               NULL
+               NULL,
        },
        { NULL },
 };
@@ -113,4 +203,5 @@ PyTypeObject DatabaseType = {
        tp_doc:                 "Database object",
        tp_methods:             Database_methods,
        tp_getset:              Database_getsetters,
+       tp_repr:                (reprfunc)Database_repr,
 };