]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/python/database.c
Add license attribute to the database
[people/ms/libloc.git] / src / python / database.c
index 7bac3bca0b8883af29804e68800149b1acb8b01d..d41da47e52813c23ecbb94da3d27bae998f3c6d6 100644 (file)
@@ -34,6 +34,9 @@ static void Database_dealloc(DatabaseObject* self) {
        if (self->db)
                loc_database_unref(self->db);
 
+       if (self->path)
+               free(self->path);
+
        Py_TYPE(self)->tp_free((PyObject* )self);
 }
 
@@ -43,8 +46,10 @@ static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs)
        if (!PyArg_ParseTuple(args, "s", &path))
                return -1;
 
+       self->path = strdup(path);
+
        // Open the file for reading
-       FILE* f = fopen(path, "r");
+       FILE* f = fopen(self->path, "r");
        if (!f)
                return -1;
 
@@ -59,6 +64,10 @@ static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs)
        return 0;
 }
 
+static PyObject* Database_repr(DatabaseObject* self) {
+       return PyUnicode_FromFormat("<Database %s>", self->path);
+}
+
 static PyObject* Database_get_description(DatabaseObject* self) {
        const char* description = loc_database_get_description(self->db);
 
@@ -71,6 +80,12 @@ static PyObject* Database_get_vendor(DatabaseObject* self) {
        return PyUnicode_FromString(vendor);
 }
 
+static PyObject* Database_get_license(DatabaseObject* self) {
+       const char* license = loc_database_get_license(self->db);
+
+       return PyUnicode_FromString(license);
+}
+
 static PyObject* Database_get_created_at(DatabaseObject* self) {
        time_t created_at = loc_database_created_at(self->db);
 
@@ -160,6 +175,13 @@ static struct PyGetSetDef Database_getsetters[] = {
                NULL,
                NULL,
        },
+       {
+               "license",
+               (getter)Database_get_license,
+               NULL,
+               NULL,
+               NULL,
+       },
        {
                "vendor",
                (getter)Database_get_vendor,
@@ -181,4 +203,5 @@ PyTypeObject DatabaseType = {
        tp_doc:                 "Database object",
        tp_methods:             Database_methods,
        tp_getset:              Database_getsetters,
+       tp_repr:                (reprfunc)Database_repr,
 };