]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/as.c
a28727d6f36d1de1aed3f8249e28a0b5b845a33a
[people/ms/libloc.git] / src / python / as.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15 */
16
17 #include <Python.h>
18
19 #include <loc/libloc.h>
20 #include <loc/as.h>
21 #include <loc/stringpool.h>
22
23 #include "locationmodule.h"
24 #include "as.h"
25
26 PyObject* new_as(PyTypeObject* type, struct loc_as* as) {
27 ASObject* self = (ASObject*)type->tp_alloc(type, 0);
28 if (self) {
29 self->as = loc_as_ref(as);
30 }
31
32 return (PyObject*)self;
33 }
34
35 static PyObject* AS_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
36 // Create stringpool
37 struct loc_stringpool* pool;
38 int r = loc_stringpool_new(loc_ctx, &pool);
39 if (r)
40 return NULL;
41
42 ASObject* self = (ASObject*)type->tp_alloc(type, 0);
43 if (self) {
44 self->ctx = loc_ref(loc_ctx);
45 self->pool = pool;
46 }
47
48 return (PyObject*)self;
49 }
50
51 static void AS_dealloc(ASObject* self) {
52 if (self->as)
53 loc_as_unref(self->as);
54
55 if (self->pool)
56 loc_stringpool_unref(self->pool);
57
58 if (self->ctx)
59 loc_unref(self->ctx);
60
61 Py_TYPE(self)->tp_free((PyObject* )self);
62 }
63
64 static int AS_init(ASObject* self, PyObject* args, PyObject* kwargs) {
65 uint32_t number = 0;
66
67 if (!PyArg_ParseTuple(args, "i", &number))
68 return -1;
69
70 // Create the AS object
71 int r = loc_as_new(self->ctx, self->pool, &self->as, number);
72 if (r)
73 return -1;
74
75 return 0;
76 }
77
78 static PyObject* AS_get_number(ASObject* self) {
79 uint32_t number = loc_as_get_number(self->as);
80
81 return PyLong_FromLong(number);
82 }
83
84 static PyObject* AS_get_name(ASObject* self) {
85 const char* name = loc_as_get_name(self->as);
86
87 return PyUnicode_FromString(name);
88 }
89
90 static struct PyGetSetDef AS_getsetters[] = {
91 {
92 "name",
93 (getter)AS_get_name,
94 NULL,
95 NULL,
96 NULL,
97 },
98 {
99 "number",
100 (getter)AS_get_number,
101 NULL,
102 NULL,
103 NULL,
104 },
105 { NULL },
106 };
107
108 PyTypeObject ASType = {
109 PyVarObject_HEAD_INIT(NULL, 0)
110 tp_name: "location.AS",
111 tp_basicsize: sizeof(ASObject),
112 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
113 tp_new: AS_new,
114 tp_dealloc: (destructor)AS_dealloc,
115 tp_init: (initproc)AS_init,
116 tp_doc: "AS object",
117 tp_getset: AS_getsetters,
118 };