]> git.ipfire.org Git - people/ms/libloc.git/blame - src/python/locationmodule.c
python: Move C module to make space for some native python code
[people/ms/libloc.git] / src / python / locationmodule.c
CommitLineData
18e2b14c
MT
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>
c941dd31 18#include <syslog.h>
18e2b14c 19
f4fef543
MT
20#include <loc/resolv.h>
21
62ffafdd 22#include "locationmodule.h"
fadebc89 23#include "as.h"
af208e26 24#include "country.h"
9cdf6c53 25#include "database.h"
39967361 26#include "network.h"
d688e569 27#include "writer.h"
9cdf6c53 28
51173f45
MT
29/* Declare global context */
30struct loc_ctx* loc_ctx;
31
7c13a4e6 32PyMODINIT_FUNC PyInit__location(void);
18e2b14c 33
62ffafdd
MT
34static void location_free(void) {
35 // Release context
36 if (loc_ctx)
37 loc_unref(loc_ctx);
38}
39
c941dd31
MT
40static PyObject* set_log_level(PyObject* m, PyObject* args) {
41 int priority = LOG_INFO;
42
43 if (!PyArg_ParseTuple(args, "i", &priority))
44 return NULL;
45
46 loc_set_log_priority(loc_ctx, priority);
47
48 Py_RETURN_NONE;
49}
50
f4fef543
MT
51static PyObject* discover_latest_version(PyObject* m, PyObject* args) {
52 const char* domain = NULL;
53
54 if (!PyArg_ParseTuple(args, "|s", &domain))
55 return NULL;
56
57 time_t t = 0;
58
59 int r = loc_discover_latest_version(loc_ctx, domain, &t);
60 if (r) {
61 PyErr_SetFromErrno(PyExc_OSError);
62 return NULL;
63 }
64
65 return PyLong_FromUnsignedLong(t);
66}
67
0f0829ef
MT
68static PyObject* country_code_is_valid(PyObject* m, PyObject* args) {
69 const char* country_code = NULL;
70
71 if (!PyArg_ParseTuple(args, "s", &country_code))
72 return NULL;
73
74 if (loc_country_code_is_valid(country_code))
75 Py_RETURN_TRUE;
76
77 Py_RETURN_FALSE;
78}
79
18e2b14c 80static PyMethodDef location_module_methods[] = {
0f0829ef
MT
81 {
82 "country_code_is_valid",
83 (PyCFunction)country_code_is_valid,
84 METH_VARARGS,
85 NULL,
86 },
f4fef543
MT
87 {
88 "discover_latest_version",
89 (PyCFunction)discover_latest_version,
90 METH_VARARGS,
91 NULL,
92 },
c941dd31
MT
93 {
94 "set_log_level",
95 (PyCFunction)set_log_level,
96 METH_VARARGS,
97 NULL,
98 },
18e2b14c
MT
99 { NULL },
100};
101
102static struct PyModuleDef location_module = {
103 .m_base = PyModuleDef_HEAD_INIT,
7c13a4e6 104 .m_name = "_location",
18e2b14c
MT
105 .m_size = -1,
106 .m_doc = "Python module for libloc",
107 .m_methods = location_module_methods,
62ffafdd 108 .m_free = (freefunc)location_free,
18e2b14c
MT
109};
110
7c13a4e6 111PyMODINIT_FUNC PyInit__location(void) {
62ffafdd
MT
112 // Initialise loc context
113 int r = loc_new(&loc_ctx);
114 if (r)
115 return NULL;
116
18e2b14c
MT
117 PyObject* m = PyModule_Create(&location_module);
118 if (!m)
119 return NULL;
120
fadebc89
MT
121 // AS
122 if (PyType_Ready(&ASType) < 0)
123 return NULL;
124
125 Py_INCREF(&ASType);
126 PyModule_AddObject(m, "AS", (PyObject *)&ASType);
127
af208e26
MT
128 // Country
129 if (PyType_Ready(&CountryType) < 0)
130 return NULL;
131
132 Py_INCREF(&CountryType);
133 PyModule_AddObject(m, "Country", (PyObject *)&CountryType);
134
9cdf6c53
MT
135 // Database
136 if (PyType_Ready(&DatabaseType) < 0)
137 return NULL;
138
139 Py_INCREF(&DatabaseType);
140 PyModule_AddObject(m, "Database", (PyObject *)&DatabaseType);
141
afb426df
MT
142 // Database Enumerator
143 if (PyType_Ready(&DatabaseEnumeratorType) < 0)
144 return NULL;
145
146 Py_INCREF(&DatabaseEnumeratorType);
147 //PyModule_AddObject(m, "DatabaseEnumerator", (PyObject *)&DatabaseEnumeratorType);
148
39967361
MT
149 // Network
150 if (PyType_Ready(&NetworkType) < 0)
151 return NULL;
152
153 Py_INCREF(&NetworkType);
154 PyModule_AddObject(m, "Network", (PyObject *)&NetworkType);
155
d688e569
MT
156 // Writer
157 if (PyType_Ready(&WriterType) < 0)
158 return NULL;
159
160 Py_INCREF(&WriterType);
161 PyModule_AddObject(m, "Writer", (PyObject *)&WriterType);
162
ddb184be 163 // Add constants
0a56a83e
MT
164 if (PyModule_AddStringConstant(m, "__version__", VERSION))
165 return NULL;
ddb184be 166
24ca7992 167 // Add flags
c1467f55 168 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANONYMOUS_PROXY", LOC_NETWORK_FLAG_ANONYMOUS_PROXY))
24ca7992
MT
169 return NULL;
170
c1467f55 171 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_SATELLITE_PROVIDER", LOC_NETWORK_FLAG_SATELLITE_PROVIDER))
24ca7992
MT
172 return NULL;
173
c1467f55 174 if (PyModule_AddIntConstant(m, "NETWORK_FLAG_ANYCAST", LOC_NETWORK_FLAG_ANYCAST))
24ca7992
MT
175 return NULL;
176
18e2b14c
MT
177 return m;
178}