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 <loc/libloc.h>
20 #include <loc/writer.h>
22 #include "locationmodule.h"
28 static PyObject
* Writer_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
29 WriterObject
* self
= (WriterObject
*)type
->tp_alloc(type
, 0);
31 return (PyObject
*)self
;
34 static void Writer_dealloc(WriterObject
* self
) {
36 loc_writer_unref(self
->writer
);
38 Py_TYPE(self
)->tp_free((PyObject
* )self
);
41 static int Writer_init(WriterObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
42 PyObject
* private_key
= NULL
;
47 if (!PyArg_ParseTuple(args
, "|Oi", &private_key
, &version
))
51 if (private_key
&& private_key
!= Py_None
) {
52 int fd
= PyObject_AsFileDescriptor(private_key
);
56 // Re-open file descriptor
59 PyErr_SetFromErrno(PyExc_IOError
);
64 // Create the writer object
65 int r
= loc_writer_new(loc_ctx
, &self
->writer
,
66 (enum loc_database_version
)version
, f
);
71 static PyObject
* Writer_get_vendor(WriterObject
* self
) {
72 const char* vendor
= loc_writer_get_vendor(self
->writer
);
74 return PyUnicode_FromString(vendor
);
77 static int Writer_set_vendor(WriterObject
* self
, PyObject
* value
) {
78 const char* vendor
= PyUnicode_AsUTF8(value
);
80 int r
= loc_writer_set_vendor(self
->writer
, vendor
);
82 PyErr_Format(PyExc_ValueError
, "Could not set vendor: %s", vendor
);
89 static PyObject
* Writer_get_description(WriterObject
* self
) {
90 const char* description
= loc_writer_get_description(self
->writer
);
92 return PyUnicode_FromString(description
);
95 static int Writer_set_description(WriterObject
* self
, PyObject
* value
) {
96 const char* description
= PyUnicode_AsUTF8(value
);
98 int r
= loc_writer_set_description(self
->writer
, description
);
100 PyErr_Format(PyExc_ValueError
, "Could not set description: %s", description
);
107 static PyObject
* Writer_get_license(WriterObject
* self
) {
108 const char* license
= loc_writer_get_license(self
->writer
);
110 return PyUnicode_FromString(license
);
113 static int Writer_set_license(WriterObject
* self
, PyObject
* value
) {
114 const char* license
= PyUnicode_AsUTF8(value
);
116 int r
= loc_writer_set_license(self
->writer
, license
);
118 PyErr_Format(PyExc_ValueError
, "Could not set license: %s", license
);
125 static PyObject
* Writer_add_as(WriterObject
* self
, PyObject
* args
) {
129 if (!PyArg_ParseTuple(args
, "i", &number
))
133 int r
= loc_writer_add_as(self
->writer
, &as
, number
);
137 PyObject
* obj
= new_as(&ASType
, as
);
143 static PyObject
* Writer_add_country(WriterObject
* self
, PyObject
* args
) {
144 struct loc_country
* country
;
145 const char* country_code
;
147 if (!PyArg_ParseTuple(args
, "s", &country_code
))
150 // Create country object
151 int r
= loc_writer_add_country(self
->writer
, &country
, country_code
);
155 PyErr_SetString(PyExc_ValueError
, "Invalid network");
163 PyObject
* obj
= new_country(&CountryType
, country
);
164 loc_country_unref(country
);
169 static PyObject
* Writer_add_network(WriterObject
* self
, PyObject
* args
) {
170 struct loc_network
* network
;
171 const char* string
= NULL
;
173 if (!PyArg_ParseTuple(args
, "s", &string
))
176 // Create network object
177 int r
= loc_writer_add_network(self
->writer
, &network
, string
);
181 PyErr_SetString(PyExc_ValueError
, "Invalid network");
185 PyErr_SetString(PyExc_IndexError
, "A network already exists here");
192 PyObject
* obj
= new_network(&NetworkType
, network
);
193 loc_network_unref(network
);
198 static PyObject
* Writer_write(WriterObject
* self
, PyObject
* args
) {
199 const char* path
= NULL
;
201 if (!PyArg_ParseTuple(args
, "s", &path
))
204 FILE* f
= fopen(path
, "w+");
206 PyErr_Format(PyExc_IOError
, strerror(errno
));
210 int r
= loc_writer_write(self
->writer
, f
);
215 PyErr_Format(PyExc_IOError
, strerror(errno
));
222 static struct PyMethodDef Writer_methods
[] = {
225 (PyCFunction
)Writer_add_as
,
231 (PyCFunction
)Writer_add_country
,
237 (PyCFunction
)Writer_add_network
,
243 (PyCFunction
)Writer_write
,
250 static struct PyGetSetDef Writer_getsetters
[] = {
253 (getter
)Writer_get_description
,
254 (setter
)Writer_set_description
,
260 (getter
)Writer_get_license
,
261 (setter
)Writer_set_license
,
267 (getter
)Writer_get_vendor
,
268 (setter
)Writer_set_vendor
,
275 PyTypeObject WriterType
= {
276 PyVarObject_HEAD_INIT(NULL
, 0)
277 .tp_name
= "location.Writer",
278 .tp_basicsize
= sizeof(WriterObject
),
279 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
280 .tp_new
= Writer_new
,
281 .tp_dealloc
= (destructor
)Writer_dealloc
,
282 .tp_init
= (initproc
)Writer_init
,
283 .tp_doc
= "Writer object",
284 .tp_methods
= Writer_methods
,
285 .tp_getset
= Writer_getsetters
,