2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
19 #include <loc/libloc.h>
20 #include <loc/database.h>
22 #include "locationmodule.h"
26 static PyObject
* Database_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
27 DatabaseObject
* self
= (DatabaseObject
*)type
->tp_alloc(type
, 0);
29 self
->ctx
= loc_ref(loc_ctx
);
32 return (PyObject
*)self
;
35 static void Database_dealloc(DatabaseObject
* self
) {
37 loc_database_unref(self
->db
);
42 Py_TYPE(self
)->tp_free((PyObject
* )self
);
45 static int Database_init(DatabaseObject
* self
, PyObject
* args
, PyObject
* kwargs
) {
46 const char* path
= NULL
;
48 if (!PyArg_ParseTuple(args
, "s", &path
))
51 // Open the file for reading
52 FILE* f
= fopen(path
, "r");
57 int r
= loc_database_new(self
->ctx
, &self
->db
, f
);
60 // Return on any errors
67 static PyObject
* Database_get_description(DatabaseObject
* self
) {
68 const char* description
= loc_database_get_description(self
->db
);
70 return PyUnicode_FromString(description
);
73 static PyObject
* Database_get_vendor(DatabaseObject
* self
) {
74 const char* vendor
= loc_database_get_vendor(self
->db
);
76 return PyUnicode_FromString(vendor
);
79 static PyObject
* Database_get_created_at(DatabaseObject
* self
) {
80 time_t created_at
= loc_database_created_at(self
->db
);
82 return PyLong_FromLong(created_at
);
85 static PyObject
* Database_get_as(DatabaseObject
* self
, PyObject
* args
) {
86 struct loc_as
* as
= NULL
;
89 if (!PyArg_ParseTuple(args
, "i", &number
))
92 // Try to retrieve the AS
93 int r
= loc_database_get_as(self
->db
, &as
, number
);
97 // Create an AS object
99 PyObject
* obj
= new_as(&ASType
, as
);
109 static struct PyMethodDef Database_methods
[] = {
112 (PyCFunction
)Database_get_as
,
119 static struct PyGetSetDef Database_getsetters
[] = {
122 (getter
)Database_get_created_at
,
129 (getter
)Database_get_description
,
136 (getter
)Database_get_vendor
,
144 PyTypeObject 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
,
154 tp_getset
: Database_getsetters
,