From 9cdf6c53933a62a455ae261ba7c46fa521cca183 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 29 Dec 2017 11:10:01 +0000 Subject: [PATCH] python: Add Database class Signed-off-by: Michael Tremer --- Makefile.am | 4 +- src/python/database.c | 83 +++++++++++++++++++++++++++++++++++++ src/python/database.h | 30 ++++++++++++++ src/python/locationmodule.c | 9 ++++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/python/database.c create mode 100644 src/python/database.h diff --git a/Makefile.am b/Makefile.am index 4a6aacf..30a7652 100644 --- a/Makefile.am +++ b/Makefile.am @@ -82,7 +82,9 @@ pkgpyexec_LTLIBRARIES = \ src/python/location.la src_python_location_la_SOURCES = \ - src/python/locationmodule.c + src/python/locationmodule.c \ + src/python/database.c \ + src/python/database.h src_python_location_la_CFLAGS = \ $(AM_CFLAGS) \ diff --git a/src/python/database.c b/src/python/database.c new file mode 100644 index 0000000..154debc --- /dev/null +++ b/src/python/database.c @@ -0,0 +1,83 @@ +/* + libloc - A library to determine the location of someone on the Internet + + Copyright (C) 2017 IPFire Development Team + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. +*/ + +#include + +#include "../database.h" +#include "database.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; +} + +static void Database_dealloc(DatabaseObject* self) { + if (self->db) + loc_database_unref(self->db); + + if (self->ctx) + loc_unref(self->ctx); + + Py_TYPE(self)->tp_free((PyObject* )self); +} + +static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) { + const char* path = NULL; + + if (!PyArg_ParseTuple(args, "s", &path)) + return -1; + + // Open the file for reading + FILE* f = fopen(path, "r"); + if (!f) + return -1; + + // Load the database + int r = loc_database_new(self->ctx, &self->db, f); + fclose(f); + + // Return on any errors + if (r) + return -1; + + return 0; +} + +static struct PyMethodDef Database_methods[] = { + { NULL }, +}; + +PyTypeObject DatabaseType = { + PyVarObject_HEAD_INIT(NULL, 0) + tp_name: "location.Database", + tp_basicsize: sizeof(DatabaseObject), + tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + tp_new: Database_new, + tp_dealloc: (destructor)Database_dealloc, + tp_init: (initproc)Database_init, + tp_doc: "Database object", + tp_methods: Database_methods, +}; diff --git a/src/python/database.h b/src/python/database.h new file mode 100644 index 0000000..8155cbd --- /dev/null +++ b/src/python/database.h @@ -0,0 +1,30 @@ +/* + libloc - A library to determine the location of someone on the Internet + + Copyright (C) 2017 IPFire Development Team + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. +*/ + +#ifndef PYTHON_LOCATION_DATABASE_H +#define PYTHON_LOCATION_DATABASE_H + +#include + +typedef struct { + PyObject_HEAD + struct loc_ctx* ctx; + struct loc_database* db; +} DatabaseObject; + +extern PyTypeObject DatabaseType; + +#endif /* PYTHON_LOCATION_DATABASE_H */ diff --git a/src/python/locationmodule.c b/src/python/locationmodule.c index dfd951d..02aeb32 100644 --- a/src/python/locationmodule.c +++ b/src/python/locationmodule.c @@ -16,6 +16,8 @@ #include +#include "database.h" + PyMODINIT_FUNC PyInit_location(void); static PyMethodDef location_module_methods[] = { @@ -35,5 +37,12 @@ PyMODINIT_FUNC PyInit_location(void) { if (!m) return NULL; + // Database + if (PyType_Ready(&DatabaseType) < 0) + return NULL; + + Py_INCREF(&DatabaseType); + PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType); + return m; } -- 2.39.2