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_add_as(WriterObject
* self
, PyObject
* args
) {
89 if (!PyArg_ParseTuple(args
, "i", &number
))
93 int r
= loc_writer_add_as(self
->writer
, &as
, number
);
97 PyObject
* obj
= new_as(&ASType
, as
);
103 static PyObject
* Writer_add_network(WriterObject
* self
, PyObject
* args
) {
104 struct loc_network
* network
;
105 const char* string
= NULL
;
107 if (!PyArg_ParseTuple(args
, "s", &string
))
110 // Create network object
111 int r
= loc_writer_add_network(self
->writer
, &network
, string
);
115 PyObject
* obj
= new_network(&NetworkType
, network
);
116 loc_network_unref(network
);
121 static PyObject
* Writer_write(WriterObject
* self
, PyObject
* args
) {
122 const char* path
= NULL
;
124 if (!PyArg_ParseTuple(args
, "s", &path
))
127 FILE* f
= fopen(path
, "w");
129 PyErr_Format(PyExc_IOError
, strerror(errno
));
133 int r
= loc_writer_write(self
->writer
, f
);
138 PyErr_Format(PyExc_IOError
, strerror(errno
));
145 static struct PyMethodDef Writer_methods
[] = {
148 (PyCFunction
)Writer_add_as
,
154 (PyCFunction
)Writer_add_network
,
160 (PyCFunction
)Writer_write
,
167 static struct PyGetSetDef Writer_getsetters
[] = {
170 (getter
)Writer_get_description
,
171 (setter
)Writer_set_description
,
177 (getter
)Writer_get_vendor
,
178 (setter
)Writer_set_vendor
,
185 PyTypeObject WriterType
= {
186 PyVarObject_HEAD_INIT(NULL
, 0)
187 tp_name
: "location.Writer",
188 tp_basicsize
: sizeof(WriterObject
),
189 tp_flags
: Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
191 tp_dealloc
: (destructor
)Writer_dealloc
,
192 tp_init
: (initproc
)Writer_init
,
193 tp_doc
: "Writer object",
194 tp_methods
: Writer_methods
,
195 tp_getset
: Writer_getsetters
,