]> git.ipfire.org Git - people/ms/libloc.git/blame - src/python/writer.c
database: Unmap all mapped sections when freeing database
[people/ms/libloc.git] / src / python / writer.c
CommitLineData
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"
d688e569
MT
24#include "writer.h"
25
26static PyObject* Writer_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
27 WriterObject* self = (WriterObject*)type->tp_alloc(type, 0);
28
29 return (PyObject*)self;
30}
31
32static void Writer_dealloc(WriterObject* self) {
33 if (self->writer)
34 loc_writer_unref(self->writer);
35
36 Py_TYPE(self)->tp_free((PyObject* )self);
37}
38
39static int Writer_init(WriterObject* self, PyObject* args, PyObject* kwargs) {
40 // Create the writer object
41 int r = loc_writer_new(loc_ctx, &self->writer);
42 if (r)
43 return -1;
44
45 return 0;
46}
47
48static PyObject* Writer_get_vendor(WriterObject* self) {
49 const char* vendor = loc_writer_get_vendor(self->writer);
50
51 return PyUnicode_FromString(vendor);
52}
53
360ae5ad
MT
54static int Writer_set_vendor(WriterObject* self, PyObject* value) {
55 const char* vendor = PyUnicode_AsUTF8(value);
d688e569
MT
56
57 int r = loc_writer_set_vendor(self->writer, vendor);
58 if (r) {
59 PyErr_Format(PyExc_ValueError, "Could not set vendor: %s", vendor);
60 return r;
61 }
62
63 return 0;
64}
65
9224f7ba
MT
66static PyObject* Writer_get_description(WriterObject* self) {
67 const char* description = loc_writer_get_description(self->writer);
68
69 return PyUnicode_FromString(description);
70}
71
72static int Writer_set_description(WriterObject* self, PyObject* value) {
73 const char* description = PyUnicode_AsUTF8(value);
74
75 int r = loc_writer_set_description(self->writer, description);
76 if (r) {
77 PyErr_Format(PyExc_ValueError, "Could not set description: %s", description);
78 return r;
79 }
80
81 return 0;
82}
83
f8710bd6
MT
84static PyObject* Writer_add_as(WriterObject* self, PyObject* args) {
85 struct loc_as* as;
86 uint32_t number = 0;
87
88 if (!PyArg_ParseTuple(args, "i", &number))
89 return NULL;
90
91 // Create AS object
92 int r = loc_writer_add_as(self->writer, &as, number);
93 if (r)
94 return NULL;
95
96 PyObject* obj = new_as(&ASType, as);
97 loc_as_unref(as);
98
99 return obj;
100}
101
7d278671
MT
102static PyObject* Writer_write(WriterObject* self, PyObject* args) {
103 const char* path = NULL;
104
105 if (!PyArg_ParseTuple(args, "s", &path))
106 return NULL;
107
108 FILE* f = fopen(path, "w");
109 if (!f) {
110 PyErr_Format(PyExc_IOError, strerror(errno));
111 return NULL;
112 }
113
114 int r = loc_writer_write(self->writer, f);
115 fclose(f);
116
117 // Raise any errors
118 if (r) {
119 PyErr_Format(PyExc_IOError, strerror(errno));
120 return NULL;
121 }
122
123 Py_RETURN_NONE;
124}
125
126static struct PyMethodDef Writer_methods[] = {
f8710bd6
MT
127 {
128 "add_as",
129 (PyCFunction)Writer_add_as,
130 METH_VARARGS,
131 NULL,
132 },
7d278671
MT
133 {
134 "write",
135 (PyCFunction)Writer_write,
136 METH_VARARGS,
137 NULL,
138 },
139 { NULL },
140};
141
d688e569 142static struct PyGetSetDef Writer_getsetters[] = {
9224f7ba
MT
143 {
144 "description",
145 (getter)Writer_get_description,
146 (setter)Writer_set_description,
147 NULL,
148 NULL,
149 },
d688e569
MT
150 {
151 "vendor",
152 (getter)Writer_get_vendor,
153 (setter)Writer_set_vendor,
154 NULL,
155 NULL,
156 },
157 { NULL },
158};
159
160PyTypeObject WriterType = {
161 PyVarObject_HEAD_INIT(NULL, 0)
162 tp_name: "location.Writer",
163 tp_basicsize: sizeof(WriterObject),
164 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
165 tp_new: Writer_new,
166 tp_dealloc: (destructor)Writer_dealloc,
167 tp_init: (initproc)Writer_init,
168 tp_doc: "Writer object",
7d278671 169 tp_methods: Writer_methods,
d688e569
MT
170 tp_getset: Writer_getsetters,
171};