]>
Commit | Line | Data |
---|---|---|
af208e26 MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2019 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/country.h> | |
21 | ||
22 | #include "locationmodule.h" | |
23 | #include "country.h" | |
24 | ||
25 | PyObject* new_country(PyTypeObject* type, struct loc_country* country) { | |
26 | CountryObject* self = (CountryObject*)type->tp_alloc(type, 0); | |
27 | if (self) { | |
28 | self->country = loc_country_ref(country); | |
29 | } | |
30 | ||
31 | return (PyObject*)self; | |
32 | } | |
33 | ||
34 | static PyObject* Country_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { | |
35 | CountryObject* self = (CountryObject*)type->tp_alloc(type, 0); | |
36 | ||
37 | return (PyObject*)self; | |
38 | } | |
39 | ||
40 | static void Country_dealloc(CountryObject* self) { | |
41 | if (self->country) | |
42 | loc_country_unref(self->country); | |
43 | ||
44 | Py_TYPE(self)->tp_free((PyObject* )self); | |
45 | } | |
46 | ||
47 | static int Country_init(CountryObject* self, PyObject* args, PyObject* kwargs) { | |
48 | const char* country_code = NULL; | |
49 | ||
50 | if (!PyArg_ParseTuple(args, "s", &country_code)) | |
51 | return -1; | |
52 | ||
53 | // Create the country object | |
54 | int r = loc_country_new(loc_ctx, &self->country, country_code); | |
55 | if (r) | |
56 | return -1; | |
57 | ||
58 | return 0; | |
59 | } | |
60 | ||
61 | static PyObject* Country_repr(CountryObject* self) { | |
62 | const char* code = loc_country_get_code(self->country); | |
63 | const char* name = loc_country_get_name(self->country); | |
64 | ||
65 | if (name) | |
66 | return PyUnicode_FromFormat("<Country %s (%s)>", code, name); | |
67 | ||
68 | return PyUnicode_FromFormat("<Country %s>", code); | |
69 | } | |
70 | ||
71 | static PyObject* Country_get_code(CountryObject* self) { | |
72 | const char* code = loc_country_get_code(self->country); | |
73 | ||
74 | return PyUnicode_FromString(code); | |
75 | } | |
76 | ||
77 | static PyObject* Country_str(CountryObject* self) { | |
78 | return Country_get_code(self); | |
79 | } | |
80 | ||
81 | static PyObject* Country_get_name(CountryObject* self) { | |
82 | const char* name = loc_country_get_name(self->country); | |
83 | ||
84 | return PyUnicode_FromString(name); | |
85 | } | |
86 | ||
87 | static int Country_set_name(CountryObject* self, PyObject* value) { | |
88 | const char* name = PyUnicode_AsUTF8(value); | |
89 | ||
90 | int r = loc_country_set_name(self->country, name); | |
91 | if (r) { | |
92 | PyErr_Format(PyExc_ValueError, "Could not set name: %s", name); | |
93 | return r; | |
94 | } | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | static PyObject* Country_get_continent_code(CountryObject* self) { | |
100 | const char* code = loc_country_get_continent_code(self->country); | |
101 | ||
102 | return PyUnicode_FromString(code); | |
103 | } | |
104 | ||
105 | static int Country_set_continent_code(CountryObject* self, PyObject* value) { | |
106 | const char* code = PyUnicode_AsUTF8(value); | |
107 | ||
108 | int r = loc_country_set_continent_code(self->country, code); | |
109 | if (r) { | |
110 | PyErr_Format(PyExc_ValueError, "Could not set continent code: %s", code); | |
111 | return r; | |
112 | } | |
113 | ||
114 | return 0; | |
115 | } | |
116 | ||
117 | static PyObject* Country_richcompare(CountryObject* self, CountryObject* other, int op) { | |
118 | int r = loc_country_cmp(self->country, other->country); | |
119 | ||
120 | switch (op) { | |
121 | case Py_EQ: | |
122 | if (r == 0) | |
123 | Py_RETURN_TRUE; | |
124 | ||
125 | Py_RETURN_FALSE; | |
126 | ||
127 | case Py_LT: | |
128 | if (r < 0) | |
129 | Py_RETURN_TRUE; | |
130 | ||
131 | Py_RETURN_FALSE; | |
132 | ||
133 | default: | |
134 | break; | |
135 | } | |
136 | ||
137 | Py_RETURN_NOTIMPLEMENTED; | |
138 | } | |
139 | ||
140 | static struct PyGetSetDef Country_getsetters[] = { | |
141 | { | |
142 | "code", | |
143 | (getter)Country_get_code, | |
144 | NULL, | |
145 | NULL, | |
146 | NULL, | |
147 | }, | |
148 | { | |
149 | "name", | |
150 | (getter)Country_get_name, | |
151 | (setter)Country_set_name, | |
152 | NULL, | |
153 | NULL, | |
154 | }, | |
155 | { | |
156 | "continent_code", | |
157 | (getter)Country_get_continent_code, | |
158 | (setter)Country_set_continent_code, | |
159 | NULL, | |
160 | NULL, | |
161 | }, | |
162 | { NULL }, | |
163 | }; | |
164 | ||
165 | PyTypeObject CountryType = { | |
166 | PyVarObject_HEAD_INIT(NULL, 0) | |
167 | .tp_name = "location.Country", | |
168 | .tp_basicsize = sizeof(CountryObject), | |
169 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, | |
170 | .tp_new = Country_new, | |
171 | .tp_dealloc = (destructor)Country_dealloc, | |
172 | .tp_init = (initproc)Country_init, | |
173 | .tp_doc = "Country object", | |
174 | .tp_getset = Country_getsetters, | |
175 | .tp_repr = (reprfunc)Country_repr, | |
176 | .tp_str = (reprfunc)Country_str, | |
177 | .tp_richcompare = (richcmpfunc)Country_richcompare, | |
178 | }; |