]> git.ipfire.org Git - people/ms/libloc.git/blame - src/python/as.c
python: Do not use any GNU-style initialisers for structs
[people/ms/libloc.git] / src / python / as.c
CommitLineData
fadebc89
MT
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>
fadebc89 21
1da9cd39 22#include "locationmodule.h"
fadebc89
MT
23#include "as.h"
24
86ca7ef7
MT
25PyObject* new_as(PyTypeObject* type, struct loc_as* as) {
26 ASObject* self = (ASObject*)type->tp_alloc(type, 0);
27 if (self) {
28 self->as = loc_as_ref(as);
29 }
30
31 return (PyObject*)self;
32}
33
fadebc89 34static PyObject* AS_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
fadebc89 35 ASObject* self = (ASObject*)type->tp_alloc(type, 0);
fadebc89
MT
36
37 return (PyObject*)self;
38}
39
40static void AS_dealloc(ASObject* self) {
41 if (self->as)
42 loc_as_unref(self->as);
43
fadebc89
MT
44 Py_TYPE(self)->tp_free((PyObject* )self);
45}
46
47static int AS_init(ASObject* self, PyObject* args, PyObject* kwargs) {
48 uint32_t number = 0;
49
50 if (!PyArg_ParseTuple(args, "i", &number))
51 return -1;
52
53 // Create the AS object
29bd8211 54 int r = loc_as_new(loc_ctx, &self->as, number);
fadebc89
MT
55 if (r)
56 return -1;
57
58 return 0;
59}
60
2e07b5a7
MT
61static PyObject* AS_repr(ASObject* self) {
62 uint32_t number = loc_as_get_number(self->as);
63 const char* name = loc_as_get_name(self->as);
64
65 if (name)
66 return PyUnicode_FromFormat("<AS %d (%s)>", number, name);
67
68 return PyUnicode_FromFormat("<AS %d>", number);
69}
70
5118a4b8
MT
71static PyObject* AS_str(ASObject* self) {
72 uint32_t number = loc_as_get_number(self->as);
73 const char* name = loc_as_get_name(self->as);
74
75 if (name)
76 return PyUnicode_FromFormat("AS%d (%s)", number, name);
77
78 return PyUnicode_FromFormat("AS%d", number);
79}
80
fadebc89
MT
81static PyObject* AS_get_number(ASObject* self) {
82 uint32_t number = loc_as_get_number(self->as);
83
84 return PyLong_FromLong(number);
85}
86
87static PyObject* AS_get_name(ASObject* self) {
88 const char* name = loc_as_get_name(self->as);
89
90 return PyUnicode_FromString(name);
91}
92
360ae5ad
MT
93static int AS_set_name(ASObject* self, PyObject* value) {
94 const char* name = PyUnicode_AsUTF8(value);
1069a70e
MT
95
96 int r = loc_as_set_name(self->as, name);
97 if (r) {
98 PyErr_Format(PyExc_ValueError, "Could not set name: %s", name);
99 return r;
100 }
101
102 return 0;
103}
104
63e2ce17
MT
105static PyObject* AS_richcompare(ASObject* self, ASObject* other, int op) {
106 int r = loc_as_cmp(self->as, other->as);
107
108 switch (op) {
109 case Py_EQ:
110 if (r == 0)
111 Py_RETURN_TRUE;
112
113 Py_RETURN_FALSE;
114
115 case Py_LT:
116 if (r < 0)
117 Py_RETURN_TRUE;
118
119 Py_RETURN_FALSE;
120
121 default:
122 break;
123 }
124
125 Py_RETURN_NOTIMPLEMENTED;
126}
127
fadebc89
MT
128static struct PyGetSetDef AS_getsetters[] = {
129 {
130 "name",
131 (getter)AS_get_name,
1069a70e 132 (setter)AS_set_name,
fadebc89
MT
133 NULL,
134 NULL,
135 },
136 {
137 "number",
138 (getter)AS_get_number,
139 NULL,
140 NULL,
141 NULL,
142 },
143 { NULL },
144};
145
146PyTypeObject ASType = {
147 PyVarObject_HEAD_INIT(NULL, 0)
d42e1dcd
MT
148 .tp_name = "location.AS",
149 .tp_basicsize = sizeof(ASObject),
150 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
151 .tp_new = AS_new,
152 .tp_dealloc = (destructor)AS_dealloc,
153 .tp_init = (initproc)AS_init,
154 .tp_doc = "AS object",
155 .tp_getset = AS_getsetters,
156 .tp_repr = (reprfunc)AS_repr,
157 .tp_str = (reprfunc)AS_str,
158 .tp_richcompare = (richcmpfunc)AS_richcompare,
fadebc89 159};