]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/as.c
python: Impement setting the name of an AS
[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
22 #include "locationmodule.h"
23 #include "as.h"
24
25 PyObject* 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
34 static PyObject* AS_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
35 ASObject* self = (ASObject*)type->tp_alloc(type, 0);
36
37 return (PyObject*)self;
38 }
39
40 static void AS_dealloc(ASObject* self) {
41 if (self->as)
42 loc_as_unref(self->as);
43
44 Py_TYPE(self)->tp_free((PyObject* )self);
45 }
46
47 static 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
54 int r = loc_as_new(loc_ctx, NULL, &self->as, number);
55 if (r)
56 return -1;
57
58 return 0;
59 }
60
61 static 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
71 static PyObject* AS_get_number(ASObject* self) {
72 uint32_t number = loc_as_get_number(self->as);
73
74 return PyLong_FromLong(number);
75 }
76
77 static PyObject* AS_get_name(ASObject* self) {
78 const char* name = loc_as_get_name(self->as);
79
80 return PyUnicode_FromString(name);
81 }
82
83 static int AS_set_name(ASObject* self, PyObject* args) {
84 const char* name = NULL;
85
86 if (!PyArg_ParseTuple(args, "s", &name))
87 return -1;
88
89 int r = loc_as_set_name(self->as, name);
90 if (r) {
91 PyErr_Format(PyExc_ValueError, "Could not set name: %s", name);
92 return r;
93 }
94
95 return 0;
96 }
97
98 static PyObject* AS_richcompare(ASObject* self, ASObject* other, int op) {
99 int r = loc_as_cmp(self->as, other->as);
100
101 switch (op) {
102 case Py_EQ:
103 if (r == 0)
104 Py_RETURN_TRUE;
105
106 Py_RETURN_FALSE;
107
108 case Py_LT:
109 if (r < 0)
110 Py_RETURN_TRUE;
111
112 Py_RETURN_FALSE;
113
114 default:
115 break;
116 }
117
118 Py_RETURN_NOTIMPLEMENTED;
119 }
120
121 static struct PyGetSetDef AS_getsetters[] = {
122 {
123 "name",
124 (getter)AS_get_name,
125 (setter)AS_set_name,
126 NULL,
127 NULL,
128 },
129 {
130 "number",
131 (getter)AS_get_number,
132 NULL,
133 NULL,
134 NULL,
135 },
136 { NULL },
137 };
138
139 PyTypeObject ASType = {
140 PyVarObject_HEAD_INIT(NULL, 0)
141 tp_name: "location.AS",
142 tp_basicsize: sizeof(ASObject),
143 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
144 tp_new: AS_new,
145 tp_dealloc: (destructor)AS_dealloc,
146 tp_init: (initproc)AS_init,
147 tp_doc: "AS object",
148 tp_getset: AS_getsetters,
149 tp_repr: (reprfunc)AS_repr,
150 tp_richcompare: (richcmpfunc)AS_richcompare,
151 };