From 39967361636d0468d8760d100d286e290d05c4f7 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 29 Dec 2017 20:21:45 +0000 Subject: [PATCH] python: Implement Network class Signed-off-by: Michael Tremer --- Makefile.am | 2 + src/libloc.sym | 1 + src/python/locationmodule.c | 8 +++ src/python/network.c | 107 ++++++++++++++++++++++++++++++++++++ src/python/network.h | 31 +++++++++++ 5 files changed, 149 insertions(+) create mode 100644 src/python/network.c create mode 100644 src/python/network.h diff --git a/Makefile.am b/Makefile.am index d32821f..2fa01f9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -90,6 +90,8 @@ src_python_location_la_SOURCES = \ src/python/as.h \ src/python/database.c \ src/python/database.h \ + src/python/network.c \ + src/python/network.h \ src/python/writer.c \ src/python/writer.h diff --git a/src/libloc.sym b/src/libloc.sym index 25fe5f5..7218e7b 100644 --- a/src/libloc.sym +++ b/src/libloc.sym @@ -25,6 +25,7 @@ global: loc_network_new; loc_network_new_from_string; loc_network_set_country_code; + loc_network_str; loc_network_unref; # Network Tree diff --git a/src/python/locationmodule.c b/src/python/locationmodule.c index f4ea6cf..735d7dc 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 "network.h" #include "writer.h" PyMODINIT_FUNC PyInit_location(void); @@ -66,6 +67,13 @@ PyMODINIT_FUNC PyInit_location(void) { Py_INCREF(&DatabaseType); PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType); + // Network + if (PyType_Ready(&NetworkType) < 0) + return NULL; + + Py_INCREF(&NetworkType); + PyModule_AddObject(m, "Network", (PyObject *)&NetworkType); + // Writer if (PyType_Ready(&WriterType) < 0) return NULL; diff --git a/src/python/network.c b/src/python/network.c new file mode 100644 index 0000000..702faa8 --- /dev/null +++ b/src/python/network.c @@ -0,0 +1,107 @@ +/* + 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 + +#include "locationmodule.h" +#include "network.h" + +static PyObject* Network_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { + NetworkObject* self = (NetworkObject*)type->tp_alloc(type, 0); + + return (PyObject*)self; +} + +static void Network_dealloc(NetworkObject* self) { + if (self->network) + loc_network_unref(self->network); + + Py_TYPE(self)->tp_free((PyObject* )self); +} + +static int Network_init(NetworkObject* self, PyObject* args, PyObject* kwargs) { + const char* network = NULL; + + if (!PyArg_ParseTuple(args, "s", &network)) + return -1; + + // Load the Network + int r = loc_network_new_from_string(loc_ctx, &self->network, network); + if (r) { + PyErr_Format(PyExc_ValueError, "Invalid network: %s", network); + return -1; + } + + return 0; +} + +static PyObject* Network_repr(NetworkObject* self) { + char* network = loc_network_str(self->network); + + PyObject* obj = PyUnicode_FromFormat("", network); + free(network); + + return obj; +} + +static PyObject* Network_get_country_code(NetworkObject* self) { + const char* country_code = loc_network_get_country_code(self->network); + + return PyUnicode_FromString(country_code); +} + +static int Network_set_country_code(NetworkObject* self, PyObject* value) { + const char* country_code = PyUnicode_AsUTF8(value); + + int r = loc_network_set_country_code(self->network, country_code); + if (r) { + if (r == -EINVAL) + PyErr_SetString(PyExc_ValueError, "Invalid country code"); + + return -1; + } + + return 0; +} + +static struct PyGetSetDef Network_getsetters[] = { + { + "country_code", + (getter)Network_get_country_code, + (setter)Network_set_country_code, + NULL, + NULL, + }, + { NULL }, +}; + +PyTypeObject NetworkType = { + PyVarObject_HEAD_INIT(NULL, 0) + tp_name: "location.Network", + tp_basicsize: sizeof(NetworkObject), + tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, + tp_new: Network_new, + tp_dealloc: (destructor)Network_dealloc, + tp_init: (initproc)Network_init, + tp_doc: "Network object", + tp_getset: Network_getsetters, + tp_repr: (reprfunc)Network_repr, +}; diff --git a/src/python/network.h b/src/python/network.h new file mode 100644 index 0000000..4a4b263 --- /dev/null +++ b/src/python/network.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_NETWORK_H +#define PYTHON_LOCATION_NETWORK_H + +#include + +#include + +typedef struct { + PyObject_HEAD + struct loc_network* network; +} NetworkObject; + +extern PyTypeObject NetworkType; + +#endif /* PYTHON_LOCATION_NETWORK_H */ -- 2.39.2