]> git.ipfire.org Git - location/libloc.git/blob - src/python/writer.c
9a37b0ee6247df0d8de6f61f9a15765f7c676ef4
[location/libloc.git] / src / python / writer.c
1 /*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
5
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.
10
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.
15 */
16
17 #include <Python.h>
18
19 #include <loc/libloc.h>
20 #include <loc/writer.h>
21
22 #include "locationmodule.h"
23 #include "as.h"
24 #include "network.h"
25 #include "writer.h"
26
27 static PyObject* Writer_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
28 WriterObject* self = (WriterObject*)type->tp_alloc(type, 0);
29
30 return (PyObject*)self;
31 }
32
33 static void Writer_dealloc(WriterObject* self) {
34 if (self->writer)
35 loc_writer_unref(self->writer);
36
37 Py_TYPE(self)->tp_free((PyObject* )self);
38 }
39
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);
43 if (r)
44 return -1;
45
46 return 0;
47 }
48
49 static PyObject* Writer_get_vendor(WriterObject* self) {
50 const char* vendor = loc_writer_get_vendor(self->writer);
51
52 return PyUnicode_FromString(vendor);
53 }
54
55 static int Writer_set_vendor(WriterObject* self, PyObject* value) {
56 const char* vendor = PyUnicode_AsUTF8(value);
57
58 int r = loc_writer_set_vendor(self->writer, vendor);
59 if (r) {
60 PyErr_Format(PyExc_ValueError, "Could not set vendor: %s", vendor);
61 return r;
62 }
63
64 return 0;
65 }
66
67 static PyObject* Writer_get_description(WriterObject* self) {
68 const char* description = loc_writer_get_description(self->writer);
69
70 return PyUnicode_FromString(description);
71 }
72
73 static int Writer_set_description(WriterObject* self, PyObject* value) {
74 const char* description = PyUnicode_AsUTF8(value);
75
76 int r = loc_writer_set_description(self->writer, description);
77 if (r) {
78 PyErr_Format(PyExc_ValueError, "Could not set description: %s", description);
79 return r;
80 }
81
82 return 0;
83 }
84
85 static PyObject* Writer_add_as(WriterObject* self, PyObject* args) {
86 struct loc_as* as;
87 uint32_t number = 0;
88
89 if (!PyArg_ParseTuple(args, "i", &number))
90 return NULL;
91
92 // Create AS object
93 int r = loc_writer_add_as(self->writer, &as, number);
94 if (r)
95 return NULL;
96
97 PyObject* obj = new_as(&ASType, as);
98 loc_as_unref(as);
99
100 return obj;
101 }
102
103 static PyObject* Writer_add_network(WriterObject* self, PyObject* args) {
104 struct loc_network* network;
105 const char* string = NULL;
106
107 if (!PyArg_ParseTuple(args, "s", &string))
108 return NULL;
109
110 // Create network object
111 int r = loc_writer_add_network(self->writer, &network, string);
112 if (r)
113 return NULL;
114
115 PyObject* obj = new_network(&NetworkType, network);
116 loc_network_unref(network);
117
118 return obj;
119 }
120
121 static PyObject* Writer_write(WriterObject* self, PyObject* args) {
122 const char* path = NULL;
123
124 if (!PyArg_ParseTuple(args, "s", &path))
125 return NULL;
126
127 FILE* f = fopen(path, "w");
128 if (!f) {
129 PyErr_Format(PyExc_IOError, strerror(errno));
130 return NULL;
131 }
132
133 int r = loc_writer_write(self->writer, f);
134 fclose(f);
135
136 // Raise any errors
137 if (r) {
138 PyErr_Format(PyExc_IOError, strerror(errno));
139 return NULL;
140 }
141
142 Py_RETURN_NONE;
143 }
144
145 static struct PyMethodDef Writer_methods[] = {
146 {
147 "add_as",
148 (PyCFunction)Writer_add_as,
149 METH_VARARGS,
150 NULL,
151 },
152 {
153 "add_network",
154 (PyCFunction)Writer_add_network,
155 METH_VARARGS,
156 NULL,
157 },
158 {
159 "write",
160 (PyCFunction)Writer_write,
161 METH_VARARGS,
162 NULL,
163 },
164 { NULL },
165 };
166
167 static struct PyGetSetDef Writer_getsetters[] = {
168 {
169 "description",
170 (getter)Writer_get_description,
171 (setter)Writer_set_description,
172 NULL,
173 NULL,
174 },
175 {
176 "vendor",
177 (getter)Writer_get_vendor,
178 (setter)Writer_set_vendor,
179 NULL,
180 NULL,
181 },
182 { NULL },
183 };
184
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,
190 tp_new: Writer_new,
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,
196 };