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
;
46 if (!PyArg_ParseTuple(args
, "|O", &private_key
))
51 int fd
= PyObject_AsFileDescriptor(private_key
);
55 // Re-open file descriptor
58 PyErr_SetFromErrno(PyExc_IOError
);
63 // Create the writer object
64 int r
= loc_writer_new(loc_ctx
, &self
->writer
, f
);
69 static PyObject
* Writer_get_vendor(WriterObject
* self
) {
70 const char* vendor
= loc_writer_get_vendor(self
->writer
);
72 return PyUnicode_FromString(vendor
);
75 static int Writer_set_vendor(WriterObject
* self
, PyObject
* value
) {
76 const char* vendor
= PyUnicode_AsUTF8(value
);
78 int r
= loc_writer_set_vendor(self
->writer
, vendor
);
80 PyErr_Format(PyExc_ValueError
, "Could not set vendor: %s", vendor
);
87 static PyObject
* Writer_get_description(WriterObject
* self
) {
88 const char* description
= loc_writer_get_description(self
->writer
);
90 return PyUnicode_FromString(description
);
93 static int Writer_set_description(WriterObject
* self
, PyObject
* value
) {
94 const char* description
= PyUnicode_AsUTF8(value
);
96 int r
= loc_writer_set_description(self
->writer
, description
);
98 PyErr_Format(PyExc_ValueError
, "Could not set description: %s", description
);
105 static PyObject
* Writer_get_license(WriterObject
* self
) {
106 const char* license
= loc_writer_get_license(self
->writer
);
108 return PyUnicode_FromString(license
);
111 static int Writer_set_license(WriterObject
* self
, PyObject
* value
) {
112 const char* license
= PyUnicode_AsUTF8(value
);
114 int r
= loc_writer_set_license(self
->writer
, license
);
116 PyErr_Format(PyExc_ValueError
, "Could not set license: %s", license
);
123 static PyObject
* Writer_add_as(WriterObject
* self
, PyObject
* args
) {
127 if (!PyArg_ParseTuple(args
, "i", &number
))
131 int r
= loc_writer_add_as(self
->writer
, &as
, number
);
135 PyObject
* obj
= new_as(&ASType
, as
);
141 static PyObject
* Writer_add_country(WriterObject
* self
, PyObject
* args
) {
142 struct loc_country
* country
;
143 const char* country_code
;
145 if (!PyArg_ParseTuple(args
, "s", &country_code
))
148 // Create country object
149 int r
= loc_writer_add_country(self
->writer
, &country
, country_code
);
153 PyErr_SetString(PyExc_ValueError
, "Invalid network");
161 PyObject
* obj
= new_country(&CountryType
, country
);
162 loc_country_unref(country
);
167 static PyObject
* Writer_add_network(WriterObject
* self
, PyObject
* args
) {
168 struct loc_network
* network
;
169 const char* string
= NULL
;
171 if (!PyArg_ParseTuple(args
, "s", &string
))
174 // Create network object
175 int r
= loc_writer_add_network(self
->writer
, &network
, string
);
179 PyErr_SetString(PyExc_ValueError
, "Invalid network");
183 PyErr_SetString(PyExc_IndexError
, "A network already exists here");
190 PyObject
* obj
= new_network(&NetworkType
, network
);
191 loc_network_unref(network
);
196 static PyObject
* Writer_write(WriterObject
* self
, PyObject
* args
) {
197 const char* path
= NULL
;
199 if (!PyArg_ParseTuple(args
, "s", &path
))
202 FILE* f
= fopen(path
, "w+");
204 PyErr_Format(PyExc_IOError
, strerror(errno
));
208 int r
= loc_writer_write(self
->writer
, f
);
213 PyErr_Format(PyExc_IOError
, strerror(errno
));
220 static struct PyMethodDef Writer_methods
[] = {
223 (PyCFunction
)Writer_add_as
,
229 (PyCFunction
)Writer_add_country
,
235 (PyCFunction
)Writer_add_network
,
241 (PyCFunction
)Writer_write
,
248 static struct PyGetSetDef Writer_getsetters
[] = {
251 (getter
)Writer_get_description
,
252 (setter
)Writer_set_description
,
258 (getter
)Writer_get_license
,
259 (setter
)Writer_set_license
,
265 (getter
)Writer_get_vendor
,
266 (setter
)Writer_set_vendor
,
273 PyTypeObject WriterType
= {
274 PyVarObject_HEAD_INIT(NULL
, 0)
275 .tp_name
= "location.Writer",
276 .tp_basicsize
= sizeof(WriterObject
),
277 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
278 .tp_new
= Writer_new
,
279 .tp_dealloc
= (destructor
)Writer_dealloc
,
280 .tp_init
= (initproc
)Writer_init
,
281 .tp_doc
= "Writer object",
282 .tp_methods
= Writer_methods
,
283 .tp_getset
= Writer_getsetters
,