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