]>
Commit | Line | Data |
---|---|---|
d688e569 MT |
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" | |
f8710bd6 | 23 | #include "as.h" |
a92adad3 | 24 | #include "network.h" |
d688e569 MT |
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 | ||
360ae5ad MT |
55 | static int Writer_set_vendor(WriterObject* self, PyObject* value) { |
56 | const char* vendor = PyUnicode_AsUTF8(value); | |
d688e569 MT |
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 | ||
9224f7ba MT |
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 | ||
4bf49d00 MT |
85 | static PyObject* Writer_get_license(WriterObject* self) { |
86 | const char* license = loc_writer_get_license(self->writer); | |
87 | ||
88 | return PyUnicode_FromString(license); | |
89 | } | |
90 | ||
91 | static int Writer_set_license(WriterObject* self, PyObject* value) { | |
92 | const char* license = PyUnicode_AsUTF8(value); | |
93 | ||
94 | int r = loc_writer_set_license(self->writer, license); | |
95 | if (r) { | |
96 | PyErr_Format(PyExc_ValueError, "Could not set license: %s", license); | |
97 | return r; | |
98 | } | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
f8710bd6 MT |
103 | static PyObject* Writer_add_as(WriterObject* self, PyObject* args) { |
104 | struct loc_as* as; | |
105 | uint32_t number = 0; | |
106 | ||
107 | if (!PyArg_ParseTuple(args, "i", &number)) | |
108 | return NULL; | |
109 | ||
110 | // Create AS object | |
111 | int r = loc_writer_add_as(self->writer, &as, number); | |
112 | if (r) | |
113 | return NULL; | |
114 | ||
115 | PyObject* obj = new_as(&ASType, as); | |
116 | loc_as_unref(as); | |
117 | ||
118 | return obj; | |
119 | } | |
120 | ||
a92adad3 MT |
121 | static PyObject* Writer_add_network(WriterObject* self, PyObject* args) { |
122 | struct loc_network* network; | |
123 | const char* string = NULL; | |
124 | ||
125 | if (!PyArg_ParseTuple(args, "s", &string)) | |
126 | return NULL; | |
127 | ||
128 | // Create network object | |
129 | int r = loc_writer_add_network(self->writer, &network, string); | |
dafdb6c8 | 130 | if (r) { |
c04005bb MT |
131 | switch (r) { |
132 | case -EINVAL: | |
133 | PyErr_SetString(PyExc_ValueError, "Invalid network"); | |
134 | break; | |
135 | ||
136 | case -EBUSY: | |
137 | PyErr_SetString(PyExc_IndexError, "A network already exists here"); | |
138 | break; | |
139 | } | |
dafdb6c8 | 140 | |
a92adad3 | 141 | return NULL; |
dafdb6c8 | 142 | } |
a92adad3 MT |
143 | |
144 | PyObject* obj = new_network(&NetworkType, network); | |
145 | loc_network_unref(network); | |
146 | ||
147 | return obj; | |
148 | } | |
149 | ||
7d278671 MT |
150 | static PyObject* Writer_write(WriterObject* self, PyObject* args) { |
151 | const char* path = NULL; | |
152 | ||
153 | if (!PyArg_ParseTuple(args, "s", &path)) | |
154 | return NULL; | |
155 | ||
156 | FILE* f = fopen(path, "w"); | |
157 | if (!f) { | |
158 | PyErr_Format(PyExc_IOError, strerror(errno)); | |
159 | return NULL; | |
160 | } | |
161 | ||
162 | int r = loc_writer_write(self->writer, f); | |
163 | fclose(f); | |
164 | ||
165 | // Raise any errors | |
166 | if (r) { | |
167 | PyErr_Format(PyExc_IOError, strerror(errno)); | |
168 | return NULL; | |
169 | } | |
170 | ||
171 | Py_RETURN_NONE; | |
172 | } | |
173 | ||
174 | static struct PyMethodDef Writer_methods[] = { | |
f8710bd6 MT |
175 | { |
176 | "add_as", | |
177 | (PyCFunction)Writer_add_as, | |
178 | METH_VARARGS, | |
179 | NULL, | |
180 | }, | |
a92adad3 MT |
181 | { |
182 | "add_network", | |
183 | (PyCFunction)Writer_add_network, | |
184 | METH_VARARGS, | |
185 | NULL, | |
186 | }, | |
7d278671 MT |
187 | { |
188 | "write", | |
189 | (PyCFunction)Writer_write, | |
190 | METH_VARARGS, | |
191 | NULL, | |
192 | }, | |
193 | { NULL }, | |
194 | }; | |
195 | ||
d688e569 | 196 | static struct PyGetSetDef Writer_getsetters[] = { |
9224f7ba MT |
197 | { |
198 | "description", | |
199 | (getter)Writer_get_description, | |
200 | (setter)Writer_set_description, | |
201 | NULL, | |
202 | NULL, | |
203 | }, | |
4bf49d00 MT |
204 | { |
205 | "license", | |
206 | (getter)Writer_get_license, | |
207 | (setter)Writer_set_license, | |
208 | NULL, | |
209 | NULL, | |
210 | }, | |
d688e569 MT |
211 | { |
212 | "vendor", | |
213 | (getter)Writer_get_vendor, | |
214 | (setter)Writer_set_vendor, | |
215 | NULL, | |
216 | NULL, | |
217 | }, | |
218 | { NULL }, | |
219 | }; | |
220 | ||
221 | PyTypeObject WriterType = { | |
222 | PyVarObject_HEAD_INIT(NULL, 0) | |
d42e1dcd MT |
223 | .tp_name = "location.Writer", |
224 | .tp_basicsize = sizeof(WriterObject), | |
225 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, | |
226 | .tp_new = Writer_new, | |
227 | .tp_dealloc = (destructor)Writer_dealloc, | |
228 | .tp_init = (initproc)Writer_init, | |
229 | .tp_doc = "Writer object", | |
230 | .tp_methods = Writer_methods, | |
231 | .tp_getset = Writer_getsetters, | |
d688e569 | 232 | }; |