]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
python: Access ASes from database
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 14:03:41 +0000 (14:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 14:04:02 +0000 (14:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/as.c
src/python/as.h
src/python/database.c

index 4aad074fc653add9c7aab9d06ec2d74475f9ff76..a28727d6f36d1de1aed3f8249e28a0b5b845a33a 100644 (file)
 #include "locationmodule.h"
 #include "as.h"
 
+PyObject* new_as(PyTypeObject* type, struct loc_as* as) {
+       ASObject* self = (ASObject*)type->tp_alloc(type, 0);
+       if (self) {
+               self->as = loc_as_ref(as);
+       }
+
+       return (PyObject*)self;
+}
+
 static PyObject* AS_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
        // Create stringpool
        struct loc_stringpool* pool;
index 223b84723d3facc8fa5535bb20752d1f6de8ce1a..19b5c73050eefdd413e9f856b53021c8dcbaf819 100644 (file)
@@ -32,4 +32,6 @@ typedef struct {
 
 extern PyTypeObject ASType;
 
+PyObject* new_as(PyTypeObject* type, struct loc_as* as);
+
 #endif /* PYTHON_LOCATION_AS_H */
index e38256bfd7db0198a6eaabc7ab3fad8ed82e9210..53b454c4f6dde3dd9f1fd28055fbdf64e70793d7 100644 (file)
@@ -20,6 +20,7 @@
 #include <loc/database.h>
 
 #include "locationmodule.h"
+#include "as.h"
 #include "database.h"
 
 static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
@@ -81,7 +82,37 @@ static PyObject* Database_get_created_at(DatabaseObject* self) {
        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);
+       if (r)
+               return NULL;
+
+       // Create an AS object
+       if (as) {
+               PyObject* obj = new_as(&ASType, as);
+               loc_as_unref(as);
+
+               return obj;
+       }
+
+       // Nothing found
+       Py_RETURN_NONE;
+}
+
 static struct PyMethodDef Database_methods[] = {
+       {
+               "get_as",
+               (PyCFunction)Database_get_as,
+               METH_VARARGS,
+               NULL,
+       },
        { NULL },
 };