]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/writer.c
python: Add Writer class
[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 "writer.h"
24
25 static PyObject* Writer_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
26 WriterObject* self = (WriterObject*)type->tp_alloc(type, 0);
27
28 return (PyObject*)self;
29 }
30
31 static void Writer_dealloc(WriterObject* self) {
32 if (self->writer)
33 loc_writer_unref(self->writer);
34
35 Py_TYPE(self)->tp_free((PyObject* )self);
36 }
37
38 static int Writer_init(WriterObject* self, PyObject* args, PyObject* kwargs) {
39 // Create the writer object
40 int r = loc_writer_new(loc_ctx, &self->writer);
41 if (r)
42 return -1;
43
44 return 0;
45 }
46
47 static PyObject* Writer_get_vendor(WriterObject* self) {
48 const char* vendor = loc_writer_get_vendor(self->writer);
49
50 return PyUnicode_FromString(vendor);
51 }
52
53 static int Writer_set_vendor(WriterObject* self, PyObject* args) {
54 const char* vendor = NULL;
55
56 if (!PyArg_ParseTuple(args, "s", &vendor))
57 return -1;
58
59 int r = loc_writer_set_vendor(self->writer, vendor);
60 if (r) {
61 PyErr_Format(PyExc_ValueError, "Could not set vendor: %s", vendor);
62 return r;
63 }
64
65 return 0;
66 }
67
68 static struct PyGetSetDef Writer_getsetters[] = {
69 {
70 "vendor",
71 (getter)Writer_get_vendor,
72 (setter)Writer_set_vendor,
73 NULL,
74 NULL,
75 },
76 { NULL },
77 };
78
79 PyTypeObject WriterType = {
80 PyVarObject_HEAD_INIT(NULL, 0)
81 tp_name: "location.Writer",
82 tp_basicsize: sizeof(WriterObject),
83 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
84 tp_new: Writer_new,
85 tp_dealloc: (destructor)Writer_dealloc,
86 tp_init: (initproc)Writer_init,
87 tp_doc: "Writer object",
88 tp_getset: Writer_getsetters,
89 };