From 86ca7ef731d61e650a777bd503413676fdc250d4 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 29 Dec 2017 14:03:41 +0000 Subject: [PATCH] python: Access ASes from database Signed-off-by: Michael Tremer --- src/python/as.c | 9 +++++++++ src/python/as.h | 2 ++ src/python/database.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/src/python/as.c b/src/python/as.c index 4aad074..a28727d 100644 --- a/src/python/as.c +++ b/src/python/as.c @@ -23,6 +23,15 @@ #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; diff --git a/src/python/as.h b/src/python/as.h index 223b847..19b5c73 100644 --- a/src/python/as.h +++ b/src/python/as.h @@ -32,4 +32,6 @@ typedef struct { extern PyTypeObject ASType; +PyObject* new_as(PyTypeObject* type, struct loc_as* as); + #endif /* PYTHON_LOCATION_AS_H */ diff --git a/src/python/database.c b/src/python/database.c index e38256b..53b454c 100644 --- a/src/python/database.c +++ b/src/python/database.c @@ -20,6 +20,7 @@ #include #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 }, }; -- 2.39.2