]> git.ipfire.org Git - people/ms/libloc.git/blame - src/python/writer.c
python: Add write method to Writer
[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"
23#include "writer.h"
24
25static 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
31static 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
38static 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
47static PyObject* Writer_get_vendor(WriterObject* self) {
48 const char* vendor = loc_writer_get_vendor(self->writer);
49
50 return PyUnicode_FromString(vendor);
51}
52
53static 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
7d278671
MT
68static PyObject* Writer_write(WriterObject* self, PyObject* args) {
69 const char* path = NULL;
70
71 if (!PyArg_ParseTuple(args, "s", &path))
72 return NULL;
73
74 FILE* f = fopen(path, "w");
75 if (!f) {
76 PyErr_Format(PyExc_IOError, strerror(errno));
77 return NULL;
78 }
79
80 int r = loc_writer_write(self->writer, f);
81 fclose(f);
82
83 // Raise any errors
84 if (r) {
85 PyErr_Format(PyExc_IOError, strerror(errno));
86 return NULL;
87 }
88
89 Py_RETURN_NONE;
90}
91
92static struct PyMethodDef Writer_methods[] = {
93 {
94 "write",
95 (PyCFunction)Writer_write,
96 METH_VARARGS,
97 NULL,
98 },
99 { NULL },
100};
101
d688e569
MT
102static struct PyGetSetDef Writer_getsetters[] = {
103 {
104 "vendor",
105 (getter)Writer_get_vendor,
106 (setter)Writer_set_vendor,
107 NULL,
108 NULL,
109 },
110 { NULL },
111};
112
113PyTypeObject WriterType = {
114 PyVarObject_HEAD_INIT(NULL, 0)
115 tp_name: "location.Writer",
116 tp_basicsize: sizeof(WriterObject),
117 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
118 tp_new: Writer_new,
119 tp_dealloc: (destructor)Writer_dealloc,
120 tp_init: (initproc)Writer_init,
121 tp_doc: "Writer object",
7d278671 122 tp_methods: Writer_methods,
d688e569
MT
123 tp_getset: Writer_getsetters,
124};