From d688e56931f060e0e2333025046b0af307485580 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 29 Dec 2017 14:57:52 +0000 Subject: [PATCH] python: Add Writer class Signed-off-by: Michael Tremer --- Makefile.am | 4 +- src/python/locationmodule.c | 8 ++++ src/python/writer.c | 89 +++++++++++++++++++++++++++++++++++++ src/python/writer.h | 31 +++++++++++++ 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/python/writer.c create mode 100644 src/python/writer.h diff --git a/Makefile.am b/Makefile.am index 7061b05..d32821f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -89,7 +89,9 @@ src_python_location_la_SOURCES = \ src/python/as.c \ src/python/as.h \ src/python/database.c \ - src/python/database.h + src/python/database.h \ + src/python/writer.c \ + src/python/writer.h src_python_location_la_CFLAGS = \ $(AM_CFLAGS) \ diff --git a/src/python/locationmodule.c b/src/python/locationmodule.c index 67f796d..f4ea6cf 100644 --- a/src/python/locationmodule.c +++ b/src/python/locationmodule.c @@ -19,6 +19,7 @@ #include "locationmodule.h" #include "as.h" #include "database.h" +#include "writer.h" PyMODINIT_FUNC PyInit_location(void); @@ -65,5 +66,12 @@ PyMODINIT_FUNC PyInit_location(void) { Py_INCREF(&DatabaseType); PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType); + // Writer + if (PyType_Ready(&WriterType) < 0) + return NULL; + + Py_INCREF(&WriterType); + PyModule_AddObject(m, "Writer", (PyObject *)&WriterType); + return m; } diff --git a/src/python/writer.c b/src/python/writer.c new file mode 100644 index 0000000..989ef99 --- /dev/null +++ b/src/python/writer.c @@ -0,0 +1,89 @@ +/* + 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 +#include + +#include "locationmodule.h" +#include "writer.h" + +static PyObject* Writer_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { + WriterObject* self = (WriterObject*)type->tp_alloc(type, 0); + + return (PyObject*)self; +} + +static void Writer_dealloc(WriterObject* self) { + if (self->writer) + loc_writer_unref(self->writer); + + Py_TYPE(self)->tp_free((PyObject* )self); +} + +static int Writer_init(WriterObject* self, PyObject* args, PyObject* kwargs) { + // Create the writer object + int r = loc_writer_new(loc_ctx, &self->writer); + if (r) + return -1; + + return 0; +} + +static PyObject* Writer_get_vendor(WriterObject* self) { + const char* vendor = loc_writer_get_vendor(self->writer); + + return PyUnicode_FromString(vendor); +} + +static int Writer_set_vendor(WriterObject* self, PyObject* args) { + const char* vendor = NULL; + + if (!PyArg_ParseTuple(args, "s", &vendor)) + return -1; + + int r = loc_writer_set_vendor(self->writer, vendor); + if (r) { + PyErr_Format(PyExc_ValueError, "Could not set vendor: %s", vendor); + return r; + } + + return 0; +} + +static struct PyGetSetDef Writer_getsetters[] = { + { + "vendor", + (getter)Writer_get_vendor, + (setter)Writer_set_vendor, + NULL, + NULL, + }, + { NULL }, +}; + +PyTypeObject WriterType = { + PyVarObject_HEAD_INIT(NULL, 0) + tp_name: "location.Writer", + tp_basicsize: sizeof(WriterObject), + tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + tp_new: Writer_new, + tp_dealloc: (destructor)Writer_dealloc, + tp_init: (initproc)Writer_init, + tp_doc: "Writer object", + tp_getset: Writer_getsetters, +}; diff --git a/src/python/writer.h b/src/python/writer.h new file mode 100644 index 0000000..6fe4c8d --- /dev/null +++ b/src/python/writer.h @@ -0,0 +1,31 @@ +/* + 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_WRITER_H +#define PYTHON_LOCATION_WRITER_H + +#include + +#include + +typedef struct { + PyObject_HEAD + struct loc_writer* writer; +} WriterObject; + +extern PyTypeObject WriterType; + +#endif /* PYTHON_LOCATION_WRITER_H */ -- 2.39.2