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 // Create the writer object
43 int r
= loc_writer_new(loc_ctx
, &self
->writer
);
50 static PyObject
* Writer_get_vendor(WriterObject
* self
) {
51 const char* vendor
= loc_writer_get_vendor(self
->writer
);
53 return PyUnicode_FromString(vendor
);
56 static int Writer_set_vendor(WriterObject
* self
, PyObject
* value
) {
57 const char* vendor
= PyUnicode_AsUTF8(value
);
59 int r
= loc_writer_set_vendor(self
->writer
, vendor
);
61 PyErr_Format(PyExc_ValueError
, "Could not set vendor: %s", vendor
);
68 static PyObject
* Writer_get_description(WriterObject
* self
) {
69 const char* description
= loc_writer_get_description(self
->writer
);
71 return PyUnicode_FromString(description
);
74 static int Writer_set_description(WriterObject
* self
, PyObject
* value
) {
75 const char* description
= PyUnicode_AsUTF8(value
);
77 int r
= loc_writer_set_description(self
->writer
, description
);
79 PyErr_Format(PyExc_ValueError
, "Could not set description: %s", description
);
86 static PyObject
* Writer_get_license(WriterObject
* self
) {
87 const char* license
= loc_writer_get_license(self
->writer
);
89 return PyUnicode_FromString(license
);
92 static int Writer_set_license(WriterObject
* self
, PyObject
* value
) {
93 const char* license
= PyUnicode_AsUTF8(value
);
95 int r
= loc_writer_set_license(self
->writer
, license
);
97 PyErr_Format(PyExc_ValueError
, "Could not set license: %s", license
);
104 static PyObject
* Writer_add_as(WriterObject
* self
, PyObject
* args
) {
108 if (!PyArg_ParseTuple(args
, "i", &number
))
112 int r
= loc_writer_add_as(self
->writer
, &as
, number
);
116 PyObject
* obj
= new_as(&ASType
, as
);
122 static PyObject
* Writer_add_country(WriterObject
* self
, PyObject
* args
) {
123 struct loc_country
* country
;
124 const char* country_code
;
126 if (!PyArg_ParseTuple(args
, "s", &country_code
))
129 // Create country object
130 int r
= loc_writer_add_country(self
->writer
, &country
, country_code
);
134 PyErr_SetString(PyExc_ValueError
, "Invalid network");
142 PyObject
* obj
= new_country(&CountryType
, country
);
143 loc_country_unref(country
);
148 static PyObject
* Writer_add_network(WriterObject
* self
, PyObject
* args
) {
149 struct loc_network
* network
;
150 const char* string
= NULL
;
152 if (!PyArg_ParseTuple(args
, "s", &string
))
155 // Create network object
156 int r
= loc_writer_add_network(self
->writer
, &network
, string
);
160 PyErr_SetString(PyExc_ValueError
, "Invalid network");
164 PyErr_SetString(PyExc_IndexError
, "A network already exists here");
171 PyObject
* obj
= new_network(&NetworkType
, network
);
172 loc_network_unref(network
);
177 static PyObject
* Writer_write(WriterObject
* self
, PyObject
* args
) {
178 const char* path
= NULL
;
180 if (!PyArg_ParseTuple(args
, "s", &path
))
183 FILE* f
= fopen(path
, "w");
185 PyErr_Format(PyExc_IOError
, strerror(errno
));
189 int r
= loc_writer_write(self
->writer
, f
);
194 PyErr_Format(PyExc_IOError
, strerror(errno
));
201 static struct PyMethodDef Writer_methods
[] = {
204 (PyCFunction
)Writer_add_as
,
210 (PyCFunction
)Writer_add_country
,
216 (PyCFunction
)Writer_add_network
,
222 (PyCFunction
)Writer_write
,
229 static struct PyGetSetDef Writer_getsetters
[] = {
232 (getter
)Writer_get_description
,
233 (setter
)Writer_set_description
,
239 (getter
)Writer_get_license
,
240 (setter
)Writer_set_license
,
246 (getter
)Writer_get_vendor
,
247 (setter
)Writer_set_vendor
,
254 PyTypeObject WriterType
= {
255 PyVarObject_HEAD_INIT(NULL
, 0)
256 .tp_name
= "location.Writer",
257 .tp_basicsize
= sizeof(WriterObject
),
258 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
259 .tp_new
= Writer_new
,
260 .tp_dealloc
= (destructor
)Writer_dealloc
,
261 .tp_init
= (initproc
)Writer_init
,
262 .tp_doc
= "Writer object",
263 .tp_methods
= Writer_methods
,
264 .tp_getset
= Writer_getsetters
,