2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
19 #include <loc/libloc.h>
20 #include <loc/writer.h>
22 #include "locationmodule.h"
26 static PyObject
* Writer_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
27 WriterObject
* self
= (WriterObject
*)type
->tp_alloc(type
, 0);
29 return (PyObject
*)self
;
32 static void Writer_dealloc(WriterObject
* self
) {
34 loc_writer_unref(self
->writer
);
36 Py_TYPE(self
)->tp_free((PyObject
* )self
);
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
);
48 static PyObject
* Writer_get_vendor(WriterObject
* self
) {
49 const char* vendor
= loc_writer_get_vendor(self
->writer
);
51 return PyUnicode_FromString(vendor
);
54 static int Writer_set_vendor(WriterObject
* self
, PyObject
* args
) {
55 const char* vendor
= NULL
;
57 if (!PyArg_ParseTuple(args
, "s", &vendor
))
60 int r
= loc_writer_set_vendor(self
->writer
, vendor
);
62 PyErr_Format(PyExc_ValueError
, "Could not set vendor: %s", vendor
);
69 static PyObject
* Writer_add_as(WriterObject
* self
, PyObject
* args
) {
73 if (!PyArg_ParseTuple(args
, "i", &number
))
77 int r
= loc_writer_add_as(self
->writer
, &as
, number
);
81 PyObject
* obj
= new_as(&ASType
, as
);
87 static PyObject
* Writer_write(WriterObject
* self
, PyObject
* args
) {
88 const char* path
= NULL
;
90 if (!PyArg_ParseTuple(args
, "s", &path
))
93 FILE* f
= fopen(path
, "w");
95 PyErr_Format(PyExc_IOError
, strerror(errno
));
99 int r
= loc_writer_write(self
->writer
, f
);
104 PyErr_Format(PyExc_IOError
, strerror(errno
));
111 static struct PyMethodDef Writer_methods
[] = {
114 (PyCFunction
)Writer_add_as
,
120 (PyCFunction
)Writer_write
,
127 static struct PyGetSetDef Writer_getsetters
[] = {
130 (getter
)Writer_get_vendor
,
131 (setter
)Writer_set_vendor
,
138 PyTypeObject 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
,
144 tp_dealloc
: (destructor
)Writer_dealloc
,
145 tp_init
: (initproc
)Writer_init
,
146 tp_doc
: "Writer object",
147 tp_methods
: Writer_methods
,
148 tp_getset
: Writer_getsetters
,