]> git.ipfire.org Git - people/ms/libloc.git/blame - src/python/database.c
python: Remove reference to context from database
[people/ms/libloc.git] / src / python / database.c
CommitLineData
9cdf6c53
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>
18
9fc7f001
MT
19#include <loc/libloc.h>
20#include <loc/database.h>
21
1da9cd39 22#include "locationmodule.h"
86ca7ef7 23#include "as.h"
9cdf6c53
MT
24#include "database.h"
25
26static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
9cdf6c53 27 DatabaseObject* self = (DatabaseObject*)type->tp_alloc(type, 0);
9cdf6c53
MT
28
29 return (PyObject*)self;
30}
31
32static void Database_dealloc(DatabaseObject* self) {
33 if (self->db)
34 loc_database_unref(self->db);
35
9cdf6c53
MT
36 Py_TYPE(self)->tp_free((PyObject* )self);
37}
38
39static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) {
40 const char* path = NULL;
41
42 if (!PyArg_ParseTuple(args, "s", &path))
43 return -1;
44
45 // Open the file for reading
46 FILE* f = fopen(path, "r");
47 if (!f)
48 return -1;
49
50 // Load the database
38e07ee0 51 int r = loc_database_new(loc_ctx, &self->db, f);
9cdf6c53
MT
52 fclose(f);
53
54 // Return on any errors
55 if (r)
56 return -1;
57
58 return 0;
59}
60
d99b0256
MT
61static PyObject* Database_get_description(DatabaseObject* self) {
62 const char* description = loc_database_get_description(self->db);
63
64 return PyUnicode_FromString(description);
65}
66
67static PyObject* Database_get_vendor(DatabaseObject* self) {
68 const char* vendor = loc_database_get_vendor(self->db);
69
70 return PyUnicode_FromString(vendor);
71}
72
53524b2d
MT
73static PyObject* Database_get_created_at(DatabaseObject* self) {
74 time_t created_at = loc_database_created_at(self->db);
75
76 return PyLong_FromLong(created_at);
77}
78
86ca7ef7
MT
79static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) {
80 struct loc_as* as = NULL;
81 uint32_t number = 0;
82
83 if (!PyArg_ParseTuple(args, "i", &number))
84 return NULL;
85
86 // Try to retrieve the AS
87 int r = loc_database_get_as(self->db, &as, number);
88 if (r)
89 return NULL;
90
91 // Create an AS object
92 if (as) {
93 PyObject* obj = new_as(&ASType, as);
94 loc_as_unref(as);
95
96 return obj;
97 }
98
99 // Nothing found
100 Py_RETURN_NONE;
101}
102
9cdf6c53 103static struct PyMethodDef Database_methods[] = {
86ca7ef7
MT
104 {
105 "get_as",
106 (PyCFunction)Database_get_as,
107 METH_VARARGS,
108 NULL,
109 },
9cdf6c53
MT
110 { NULL },
111};
112
d99b0256 113static struct PyGetSetDef Database_getsetters[] = {
53524b2d
MT
114 {
115 "created_at",
116 (getter)Database_get_created_at,
117 NULL,
118 NULL,
119 NULL,
120 },
d99b0256
MT
121 {
122 "description",
123 (getter)Database_get_description,
124 NULL,
125 NULL,
126 NULL,
127 },
128 {
129 "vendor",
130 (getter)Database_get_vendor,
131 NULL,
132 NULL,
53524b2d 133 NULL,
d99b0256
MT
134 },
135 { NULL },
136};
137
9cdf6c53
MT
138PyTypeObject DatabaseType = {
139 PyVarObject_HEAD_INIT(NULL, 0)
140 tp_name: "location.Database",
141 tp_basicsize: sizeof(DatabaseObject),
142 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
143 tp_new: Database_new,
144 tp_dealloc: (destructor)Database_dealloc,
145 tp_init: (initproc)Database_init,
146 tp_doc: "Database object",
147 tp_methods: Database_methods,
d99b0256 148 tp_getset: Database_getsetters,
9cdf6c53 149};