2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2019 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 <loc/libloc.h>
20 #include <loc/country.h>
22 #include "locationmodule.h"
25 PyObject
* new_country(PyTypeObject
* type
, struct loc_country
* country
) {
26 CountryObject
* self
= (CountryObject
*)type
->tp_alloc(type
, 0);
28 self
->country
= loc_country_ref(country
);
31 return (PyObject
*)self
;
34 static PyObject
* Country_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
35 CountryObject
* self
= (CountryObject
*)type
->tp_alloc(type
, 0);
37 return (PyObject
*)self
;
40 static void Country_dealloc(CountryObject
* self
) {
42 loc_country_unref(self
->country
);
44 Py_TYPE(self
)->tp_free((PyObject
* )self
);
47 static int Country_init(CountryObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
48 const char* country_code
= NULL
;
50 if (!PyArg_ParseTuple(args
, "s", &country_code
))
53 // Create the country object
54 int r
= loc_country_new(loc_ctx
, &self
->country
, country_code
);
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
);
66 return PyUnicode_FromFormat("<Country %s (%s)>", code
, name
);
68 return PyUnicode_FromFormat("<Country %s>", code
);
71 static PyObject
* Country_get_code(CountryObject
* self
) {
72 const char* code
= loc_country_get_code(self
->country
);
74 return PyUnicode_FromString(code
);
77 static PyObject
* Country_str(CountryObject
* self
) {
78 return Country_get_code(self
);
81 static PyObject
* Country_get_name(CountryObject
* self
) {
82 const char* name
= loc_country_get_name(self
->country
);
84 return PyUnicode_FromString(name
);
87 static int Country_set_name(CountryObject
* self
, PyObject
* value
) {
88 const char* name
= PyUnicode_AsUTF8(value
);
90 int r
= loc_country_set_name(self
->country
, name
);
92 PyErr_Format(PyExc_ValueError
, "Could not set name: %s", name
);
99 static PyObject
* Country_get_continent_code(CountryObject
* self
) {
100 const char* code
= loc_country_get_continent_code(self
->country
);
102 return PyUnicode_FromString(code
);
105 static int Country_set_continent_code(CountryObject
* self
, PyObject
* value
) {
106 const char* code
= PyUnicode_AsUTF8(value
);
108 int r
= loc_country_set_continent_code(self
->country
, code
);
110 PyErr_Format(PyExc_ValueError
, "Could not set continent code: %s", code
);
117 static PyObject
* Country_richcompare(CountryObject
* self
, CountryObject
* other
, int op
) {
118 int r
= loc_country_cmp(self
->country
, other
->country
);
137 Py_RETURN_NOTIMPLEMENTED
;
140 static struct PyGetSetDef Country_getsetters
[] = {
143 (getter
)Country_get_code
,
150 (getter
)Country_get_name
,
151 (setter
)Country_set_name
,
157 (getter
)Country_get_continent_code
,
158 (setter
)Country_set_continent_code
,
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
,