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