]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
python: Implement Network class
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 20:21:45 +0000 (20:21 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 20:21:45 +0000 (20:21 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libloc.sym
src/python/locationmodule.c
src/python/network.c [new file with mode: 0644]
src/python/network.h [new file with mode: 0644]

index d32821f67873dce19bee28ac228b9a107da431ad..2fa01f90451406bd746bf4565229db3b12942455 100644 (file)
@@ -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
 
index 25fe5f5836c0c21fee2a7beb53ea876a815ea4e5..7218e7bd1f363d98014a4a9a4b671f4c7301f862 100644 (file)
@@ -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
index f4ea6cf6602000c66193740dc503c85cca8178c1..735d7dcf86f7bb2e53a93abf2157f9333e5b2fbb 100644 (file)
@@ -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 (file)
index 0000000..702faa8
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+       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 <errno.h>
+
+#include <loc/libloc.h>
+#include <loc/network.h>
+
+#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("<location.Network %s>", 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 (file)
index 0000000..4a4b263
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+       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_NETWORK_H
+#define PYTHON_LOCATION_NETWORK_H
+
+#include <Python.h>
+
+#include <loc/network.h>
+
+typedef struct {
+       PyObject_HEAD
+       struct loc_network* network;
+} NetworkObject;
+
+extern PyTypeObject NetworkType;
+
+#endif /* PYTHON_LOCATION_NETWORK_H */