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"
25 static PyObject
* Writer_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
26 WriterObject
* self
= (WriterObject
*)type
->tp_alloc(type
, 0);
28 return (PyObject
*)self
;
31 static void Writer_dealloc(WriterObject
* self
) {
33 loc_writer_unref(self
->writer
);
35 Py_TYPE(self
)->tp_free((PyObject
* )self
);
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
);
47 static PyObject
* Writer_get_vendor(WriterObject
* self
) {
48 const char* vendor
= loc_writer_get_vendor(self
->writer
);
50 return PyUnicode_FromString(vendor
);
53 static int Writer_set_vendor(WriterObject
* self
, PyObject
* args
) {
54 const char* vendor
= NULL
;
56 if (!PyArg_ParseTuple(args
, "s", &vendor
))
59 int r
= loc_writer_set_vendor(self
->writer
, vendor
);
61 PyErr_Format(PyExc_ValueError
, "Could not set vendor: %s", vendor
);
68 static PyObject
* Writer_write(WriterObject
* self
, PyObject
* args
) {
69 const char* path
= NULL
;
71 if (!PyArg_ParseTuple(args
, "s", &path
))
74 FILE* f
= fopen(path
, "w");
76 PyErr_Format(PyExc_IOError
, strerror(errno
));
80 int r
= loc_writer_write(self
->writer
, f
);
85 PyErr_Format(PyExc_IOError
, strerror(errno
));
92 static struct PyMethodDef Writer_methods
[] = {
95 (PyCFunction
)Writer_write
,
102 static struct PyGetSetDef Writer_getsetters
[] = {
105 (getter
)Writer_get_vendor
,
106 (setter
)Writer_set_vendor
,
113 PyTypeObject 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
,
119 tp_dealloc
: (destructor
)Writer_dealloc
,
120 tp_init
: (initproc
)Writer_init
,
121 tp_doc
: "Writer object",
122 tp_methods
: Writer_methods
,
123 tp_getset
: Writer_getsetters
,