]> git.ipfire.org Git - location/libloc.git/commitdiff
python: Add Database class
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 11:10:01 +0000 (11:10 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 11:10:01 +0000 (11:10 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/python/database.c [new file with mode: 0644]
src/python/database.h [new file with mode: 0644]
src/python/locationmodule.c

index 4a6aacf91b6a99a99b01e3199ebd5866ed4f2a1e..30a7652095693f78e3f8ab41aef666423fa6710f 100644 (file)
@@ -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 (file)
index 0000000..154debc
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+       libloc - A library to determine the location of someone on the Internet
+
+       Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
+
+       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 <Python.h>
+
+#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 (file)
index 0000000..8155cbd
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+       libloc - A library to determine the location of someone on the Internet
+
+       Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
+
+       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 <Python.h>
+
+typedef struct {
+       PyObject_HEAD
+       struct loc_ctx* ctx;
+       struct loc_database* db;
+} DatabaseObject;
+
+extern PyTypeObject DatabaseType;
+
+#endif /* PYTHON_LOCATION_DATABASE_H */
index dfd951ddf25108148bda98bd2e391eec44c3318d..02aeb32fc5853ea020e4fbc1c0e11b6edf42d180 100644 (file)
@@ -16,6 +16,8 @@
 
 #include <Python.h>
 
+#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;
 }