]>
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); | |
130 | if (r) | |
131 | return NULL; | |
132 | ||
133 | PyObject* obj = new_network(&NetworkType, network); | |
134 | loc_network_unref(network); | |
135 | ||
136 | return obj; | |
137 | } | |
138 | ||
7d278671 MT |
139 | static PyObject* Writer_write(WriterObject* self, PyObject* args) { |
140 | const char* path = NULL; | |
141 | ||
142 | if (!PyArg_ParseTuple(args, "s", &path)) | |
143 | return NULL; | |
144 | ||
145 | FILE* f = fopen(path, "w"); | |
146 | if (!f) { | |
147 | PyErr_Format(PyExc_IOError, strerror(errno)); | |
148 | return NULL; | |
149 | } | |
150 | ||
151 | int r = loc_writer_write(self->writer, f); | |
152 | fclose(f); | |
153 | ||
154 | // Raise any errors | |
155 | if (r) { | |
156 | PyErr_Format(PyExc_IOError, strerror(errno)); | |
157 | return NULL; | |
158 | } | |
159 | ||
160 | Py_RETURN_NONE; | |
161 | } | |
162 | ||
163 | static struct PyMethodDef Writer_methods[] = { | |
f8710bd6 MT |
164 | { |
165 | "add_as", | |
166 | (PyCFunction)Writer_add_as, | |
167 | METH_VARARGS, | |
168 | NULL, | |
169 | }, | |
a92adad3 MT |
170 | { |
171 | "add_network", | |
172 | (PyCFunction)Writer_add_network, | |
173 | METH_VARARGS, | |
174 | NULL, | |
175 | }, | |
7d278671 MT |
176 | { |
177 | "write", | |
178 | (PyCFunction)Writer_write, | |
179 | METH_VARARGS, | |
180 | NULL, | |
181 | }, | |
182 | { NULL }, | |
183 | }; | |
184 | ||
d688e569 | 185 | static struct PyGetSetDef Writer_getsetters[] = { |
9224f7ba MT |
186 | { |
187 | "description", | |
188 | (getter)Writer_get_description, | |
189 | (setter)Writer_set_description, | |
190 | NULL, | |
191 | NULL, | |
192 | }, | |
4bf49d00 MT |
193 | { |
194 | "license", | |
195 | (getter)Writer_get_license, | |
196 | (setter)Writer_set_license, | |
197 | NULL, | |
198 | NULL, | |
199 | }, | |
d688e569 MT |
200 | { |
201 | "vendor", | |
202 | (getter)Writer_get_vendor, | |
203 | (setter)Writer_set_vendor, | |
204 | NULL, | |
205 | NULL, | |
206 | }, | |
207 | { NULL }, | |
208 | }; | |
209 | ||
210 | PyTypeObject WriterType = { | |
211 | PyVarObject_HEAD_INIT(NULL, 0) | |
212 | tp_name: "location.Writer", | |
213 | tp_basicsize: sizeof(WriterObject), | |
214 | tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, | |
215 | tp_new: Writer_new, | |
216 | tp_dealloc: (destructor)Writer_dealloc, | |
217 | tp_init: (initproc)Writer_init, | |
218 | tp_doc: "Writer object", | |
7d278671 | 219 | tp_methods: Writer_methods, |
d688e569 MT |
220 | tp_getset: Writer_getsetters, |
221 | }; |