]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/python/writer.c
python: Implement adding an AS to the writer
[people/ms/libloc.git] / src / python / writer.c
index 989ef9973289054b2b844c90c05fef4e5fbc33ea..f8a08387779797be2da62139ef6536b2b5a5624d 100644 (file)
@@ -20,6 +20,7 @@
 #include <loc/writer.h>
 
 #include "locationmodule.h"
+#include "as.h"
 #include "writer.h"
 
 static PyObject* Writer_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
@@ -65,6 +66,64 @@ static int Writer_set_vendor(WriterObject* self, PyObject* args) {
        return 0;
 }
 
+static PyObject* Writer_add_as(WriterObject* self, PyObject* args) {
+       struct loc_as* as;
+       uint32_t number = 0;
+
+       if (!PyArg_ParseTuple(args, "i", &number))
+               return NULL;
+
+       // Create AS object
+       int r = loc_writer_add_as(self->writer, &as, number);
+       if (r)
+               return NULL;
+
+       PyObject* obj = new_as(&ASType, as);
+       loc_as_unref(as);
+
+       return obj;
+}
+
+static PyObject* Writer_write(WriterObject* self, PyObject* args) {
+       const char* path = NULL;
+
+       if (!PyArg_ParseTuple(args, "s", &path))
+               return NULL;
+
+       FILE* f = fopen(path, "w");
+       if (!f) {
+               PyErr_Format(PyExc_IOError, strerror(errno));
+               return NULL;
+       }
+
+       int r = loc_writer_write(self->writer, f);
+       fclose(f);
+
+       // Raise any errors
+       if (r) {
+               PyErr_Format(PyExc_IOError, strerror(errno));
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static struct PyMethodDef Writer_methods[] = {
+       {
+               "add_as",
+               (PyCFunction)Writer_add_as,
+               METH_VARARGS,
+               NULL,
+       },
+       {
+               "write",
+               (PyCFunction)Writer_write,
+               METH_VARARGS,
+               NULL,
+       },
+       { NULL },
+};
+
 static struct PyGetSetDef Writer_getsetters[] = {
        {
                "vendor",
@@ -85,5 +144,6 @@ PyTypeObject WriterType = {
        tp_dealloc:             (destructor)Writer_dealloc,
        tp_init:                (initproc)Writer_init,
        tp_doc:                 "Writer object",
+       tp_methods:             Writer_methods,
        tp_getset:              Writer_getsetters,
 };