]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/writer.c
python: Fix setters
[people/ms/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 "writer.h"
25
26 static 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
32 static 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
39 static 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
48 static PyObject* Writer_get_vendor(WriterObject* self) {
49 const char* vendor = loc_writer_get_vendor(self->writer);
50
51 return PyUnicode_FromString(vendor);
52 }
53
54 static int Writer_set_vendor(WriterObject* self, PyObject* value) {
55 const char* vendor = PyUnicode_AsUTF8(value);
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
66 static PyObject* Writer_add_as(WriterObject* self, PyObject* args) {
67 struct loc_as* as;
68 uint32_t number = 0;
69
70 if (!PyArg_ParseTuple(args, "i", &number))
71 return NULL;
72
73 // Create AS object
74 int r = loc_writer_add_as(self->writer, &as, number);
75 if (r)
76 return NULL;
77
78 PyObject* obj = new_as(&ASType, as);
79 loc_as_unref(as);
80
81 return obj;
82 }
83
84 static PyObject* Writer_write(WriterObject* self, PyObject* args) {
85 const char* path = NULL;
86
87 if (!PyArg_ParseTuple(args, "s", &path))
88 return NULL;
89
90 FILE* f = fopen(path, "w");
91 if (!f) {
92 PyErr_Format(PyExc_IOError, strerror(errno));
93 return NULL;
94 }
95
96 int r = loc_writer_write(self->writer, f);
97 fclose(f);
98
99 // Raise any errors
100 if (r) {
101 PyErr_Format(PyExc_IOError, strerror(errno));
102 return NULL;
103 }
104
105 Py_RETURN_NONE;
106 }
107
108 static struct PyMethodDef Writer_methods[] = {
109 {
110 "add_as",
111 (PyCFunction)Writer_add_as,
112 METH_VARARGS,
113 NULL,
114 },
115 {
116 "write",
117 (PyCFunction)Writer_write,
118 METH_VARARGS,
119 NULL,
120 },
121 { NULL },
122 };
123
124 static struct PyGetSetDef Writer_getsetters[] = {
125 {
126 "vendor",
127 (getter)Writer_get_vendor,
128 (setter)Writer_set_vendor,
129 NULL,
130 NULL,
131 },
132 { NULL },
133 };
134
135 PyTypeObject WriterType = {
136 PyVarObject_HEAD_INIT(NULL, 0)
137 tp_name: "location.Writer",
138 tp_basicsize: sizeof(WriterObject),
139 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
140 tp_new: Writer_new,
141 tp_dealloc: (destructor)Writer_dealloc,
142 tp_init: (initproc)Writer_init,
143 tp_doc: "Writer object",
144 tp_methods: Writer_methods,
145 tp_getset: Writer_getsetters,
146 };