#include "locationmodule.h"
#include "as.h"
+#include "country.h"
#include "network.h"
#include "writer.h"
}
static int Writer_init(WriterObject* self, PyObject* args, PyObject* kwargs) {
- // Create the writer object
- int r = loc_writer_new(loc_ctx, &self->writer);
- if (r)
+ PyObject* private_key = NULL;
+ FILE* f = NULL;
+
+ // Parse arguments
+ if (!PyArg_ParseTuple(args, "|O", &private_key))
return -1;
- return 0;
+ // Convert into FILE*
+ if (private_key && private_key != Py_None) {
+ int fd = PyObject_AsFileDescriptor(private_key);
+ if (fd < 0)
+ return -1;
+
+ // Re-open file descriptor
+ f = fdopen(fd, "r");
+ if (!f) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ return -1;
+ }
+ }
+
+ // Create the writer object
+ int r = loc_writer_new(loc_ctx, &self->writer, f);
+
+ return r;
}
static PyObject* Writer_get_vendor(WriterObject* self) {
return obj;
}
+static PyObject* Writer_add_country(WriterObject* self, PyObject* args) {
+ struct loc_country* country;
+ const char* country_code;
+
+ if (!PyArg_ParseTuple(args, "s", &country_code))
+ return NULL;
+
+ // Create country object
+ int r = loc_writer_add_country(self->writer, &country, country_code);
+ if (r) {
+ switch (r) {
+ case -EINVAL:
+ PyErr_SetString(PyExc_ValueError, "Invalid network");
+ break;
+
+ default:
+ return NULL;
+ }
+ }
+
+ PyObject* obj = new_country(&CountryType, country);
+ loc_country_unref(country);
+
+ return obj;
+}
+
static PyObject* Writer_add_network(WriterObject* self, PyObject* args) {
struct loc_network* network;
const char* string = NULL;
static PyObject* Writer_write(WriterObject* self, PyObject* args) {
const char* path = NULL;
+ int version = LOC_DATABASE_VERSION_UNSET;
- if (!PyArg_ParseTuple(args, "s", &path))
+ if (!PyArg_ParseTuple(args, "s|i", &path, &version))
return NULL;
- FILE* f = fopen(path, "w");
+ FILE* f = fopen(path, "w+");
if (!f) {
PyErr_Format(PyExc_IOError, strerror(errno));
return NULL;
}
- int r = loc_writer_write(self->writer, f);
+ int r = loc_writer_write(self->writer, f, (enum loc_database_version)version);
fclose(f);
// Raise any errors
METH_VARARGS,
NULL,
},
+ {
+ "add_country",
+ (PyCFunction)Writer_add_country,
+ METH_VARARGS,
+ NULL,
+ },
{
"add_network",
(PyCFunction)Writer_add_network,
PyTypeObject WriterType = {
PyVarObject_HEAD_INIT(NULL, 0)
- tp_name: "location.Writer",
- tp_basicsize: sizeof(WriterObject),
- tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
- tp_new: Writer_new,
- tp_dealloc: (destructor)Writer_dealloc,
- tp_init: (initproc)Writer_init,
- tp_doc: "Writer object",
- tp_methods: Writer_methods,
- tp_getset: Writer_getsetters,
+ .tp_name = "location.Writer",
+ .tp_basicsize = sizeof(WriterObject),
+ .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ .tp_new = Writer_new,
+ .tp_dealloc = (destructor)Writer_dealloc,
+ .tp_init = (initproc)Writer_init,
+ .tp_doc = "Writer object",
+ .tp_methods = Writer_methods,
+ .tp_getset = Writer_getsetters,
};