2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
19 #include <libloc/libloc.h>
20 #include <libloc/as.h>
22 #include "locationmodule.h"
25 PyObject
* new_as(PyTypeObject
* type
, struct loc_as
* as
) {
26 ASObject
* self
= (ASObject
*)type
->tp_alloc(type
, 0);
28 self
->as
= loc_as_ref(as
);
31 return (PyObject
*)self
;
34 static PyObject
* AS_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
35 ASObject
* self
= (ASObject
*)type
->tp_alloc(type
, 0);
37 return (PyObject
*)self
;
40 static void AS_dealloc(ASObject
* self
) {
42 loc_as_unref(self
->as
);
44 Py_TYPE(self
)->tp_free((PyObject
* )self
);
47 static int AS_init(ASObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
50 if (!PyArg_ParseTuple(args
, "i", &number
))
53 // Create the AS object
54 int r
= loc_as_new(loc_ctx
, &self
->as
, number
);
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
);
66 return PyUnicode_FromFormat("<AS %d (%s)>", number
, name
);
68 return PyUnicode_FromFormat("<AS %d>", number
);
71 static 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
);
76 return PyUnicode_FromFormat("AS%d - %s", number
, name
);
78 return PyUnicode_FromFormat("AS%d", number
);
81 static PyObject
* AS_get_number(ASObject
* self
) {
82 uint32_t number
= loc_as_get_number(self
->as
);
84 return PyLong_FromLong(number
);
87 static PyObject
* AS_get_name(ASObject
* self
) {
88 const char* name
= loc_as_get_name(self
->as
);
90 return PyUnicode_FromString(name
);
93 static int AS_set_name(ASObject
* self
, PyObject
* value
) {
94 const char* name
= PyUnicode_AsUTF8(value
);
96 int r
= loc_as_set_name(self
->as
, name
);
98 PyErr_Format(PyExc_ValueError
, "Could not set name: %s", name
);
105 static PyObject
* AS_richcompare(ASObject
* self
, PyObject
* other
, int op
) {
109 if (!PyObject_IsInstance(other
, (PyObject
*)&ASType
))
110 Py_RETURN_NOTIMPLEMENTED
;
112 ASObject
* o
= (ASObject
*)other
;
114 r
= loc_as_cmp(self
->as
, o
->as
);
133 Py_RETURN_NOTIMPLEMENTED
;
136 static Py_hash_t
AS_hash(ASObject
* self
) {
137 uint32_t number
= loc_as_get_number(self
->as
);
142 static struct PyGetSetDef AS_getsetters
[] = {
152 (getter
)AS_get_number
,
160 PyTypeObject ASType
= {
161 PyVarObject_HEAD_INIT(NULL
, 0)
162 .tp_name
= "location.AS",
163 .tp_basicsize
= sizeof(ASObject
),
164 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
166 .tp_dealloc
= (destructor
)AS_dealloc
,
167 .tp_init
= (initproc
)AS_init
,
168 .tp_doc
= "AS object",
169 .tp_getset
= AS_getsetters
,
170 .tp_repr
= (reprfunc
)AS_repr
,
171 .tp_str
= (reprfunc
)AS_str
,
172 .tp_richcompare
= (richcmpfunc
)AS_richcompare
,
173 .tp_hash
= (hashfunc
)AS_hash
,