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/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_key1
= NULL
;
43 PyObject
* private_key2
= NULL
;
49 if (!PyArg_ParseTuple(args
, "|OO", &private_key1
, &private_key2
))
53 if (private_key1
== Py_None
) {
54 Py_DECREF(private_key1
);
58 if (private_key2
== Py_None
) {
59 Py_DECREF(private_key2
);
65 fd
= PyObject_AsFileDescriptor(private_key1
);
69 // Re-open file descriptor
72 PyErr_SetFromErrno(PyExc_IOError
);
78 fd
= PyObject_AsFileDescriptor(private_key2
);
82 // Re-open file descriptor
85 PyErr_SetFromErrno(PyExc_IOError
);
90 // Create the writer object
91 return loc_writer_new(loc_ctx
, &self
->writer
, f1
, f2
);
94 static PyObject
* Writer_get_vendor(WriterObject
* self
) {
95 const char* vendor
= loc_writer_get_vendor(self
->writer
);
97 return PyUnicode_FromString(vendor
);
100 static int Writer_set_vendor(WriterObject
* self
, PyObject
* value
) {
101 const char* vendor
= PyUnicode_AsUTF8(value
);
103 int r
= loc_writer_set_vendor(self
->writer
, vendor
);
105 PyErr_Format(PyExc_ValueError
, "Could not set vendor: %s", vendor
);
112 static PyObject
* Writer_get_description(WriterObject
* self
) {
113 const char* description
= loc_writer_get_description(self
->writer
);
115 return PyUnicode_FromString(description
);
118 static int Writer_set_description(WriterObject
* self
, PyObject
* value
) {
119 const char* description
= PyUnicode_AsUTF8(value
);
121 int r
= loc_writer_set_description(self
->writer
, description
);
123 PyErr_Format(PyExc_ValueError
, "Could not set description: %s", description
);
130 static PyObject
* Writer_get_license(WriterObject
* self
) {
131 const char* license
= loc_writer_get_license(self
->writer
);
133 return PyUnicode_FromString(license
);
136 static int Writer_set_license(WriterObject
* self
, PyObject
* value
) {
137 const char* license
= PyUnicode_AsUTF8(value
);
139 int r
= loc_writer_set_license(self
->writer
, license
);
141 PyErr_Format(PyExc_ValueError
, "Could not set license: %s", license
);
148 static PyObject
* Writer_add_as(WriterObject
* self
, PyObject
* args
) {
152 if (!PyArg_ParseTuple(args
, "i", &number
))
156 int r
= loc_writer_add_as(self
->writer
, &as
, number
);
160 PyObject
* obj
= new_as(&ASType
, as
);
166 static PyObject
* Writer_add_country(WriterObject
* self
, PyObject
* args
) {
167 struct loc_country
* country
;
168 const char* country_code
;
170 if (!PyArg_ParseTuple(args
, "s", &country_code
))
173 // Create country object
174 int r
= loc_writer_add_country(self
->writer
, &country
, country_code
);
178 PyErr_SetString(PyExc_ValueError
, "Invalid network");
186 PyObject
* obj
= new_country(&CountryType
, country
);
187 loc_country_unref(country
);
192 static PyObject
* Writer_add_network(WriterObject
* self
, PyObject
* args
) {
193 struct loc_network
* network
;
194 const char* string
= NULL
;
196 if (!PyArg_ParseTuple(args
, "s", &string
))
199 // Create network object
200 int r
= loc_writer_add_network(self
->writer
, &network
, string
);
204 PyErr_SetString(PyExc_ValueError
, "Invalid network");
208 PyErr_SetString(PyExc_IndexError
, "A network already exists here");
215 PyObject
* obj
= new_network(&NetworkType
, network
);
216 loc_network_unref(network
);
221 static PyObject
* Writer_write(WriterObject
* self
, PyObject
* args
) {
222 const char* path
= NULL
;
223 int version
= LOC_DATABASE_VERSION_UNSET
;
225 if (!PyArg_ParseTuple(args
, "s|i", &path
, &version
))
228 FILE* f
= fopen(path
, "w+");
230 PyErr_SetFromErrno(PyExc_OSError
);
234 int r
= loc_writer_write(self
->writer
, f
, (enum loc_database_version
)version
);
239 PyErr_SetFromErrno(PyExc_OSError
);
246 static struct PyMethodDef Writer_methods
[] = {
249 (PyCFunction
)Writer_add_as
,
255 (PyCFunction
)Writer_add_country
,
261 (PyCFunction
)Writer_add_network
,
267 (PyCFunction
)Writer_write
,
274 static struct PyGetSetDef Writer_getsetters
[] = {
277 (getter
)Writer_get_description
,
278 (setter
)Writer_set_description
,
284 (getter
)Writer_get_license
,
285 (setter
)Writer_set_license
,
291 (getter
)Writer_get_vendor
,
292 (setter
)Writer_set_vendor
,
299 PyTypeObject WriterType
= {
300 PyVarObject_HEAD_INIT(NULL
, 0)
301 .tp_name
= "location.Writer",
302 .tp_basicsize
= sizeof(WriterObject
),
303 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
304 .tp_new
= Writer_new
,
305 .tp_dealloc
= (destructor
)Writer_dealloc
,
306 .tp_init
= (initproc
)Writer_init
,
307 .tp_doc
= "Writer object",
308 .tp_methods
= Writer_methods
,
309 .tp_getset
= Writer_getsetters
,