%% headers #include #include #include "pygobject.h" static PyObject * pylt_wrap_gobj_list(GList *list) { GList *l; PyObject *item, *ret; ret = PyList_New(0); if (ret == NULL) return NULL; for (l = list; l != NULL; l = l->next) { item = pygobject_new((GObject *)l->data); if (item == NULL) { Py_DECREF(ret); return NULL; } PyList_Append(ret, item); Py_DECREF(item); } return ret; } GList * pylt_unwrap_gobj_list(PyObject *py_items, PyTypeObject *type, gboolean *ok) { int len, i; GList *items; *ok = TRUE; len = PyList_Size(py_items); for (i = 0; i < len; i++) { PyObject *item = PyList_GetItem(py_items, i); if (!pygobject_check(item, type)) { char *err = g_strdup_printf("list item not a %s", type->tp_name); PyErr_SetString(PyExc_TypeError, err); g_free(err); g_list_free(items); *ok = FALSE; return NULL; } items = g_list_append(items, pygobject_get(item)); } return items; } PyTypeObject PyLtTag_Type; GList * pylt_unwrap_tags_list(PyObject *py_items, gboolean *ok) { int len, i; GList *items = NULL; *ok = TRUE; len = PyList_Size(py_items); if (len == 0) { PyErr_SetString(PyExc_ValueError, "tags list cannot be empty"); *ok = FALSE; return NULL; } for (i = 0; i < len; i++) { PyObject *item = PyList_GetItem(py_items, i); if (pygobject_check(item, &PyString_Type)) { items = g_list_append(items, PyString_AsString(item)); } else if (pygobject_check(item, &PyLtTag_Type)) { items = g_list_append(items, (char *)lt_tag_get_name(LT_TAG(pygobject_get(item)))); } else { PyErr_Format(PyExc_TypeError, "list item not a string or Tag object at index %d", i); g_list_free(items); *ok = FALSE; return NULL; } } return items; } %% modulename leaftag %% import gobject.GObject as PyGObject_Type %% ignore-glob *_get_type %% override lt_get_all_sources kwargs static PyObject * _wrap_lt_get_all_sources(PyGObject *self, PyObject *args, PyObject *kwargs) { return pylt_wrap_gobj_list(lt_get_all_sources()); } %% override lt_get_all_tags kwargs static PyObject * _wrap_lt_get_all_tags(PyGObject *self, PyObject *args, PyObject *kwargs) { return pylt_wrap_gobj_list(lt_get_all_tags()); } %% override lt_get_sources_with_schema kwargs static PyObject * _wrap_lt_get_sources_with_schema(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "schema", NULL }; char *schema; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z:lt_get_sources_with_schema", kwlist, &schema)) { return NULL; } return pylt_wrap_gobj_list(lt_get_sources_with_schema(schema)); } %% override lt_get_sources_with_tags kwargs static PyObject * _wrap_lt_get_sources_with_tags(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "tags", "schema", NULL }; PyObject *py_tags; GList *results; GList *tags = NULL; char *schema; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oz:lt_get_sources_with_tags", kwlist, &py_tags, &schema)) { return NULL; } if (PyString_Check(py_tags)) { tags = g_list_append(tags, PyString_AsString(py_tags)); results = lt_get_sources_with_tag_names(tags, schema); } else if (pygobject_check(py_tags, &PyLtTag_Type)) { tags = g_list_append(tags, pygobject_get(py_tags)); results = lt_get_sources_with_tags(tags, schema); } else if (PyList_Check(py_tags)) { gboolean ok; tags = pylt_unwrap_tags_list(py_tags, &ok); if (ok) results = lt_get_sources_with_tag_names(tags, schema); else return NULL; } g_list_free(tags); return pylt_wrap_gobj_list(results); } %% override lt_get_tag_names_valid kwargs static PyObject * _wrap_lt_get_tag_names_valid(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "tags", NULL }; PyObject *py_tags; GList *tag_names = NULL; PyObject *result; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:lt_get_tag_names_valid", kwlist, &py_tags)); { return NULL; } if (PyString_Check(py_tags)) { tag_names = g_list_append(tag_names, PyString_AsString(py_tags)); } else if (PyList_Check(py_tags)) { gboolean ok; tag_names = pylt_unwrap_gobj_list(py_tags, &PyString_Type, &ok); if (!ok) return NULL; } result = PyBool_FromLong(lt_get_tag_names_valid(tag_names)); g_list_free(tag_names); return result; } %% override lt_get_visible_tags kwargs static PyObject * _wrap_lt_get_visible_tags(PyGObject *self, PyObject *args, PyObject *kwargs) { return pylt_wrap_gobj_list(lt_get_visible_tags()); } %% override lt_source_get_tags kwargs static PyObject * _wrap_lt_source_get_tags(PyGObject *self, PyObject *args, PyObject *kwargs) { return pylt_wrap_gobj_list(lt_source_get_tags(LT_SOURCE(self->obj))); } %% override lt_source_lookup_many kwargs static PyObject * _wrap_lt_source_lookup_many(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "uris", NULL }; GList *uris; PyObject *py_uris; gboolean ok; PyObject *ret; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!:lt_source_lookup_many", kwlist, &PyList_Type, &py_uris)) { return NULL; } uris = pylt_unwrap_gobj_list(py_uris, &PyString_Type, &ok); if (!ok) return NULL; ret = pylt_wrap_gobj_list(lt_source_lookup_many(uris)); g_list_free(uris); return ret; } %% override lt_source_tag kwargs static PyObject * _wrap_lt_source_tag(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "tags", NULL }; char *schema; PyObject *py_tags; GList *tags = NULL; gboolean ok = TRUE; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:lt_source_tag", kwlist, &py_tags)) { return NULL; } if (PyString_Check(py_tags)) { tags = g_list_append(tags, PyString_AsString(py_tags)); lt_source_tag_with_names(LT_SOURCE(self->obj), tags); } else if (pygobject_check(py_tags, &PyLtTag_Type)) { tags = g_list_append(tags, pygobject_get(py_tags)); lt_source_tag(LT_SOURCE(self->obj), tags); } else if (PyList_Check(py_tags)) { tags = pylt_unwrap_tags_list(py_tags, &ok); if (ok) lt_source_tag_with_names(LT_SOURCE(self->obj), tags); } g_list_free(tags); if (ok) { Py_INCREF(Py_None); return Py_None; } return NULL; } %% override lt_source_untag kwargs static PyObject * _wrap_lt_source_untag(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "tags", NULL }; char *schema; PyObject *py_tags; GList *tags = NULL; gboolean ok = TRUE; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:lt_source_tag", kwlist, &py_tags)) { return NULL; } if (PyString_Check(py_tags)) { tags = g_list_append(tags, PyString_AsString(py_tags)); lt_source_tag_with_names(LT_SOURCE(self->obj), tags); } else if (pygobject_check(py_tags, &PyLtTag_Type)) { tags = g_list_append(tags, pygobject_get(py_tags)); lt_source_tag(LT_SOURCE(self->obj), tags); } else if (PyList_Check(py_tags)) { tags = pylt_unwrap_tags_list(py_tags, &ok); if (ok) lt_source_tag_with_names(LT_SOURCE(self->obj), tags); } g_list_free(tags); if (ok) { Py_INCREF(Py_None); return Py_None; } return NULL; } %% override lt_tag_get_sources kwargs static PyObject * _wrap_lt_tag_get_sources(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "schema", NULL }; char *schema; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "z:lt_tag_get_sources", kwlist, &schema)) { return NULL; } return pylt_wrap_gobj_list(lt_tag_get_sources(LT_TAG(self->obj), schema)); } %% override lt_tag_lookup kwargs static PyObject * _wrap_lt_tag_lookup(PyGObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = { "tags", NULL }; PyObject *py_tags; GList *results; GList *tags = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:lt_tag_lookup", kwlist, &py_tags)); { return NULL; } if (PyString_Check(py_tags)) { return pygobject_new(G_OBJECT( lt_tag_lookup(PyString_AsString(py_tags)))); } else if (PyList_Check(py_tags)) { gboolean ok; results = pylt_unwrap_gobj_list(py_tags, &PyString_Type, &ok); if (ok) return pylt_wrap_gobj_list(results); } return NULL; } %% // vim: filetype=c