python: Add Writer class
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 14:57:52 +0000 (14:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 14:57:52 +0000 (14:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/python/locationmodule.c
src/python/writer.c [new file with mode: 0644]
src/python/writer.h [new file with mode: 0644]

index 7061b05..d32821f 100644 (file)
@@ -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) \
index 67f796d..f4ea6cf 100644 (file)
@@ -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 (file)
index 0000000..989ef99
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+       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 <loc/libloc.h>
+#include <loc/writer.h>
+
+#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 (file)
index 0000000..6fe4c8d
--- /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_WRITER_H
+#define PYTHON_LOCATION_WRITER_H
+
+#include <Python.h>
+
+#include <loc/writer.h>
+
+typedef struct {
+       PyObject_HEAD
+       struct loc_writer* writer;
+} WriterObject;
+
+extern PyTypeObject WriterType;
+
+#endif /* PYTHON_LOCATION_WRITER_H */