python: Implement adding an AS to the 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"
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
54static int Writer_set_vendor(WriterObject* self, PyObject* args) {
55 const char* vendor = NULL;
56
57 if (!PyArg_ParseTuple(args, "s", &vendor))
58 return -1;
59
60 int r = loc_writer_set_vendor(self->writer, vendor);
61 if (r) {
62 PyErr_Format(PyExc_ValueError, "Could not set vendor: %s", vendor);
63 return r;
64 }
65
66 return 0;
67}
68
f8710bd6
MT
69static PyObject* Writer_add_as(WriterObject* self, PyObject* args) {
70 struct loc_as* as;
71 uint32_t number = 0;
72
73 if (!PyArg_ParseTuple(args, "i", &number))
74 return NULL;
75
76 // Create AS object
77 int r = loc_writer_add_as(self->writer, &as, number);
78 if (r)
79 return NULL;
80
81 PyObject* obj = new_as(&ASType, as);
82 loc_as_unref(as);
83
84 return obj;
85}
86
7d278671
MT
87static PyObject* Writer_write(WriterObject* self, PyObject* args) {
88 const char* path = NULL;
89
90 if (!PyArg_ParseTuple(args, "s", &path))
91 return NULL;
92
93 FILE* f = fopen(path, "w");
94 if (!f) {
95 PyErr_Format(PyExc_IOError, strerror(errno));
96 return NULL;
97 }
98
99 int r = loc_writer_write(self->writer, f);
100 fclose(f);
101
102 // Raise any errors
103 if (r) {
104 PyErr_Format(PyExc_IOError, strerror(errno));
105 return NULL;
106 }
107
108 Py_RETURN_NONE;
109}
110
111static struct PyMethodDef Writer_methods[] = {
f8710bd6
MT
112 {
113 "add_as",
114 (PyCFunction)Writer_add_as,
115 METH_VARARGS,
116 NULL,
117 },
7d278671
MT
118 {
119 "write",
120 (PyCFunction)Writer_write,
121 METH_VARARGS,
122 NULL,
123 },
124 { NULL },
125};
126
d688e569
MT
127static struct PyGetSetDef Writer_getsetters[] = {
128 {
129 "vendor",
130 (getter)Writer_get_vendor,
131 (setter)Writer_set_vendor,
132 NULL,
133 NULL,
134 },
135 { NULL },
136};
137
138PyTypeObject WriterType = {
139 PyVarObject_HEAD_INIT(NULL, 0)
140 tp_name: "location.Writer",
141 tp_basicsize: sizeof(WriterObject),
142 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
143 tp_new: Writer_new,
144 tp_dealloc: (destructor)Writer_dealloc,
145 tp_init: (initproc)Writer_init,
146 tp_doc: "Writer object",
7d278671 147 tp_methods: Writer_methods,
d688e569
MT
148 tp_getset: Writer_getsetters,
149};