]> git.ipfire.org Git - location/libloc.git/blob - src/python/locationmodule.c
3b20d670ee8e98f20ab9887038e06659713040cd
[location/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 #include <syslog.h>
19
20 #include "locationmodule.h"
21 #include "as.h"
22 #include "country.h"
23 #include "database.h"
24 #include "network.h"
25 #include "writer.h"
26
27 /* Declare global context */
28 struct loc_ctx* loc_ctx;
29
30 PyMODINIT_FUNC PyInit_location(void);
31
32 static void location_free(void) {
33 // Release context
34 if (loc_ctx)
35 loc_unref(loc_ctx);
36 }
37
38 static PyObject* set_log_level(PyObject* m, PyObject* args) {
39 int priority = LOG_INFO;
40
41 if (!PyArg_ParseTuple(args, "i", &priority))
42 return NULL;
43
44 loc_set_log_priority(loc_ctx, priority);
45
46 Py_RETURN_NONE;
47 }
48
49 static PyMethodDef location_module_methods[] = {
50 {
51 "set_log_level",
52 (PyCFunction)set_log_level,
53 METH_VARARGS,
54 NULL,
55 },
56 { NULL },
57 };
58
59 static struct PyModuleDef location_module = {
60 .m_base = PyModuleDef_HEAD_INIT,
61 .m_name = "location",
62 .m_size = -1,
63 .m_doc = "Python module for libloc",
64 .m_methods = location_module_methods,
65 .m_free = (freefunc)location_free,
66 };
67
68 PyMODINIT_FUNC PyInit_location(void) {
69 // Initialise loc context
70 int r = loc_new(&loc_ctx);
71 if (r)
72 return NULL;
73
74 PyObject* m = PyModule_Create(&location_module);
75 if (!m)
76 return NULL;
77
78 // AS
79 if (PyType_Ready(&ASType) < 0)
80 return NULL;
81
82 Py_INCREF(&ASType);
83 PyModule_AddObject(m, "AS", (PyObject *)&ASType);
84
85 // Country
86 if (PyType_Ready(&CountryType) < 0)
87 return NULL;
88
89 Py_INCREF(&CountryType);
90 PyModule_AddObject(m, "Country", (PyObject *)&CountryType);
91
92 // Database
93 if (PyType_Ready(&DatabaseType) < 0)
94 return NULL;
95
96 Py_INCREF(&DatabaseType);
97 PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType);
98
99 // Database Enumerator
100 if (PyType_Ready(&DatabaseEnumeratorType) < 0)
101 return NULL;
102
103 Py_INCREF(&DatabaseEnumeratorType);
104 //PyModule_AddObject(m, "DatabaseEnumerator", (PyObject *)&DatabaseEnumeratorType);
105
106 // Network
107 if (PyType_Ready(&NetworkType) < 0)
108 return NULL;
109
110 Py_INCREF(&NetworkType);
111 PyModule_AddObject(m, "Network", (PyObject *)&NetworkType);
112
113 // Writer
114 if (PyType_Ready(&WriterType) < 0)
115 return NULL;
116
117 Py_INCREF(&WriterType);
118 PyModule_AddObject(m, "Writer", (PyObject *)&WriterType);
119
120 // Add constants
121 if (PyModule_AddStringConstant(m, "__version__", VERSION))
122 return NULL;
123
124 // Add flags
125 if (PyModule_AddIntConstant(m, "FLAG_ANONYMOUS_PROXY", LOC_NETWORK_FLAG_ANONYMOUS_PROXY))
126 return NULL;
127
128 if (PyModule_AddIntConstant(m, "FLAG_SATELLITE_PROVIDER", LOC_NETWORK_FLAG_SATELLITE_PROVIDER))
129 return NULL;
130
131 if (PyModule_AddIntConstant(m, "FLAG_ANYCAST", LOC_NETWORK_FLAG_ANYCAST))
132 return NULL;
133
134 return m;
135 }