]> git.ipfire.org Git - people/ms/libloc.git/blob - src/python/database.c
python: Make lookup function available
[people/ms/libloc.git] / src / python / database.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 <loc/libloc.h>
20 #include <loc/database.h>
21
22 #include "locationmodule.h"
23 #include "as.h"
24 #include "database.h"
25 #include "network.h"
26
27 static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
28 DatabaseObject* self = (DatabaseObject*)type->tp_alloc(type, 0);
29
30 return (PyObject*)self;
31 }
32
33 static void Database_dealloc(DatabaseObject* self) {
34 if (self->db)
35 loc_database_unref(self->db);
36
37 Py_TYPE(self)->tp_free((PyObject* )self);
38 }
39
40 static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) {
41 const char* path = NULL;
42
43 if (!PyArg_ParseTuple(args, "s", &path))
44 return -1;
45
46 // Open the file for reading
47 FILE* f = fopen(path, "r");
48 if (!f)
49 return -1;
50
51 // Load the database
52 int r = loc_database_new(loc_ctx, &self->db, f);
53 fclose(f);
54
55 // Return on any errors
56 if (r)
57 return -1;
58
59 return 0;
60 }
61
62 static PyObject* Database_get_description(DatabaseObject* self) {
63 const char* description = loc_database_get_description(self->db);
64
65 return PyUnicode_FromString(description);
66 }
67
68 static PyObject* Database_get_vendor(DatabaseObject* self) {
69 const char* vendor = loc_database_get_vendor(self->db);
70
71 return PyUnicode_FromString(vendor);
72 }
73
74 static PyObject* Database_get_created_at(DatabaseObject* self) {
75 time_t created_at = loc_database_created_at(self->db);
76
77 return PyLong_FromLong(created_at);
78 }
79
80 static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) {
81 struct loc_as* as = NULL;
82 uint32_t number = 0;
83
84 if (!PyArg_ParseTuple(args, "i", &number))
85 return NULL;
86
87 // Try to retrieve the AS
88 int r = loc_database_get_as(self->db, &as, number);
89 if (r)
90 return NULL;
91
92 // Create an AS object
93 if (as) {
94 PyObject* obj = new_as(&ASType, as);
95 loc_as_unref(as);
96
97 return obj;
98 }
99
100 // Nothing found
101 Py_RETURN_NONE;
102 }
103
104 static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) {
105 struct loc_network* network = NULL;
106 const char* address = NULL;
107
108 if (!PyArg_ParseTuple(args, "s", &address))
109 return NULL;
110
111 // Try to retrieve a matching network
112 int r = loc_database_lookup_from_string(self->db, address, &network);
113
114 // We got a network
115 if (r == 0) {
116 PyObject* obj = new_network(&NetworkType, network);
117 loc_network_unref(network);
118
119 return obj;
120
121 // Nothing found
122 } else if (r == 1) {
123 Py_RETURN_NONE;
124 }
125
126 // Unexpected error
127 return NULL;
128 }
129
130 static struct PyMethodDef Database_methods[] = {
131 {
132 "get_as",
133 (PyCFunction)Database_get_as,
134 METH_VARARGS,
135 NULL,
136 },
137 {
138 "lookup",
139 (PyCFunction)Database_lookup,
140 METH_VARARGS,
141 NULL,
142 },
143 { NULL },
144 };
145
146 static struct PyGetSetDef Database_getsetters[] = {
147 {
148 "created_at",
149 (getter)Database_get_created_at,
150 NULL,
151 NULL,
152 NULL,
153 },
154 {
155 "description",
156 (getter)Database_get_description,
157 NULL,
158 NULL,
159 NULL,
160 },
161 {
162 "vendor",
163 (getter)Database_get_vendor,
164 NULL,
165 NULL,
166 NULL,
167 },
168 { NULL },
169 };
170
171 PyTypeObject DatabaseType = {
172 PyVarObject_HEAD_INIT(NULL, 0)
173 tp_name: "location.Database",
174 tp_basicsize: sizeof(DatabaseObject),
175 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
176 tp_new: Database_new,
177 tp_dealloc: (destructor)Database_dealloc,
178 tp_init: (initproc)Database_init,
179 tp_doc: "Database object",
180 tp_methods: Database_methods,
181 tp_getset: Database_getsetters,
182 };