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"
27 static PyObject
* Writer_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
28 WriterObject
* self
= (WriterObject
*)type
->tp_alloc(type
, 0);
30 return (PyObject
*)self
;
33 static void Writer_dealloc(WriterObject
* self
) {
35 loc_writer_unref(self
->writer
);
37 Py_TYPE(self
)->tp_free((PyObject
* )self
);
40 static int Writer_init(WriterObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
41 // Create the writer object
42 int r
= loc_writer_new(loc_ctx
, &self
->writer
);
49 static PyObject
* Writer_get_vendor(WriterObject
* self
) {
50 const char* vendor
= loc_writer_get_vendor(self
->writer
);
52 return PyUnicode_FromString(vendor
);
55 static int Writer_set_vendor(WriterObject
* self
, PyObject
* value
) {
56 const char* vendor
= PyUnicode_AsUTF8(value
);
58 int r
= loc_writer_set_vendor(self
->writer
, vendor
);
60 PyErr_Format(PyExc_ValueError
, "Could not set vendor: %s", vendor
);
67 static PyObject
* Writer_get_description(WriterObject
* self
) {
68 const char* description
= loc_writer_get_description(self
->writer
);
70 return PyUnicode_FromString(description
);
73 static int Writer_set_description(WriterObject
* self
, PyObject
* value
) {
74 const char* description
= PyUnicode_AsUTF8(value
);
76 int r
= loc_writer_set_description(self
->writer
, description
);
78 PyErr_Format(PyExc_ValueError
, "Could not set description: %s", description
);
85 static PyObject
* Writer_get_license(WriterObject
* self
) {
86 const char* license
= loc_writer_get_license(self
->writer
);
88 return PyUnicode_FromString(license
);
91 static int Writer_set_license(WriterObject
* self
, PyObject
* value
) {
92 const char* license
= PyUnicode_AsUTF8(value
);
94 int r
= loc_writer_set_license(self
->writer
, license
);
96 PyErr_Format(PyExc_ValueError
, "Could not set license: %s", license
);
103 static PyObject
* Writer_add_as(WriterObject
* self
, PyObject
* args
) {
107 if (!PyArg_ParseTuple(args
, "i", &number
))
111 int r
= loc_writer_add_as(self
->writer
, &as
, number
);
115 PyObject
* obj
= new_as(&ASType
, as
);
121 static PyObject
* Writer_add_network(WriterObject
* self
, PyObject
* args
) {
122 struct loc_network
* network
;
123 const char* string
= NULL
;
125 if (!PyArg_ParseTuple(args
, "s", &string
))
128 // Create network object
129 int r
= loc_writer_add_network(self
->writer
, &network
, string
);
132 PyErr_SetString(PyExc_ValueError
, "Invalid network");
137 PyObject
* obj
= new_network(&NetworkType
, network
);
138 loc_network_unref(network
);
143 static PyObject
* Writer_write(WriterObject
* self
, PyObject
* args
) {
144 const char* path
= NULL
;
146 if (!PyArg_ParseTuple(args
, "s", &path
))
149 FILE* f
= fopen(path
, "w");
151 PyErr_Format(PyExc_IOError
, strerror(errno
));
155 int r
= loc_writer_write(self
->writer
, f
);
160 PyErr_Format(PyExc_IOError
, strerror(errno
));
167 static struct PyMethodDef Writer_methods
[] = {
170 (PyCFunction
)Writer_add_as
,
176 (PyCFunction
)Writer_add_network
,
182 (PyCFunction
)Writer_write
,
189 static struct PyGetSetDef Writer_getsetters
[] = {
192 (getter
)Writer_get_description
,
193 (setter
)Writer_set_description
,
199 (getter
)Writer_get_license
,
200 (setter
)Writer_set_license
,
206 (getter
)Writer_get_vendor
,
207 (setter
)Writer_set_vendor
,
214 PyTypeObject WriterType
= {
215 PyVarObject_HEAD_INIT(NULL
, 0)
216 tp_name
: "location.Writer",
217 tp_basicsize
: sizeof(WriterObject
),
218 tp_flags
: Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
220 tp_dealloc
: (destructor
)Writer_dealloc
,
221 tp_init
: (initproc
)Writer_init
,
222 tp_doc
: "Writer object",
223 tp_methods
: Writer_methods
,
224 tp_getset
: Writer_getsetters
,