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

index 0572567f03df5d3b36c74d5ea47a2ca9d6f95cf1..ea82663e0a03e93c7bea83075013c6c055d1ee91 100644 (file)
@@ -85,6 +85,8 @@ pkgpyexec_LTLIBRARIES = \
 
 src_python_location_la_SOURCES = \
        src/python/locationmodule.c \
+       src/python/as.c \
+       src/python/as.h \
        src/python/database.c \
        src/python/database.h
 
diff --git a/src/python/as.c b/src/python/as.c
new file mode 100644 (file)
index 0000000..83aea34
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+       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/as.h>
+#include <loc/stringpool.h>
+
+#include "as.h"
+
+static PyObject* AS_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
+       // Create libloc context
+       struct loc_ctx* ctx;
+       int r = loc_new(&ctx);
+       if (r)
+               return NULL;
+
+       // Create stringpool
+       struct loc_stringpool* pool;
+       r = loc_stringpool_new(ctx, &pool);
+       if (r) {
+               loc_unref(ctx);
+               return NULL;
+       }
+
+       ASObject* self = (ASObject*)type->tp_alloc(type, 0);
+       if (self) {
+               self->ctx = ctx;
+               self->pool = pool;
+       }
+
+       return (PyObject*)self;
+}
+
+static void AS_dealloc(ASObject* self) {
+       if (self->as)
+               loc_as_unref(self->as);
+
+       if (self->pool)
+               loc_stringpool_unref(self->pool);
+
+       if (self->ctx)
+               loc_unref(self->ctx);
+
+       Py_TYPE(self)->tp_free((PyObject* )self);
+}
+
+static int AS_init(ASObject* self, PyObject* args, PyObject* kwargs) {
+       uint32_t number = 0;
+
+       if (!PyArg_ParseTuple(args, "i", &number))
+               return -1;
+
+       // Create the AS object
+       int r = loc_as_new(self->ctx, self->pool, &self->as, number);
+       if (r)
+               return -1;
+
+       return 0;
+}
+
+static PyObject* AS_get_number(ASObject* self) {
+       uint32_t number = loc_as_get_number(self->as);
+
+       return PyLong_FromLong(number);
+}
+
+static PyObject* AS_get_name(ASObject* self) {
+       const char* name = loc_as_get_name(self->as);
+
+       return PyUnicode_FromString(name);
+}
+
+static struct PyGetSetDef AS_getsetters[] = {
+       {
+               "name",
+               (getter)AS_get_name,
+               NULL,
+               NULL,
+               NULL,
+       },
+       {
+               "number",
+               (getter)AS_get_number,
+               NULL,
+               NULL,
+               NULL,
+       },
+       { NULL },
+};
+
+PyTypeObject ASType = {
+       PyVarObject_HEAD_INIT(NULL, 0)
+       tp_name:                "location.AS",
+       tp_basicsize:           sizeof(ASObject),
+       tp_flags:               Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+       tp_new:                 AS_new,
+       tp_dealloc:             (destructor)AS_dealloc,
+       tp_init:                (initproc)AS_init,
+       tp_doc:                 "AS object",
+       tp_getset:              AS_getsetters,
+};
diff --git a/src/python/as.h b/src/python/as.h
new file mode 100644 (file)
index 0000000..223b847
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+       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_AS_H
+#define PYTHON_LOCATION_AS_H
+
+#include <Python.h>
+
+#include <loc/libloc.h>
+#include <loc/as.h>
+#include <loc/stringpool.h>
+
+typedef struct {
+       PyObject_HEAD
+       struct loc_ctx* ctx;
+       struct loc_stringpool* pool;
+       struct loc_as* as;
+} ASObject;
+
+extern PyTypeObject ASType;
+
+#endif /* PYTHON_LOCATION_AS_H */
index 02aeb32fc5853ea020e4fbc1c0e11b6edf42d180..39a7a1d30a4f30a6a9d656de04076e8af6fa0a55 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <Python.h>
 
+#include "as.h"
 #include "database.h"
 
 PyMODINIT_FUNC PyInit_location(void);
@@ -37,6 +38,13 @@ PyMODINIT_FUNC PyInit_location(void) {
        if (!m)
                return NULL;
 
+       // AS
+       if (PyType_Ready(&ASType) < 0)
+               return NULL;
+
+       Py_INCREF(&ASType);
+       PyModule_AddObject(m, "AS", (PyObject *)&ASType);
+
        // Database
        if (PyType_Ready(&DatabaseType) < 0)
                return NULL;