]> git.ipfire.org Git - people/ms/libloc.git/blame - src/python/database.c
python: Only use global loc context
[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"
9cdf6c53
MT
23#include "database.h"
24
25static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
9cdf6c53
MT
26 DatabaseObject* self = (DatabaseObject*)type->tp_alloc(type, 0);
27 if (self) {
1da9cd39 28 self->ctx = loc_ref(loc_ctx);
9cdf6c53
MT
29 }
30
31 return (PyObject*)self;
32}
33
34static void Database_dealloc(DatabaseObject* self) {
35 if (self->db)
36 loc_database_unref(self->db);
37
38 if (self->ctx)
39 loc_unref(self->ctx);
40
41 Py_TYPE(self)->tp_free((PyObject* )self);
42}
43
44static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs) {
45 const char* path = NULL;
46
47 if (!PyArg_ParseTuple(args, "s", &path))
48 return -1;
49
50 // Open the file for reading
51 FILE* f = fopen(path, "r");
52 if (!f)
53 return -1;
54
55 // Load the database
56 int r = loc_database_new(self->ctx, &self->db, f);
57 fclose(f);
58
59 // Return on any errors
60 if (r)
61 return -1;
62
63 return 0;
64}
65
d99b0256
MT
66static PyObject* Database_get_description(DatabaseObject* self) {
67 const char* description = loc_database_get_description(self->db);
68
69 return PyUnicode_FromString(description);
70}
71
72static PyObject* Database_get_vendor(DatabaseObject* self) {
73 const char* vendor = loc_database_get_vendor(self->db);
74
75 return PyUnicode_FromString(vendor);
76}
77
53524b2d
MT
78static PyObject* Database_get_created_at(DatabaseObject* self) {
79 time_t created_at = loc_database_created_at(self->db);
80
81 return PyLong_FromLong(created_at);
82}
83
9cdf6c53
MT
84static struct PyMethodDef Database_methods[] = {
85 { NULL },
86};
87
d99b0256 88static struct PyGetSetDef Database_getsetters[] = {
53524b2d
MT
89 {
90 "created_at",
91 (getter)Database_get_created_at,
92 NULL,
93 NULL,
94 NULL,
95 },
d99b0256
MT
96 {
97 "description",
98 (getter)Database_get_description,
99 NULL,
100 NULL,
101 NULL,
102 },
103 {
104 "vendor",
105 (getter)Database_get_vendor,
106 NULL,
107 NULL,
53524b2d 108 NULL,
d99b0256
MT
109 },
110 { NULL },
111};
112
9cdf6c53
MT
113PyTypeObject DatabaseType = {
114 PyVarObject_HEAD_INIT(NULL, 0)
115 tp_name: "location.Database",
116 tp_basicsize: sizeof(DatabaseObject),
117 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
118 tp_new: Database_new,
119 tp_dealloc: (destructor)Database_dealloc,
120 tp_init: (initproc)Database_init,
121 tp_doc: "Database object",
122 tp_methods: Database_methods,
d99b0256 123 tp_getset: Database_getsetters,
9cdf6c53 124};