]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/locationmodule.c
f4ea6cf6602000c66193740dc503c85cca8178c1
[people/ms/libloc.git] / src / python / locationmodule.c
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 "locationmodule.h"
20 #include "as.h"
21 #include "database.h"
22 #include "writer.h"
23
24 PyMODINIT_FUNC PyInit_location(void);
25
26 static void location_free(void) {
27 // Release context
28 if (loc_ctx)
29 loc_unref(loc_ctx);
30 }
31
32 static PyMethodDef location_module_methods[] = {
33 { NULL },
34 };
35
36 static struct PyModuleDef location_module = {
37 .m_base = PyModuleDef_HEAD_INIT,
38 .m_name = "location",
39 .m_size = -1,
40 .m_doc = "Python module for libloc",
41 .m_methods = location_module_methods,
42 .m_free = (freefunc)location_free,
43 };
44
45 PyMODINIT_FUNC PyInit_location(void) {
46 // Initialise loc context
47 int r = loc_new(&loc_ctx);
48 if (r)
49 return NULL;
50
51 PyObject* m = PyModule_Create(&location_module);
52 if (!m)
53 return NULL;
54
55 // AS
56 if (PyType_Ready(&ASType) < 0)
57 return NULL;
58
59 Py_INCREF(&ASType);
60 PyModule_AddObject(m, "AS", (PyObject *)&ASType);
61
62 // Database
63 if (PyType_Ready(&DatabaseType) < 0)
64 return NULL;
65
66 Py_INCREF(&DatabaseType);
67 PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType);
68
69 // Writer
70 if (PyType_Ready(&WriterType) < 0)
71 return NULL;
72
73 Py_INCREF(&WriterType);
74 PyModule_AddObject(m, "Writer", (PyObject *)&WriterType);
75
76 return m;
77 }