gstreamer/gst/gststructure.override
Johan Dahlin 6782bfbc7b Split out gst.override to a few more files.
Original commit message from CVS:
Split out gst.override to a few more files.
Rename gstpad-handlers.override to gstpad.override
Include more information for some LinkError exceptions
2005-01-01 15:23:42 +00:00

333 lines
9.3 KiB
C

/* -*- Mode: C; c-basic-offset: 4 -*- */
/* gst-python
* Copyright (C) 2005 Johan Dahlin
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Johan Dahlin <johan@gnome.org>
*/
%%
override gst_structure_new kwargs
static int
_wrap_gst_structure_new(PyGBoxed *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "name", NULL };
char *name;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:GstStructure.__init__", kwlist, &name))
return -1;
self->gtype = GST_TYPE_STRUCTURE;
self->free_on_dealloc = FALSE;
self->boxed = gst_structure_new(name, NULL);
if (!self->boxed) {
PyErr_SetString(PyExc_RuntimeError, "could not create GstStructure object");
return -1;
}
self->free_on_dealloc = TRUE;
return 0;
}
%%
override gst_structure_set_value kwargs
static PyObject *
_wrap_gst_structure_set_value(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "field", "value", "type_name", NULL };
char *field;
PyObject *py_value = NULL;
char *type_name = NULL;
GType type;
GValue value = { 0 };
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"sO|s:GstStructure.set_value",
kwlist, &field, &py_value,
&type_name)) {
return NULL;
}
if (type_name) {
if (strcmp (type_name, "char") == 0) {
type = G_TYPE_CHAR;
} else if (strcmp (type_name, "uchar") == 0) {
type = G_TYPE_UCHAR;
} else if (strcmp (type_name, "boolean") == 0) {
type = G_TYPE_BOOLEAN;
} else if (strcmp (type_name, "int") == 0) {
type = G_TYPE_INT;
} else if (strcmp (type_name, "uint") == 0) {
type = G_TYPE_UINT;
} else if (strcmp (type_name, "long") == 0) {
type = G_TYPE_LONG;
} else if (strcmp (type_name, "ulong") == 0) {
type = G_TYPE_ULONG;
} else if (strcmp (type_name, "int64") == 0) {
type = G_TYPE_INT64;
} else if (strcmp (type_name, "uint64") == 0) {
type = G_TYPE_UINT64;
} else if (strcmp (type_name, "float") == 0) {
type = G_TYPE_FLOAT;
} else if (strcmp (type_name, "double") == 0) {
type = G_TYPE_DOUBLE;
} else if (strcmp (type_name, "string") == 0) {
type = G_TYPE_STRING;
} else {
PyErr_SetString(PyExc_TypeError,
"invalid type name");
return NULL;
}
} else {
/* Let PyGTK guess a GType for the object. */
type = pyg_type_from_object((PyObject *) py_value->ob_type);
if (type == 0) {
return NULL;
}
}
g_value_init(&value, type);
if (pyg_value_from_pyobject(&value, py_value) != 0) {
return NULL;
}
gst_structure_set_value(pyg_boxed_get(self, GstStructure), field,
&value);
Py_INCREF(Py_None);
return Py_None;
}
%%
override gst_structure_get_int kwargs
static PyObject *
_wrap_gst_structure_get_int(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "fieldname", NULL };
char *fieldname;
int value;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"s:GstStructure.get_int",
kwlist, &fieldname))
return NULL;
if (gst_structure_get_int(pyg_boxed_get(self, GstStructure), fieldname, &value))
return PyInt_FromLong(value);
/* XXX: Raise exception? */
Py_INCREF(Py_None);
return Py_None;
}
%%
define GstStructure.has_key args
static PyObject*
_wrap_gst_structure_has_key(PyGObject *self, PyObject *args)
{
gchar *key;
gboolean has_field;
if (!PyArg_ParseTuple(args, "s:GstStructure.has_key", &key))
return NULL;
has_field = gst_structure_has_field((GstStructure*)self->obj, key);
return PyBool_FromLong(has_field);
}
%%
override-slot GstStructure.tp_as_mapping
static int
_wrap_gst_structure_length(PyGObject *self)
{
return gst_structure_n_fields((GstStructure*)self->obj);
}
static PyObject *
_wrap_gst_structure_subscript(PyGObject *self, PyObject *py_key)
{
PyObject *v = NULL;
const char *field = PyString_AsString(py_key);
if (gst_structure_has_field((GstStructure*)self->obj, field)) {
const GValue *gvalue;
gvalue = gst_structure_get_value((GstStructure*)self->obj, field);
g_assert(gvalue != NULL);
v = pyg_value_as_pyobject(gvalue, TRUE);
} else {
PyErr_SetString(PyExc_KeyError, field);
}
if (v != NULL)
Py_INCREF(v);
return v;
}
static int
_wrap_gst_structure_ass_subscript(PyGObject *self,
PyObject *py_key,
PyObject *py_value)
{
const char *key;
GstStructure* structure;
structure = (GstStructure*)self->obj;
key = PyString_AsString(py_key);
if (py_value != NULL) {
if (PyString_Check(py_value)) {
#if 0
GValue *value = NULL;
gst_structure_field_from_string(PyString_AsString(py_value), value);
g_print ("gvalue: %s %s %s\n",
PyString_AsString(py_value),
gst_value_serialize(value),
G_VALUE_TYPE_NAME(value));
gst_structure_set_value(structure, key, value);
#else
GValue value = { 0, };
g_value_init (&value, G_TYPE_STRING);
gst_value_deserialize(&value, PyString_AsString(py_value));
gst_structure_set_value(structure, key, &value);
g_value_unset(&value);
#endif
// gst_structure_set(structure, key, G_TYPE_STRING, PyString_AsString(py_value), NULL);
} else if (PyInt_Check(py_value))
gst_structure_set(structure, key, G_TYPE_INT, PyInt_AsLong(py_value), NULL);
else if (PyFloat_Check(py_value))
gst_structure_set(structure, key, G_TYPE_DOUBLE, PyFloat_AsDouble(py_value), NULL);
#if 0
g_value_init(&value, g_type_from_name("PyObject"));
if (pyg_value_from_pyobject(&value, py_value)) {
PyErr_SetString(PyExc_TypeError, "can't convert value");
return -1;
}
gst_structure_set_value(structure, key, &value);
g_value_unset(&value);
#endif
} else {
gst_structure_remove_field(structure, key);
}
return 0;
}
static PyMappingMethods _wrap_gst_structure_tp_as_mapping = {
(inquiry)_wrap_gst_structure_length, /* mp_length */
(binaryfunc)_wrap_gst_structure_subscript, /* mp_subscript */
(objobjargproc)_wrap_gst_structure_ass_subscript /* mp_ass_subscript */
};
%%
override gst_structure_foreach kwargs
static gboolean
pygst_structure_foreach_marshal(GQuark field_id,
GValue *value,
gpointer user_data)
{
PyGstCustomNotify *cunote = user_data;
PyObject *py_field, *py_value, *retobj;
gboolean retval = TRUE;
PyGILState_STATE state;
g_assert(cunote->func);
state = pyg_gil_state_ensure();
py_field = Py_BuildValue("s", g_quark_to_string(field_id));
py_value = pyg_value_as_pyobject(value, FALSE);
if (cunote->data)
retobj = PyEval_CallFunction(cunote->func, "(NNO)",
py_field, py_value,
cunote->data);
else
retobj = PyEval_CallFunction(cunote->func, "(NN)",
py_field, py_value);
if (PyErr_Occurred () || (retobj == NULL) || (retobj == Py_None)) {
PyErr_Print ();
retval = FALSE;
} else if (retobj != Py_None) {
retval = PyInt_AsLong(retobj);
}
Py_XDECREF(retobj);
pyg_gil_state_release(state);
return retval;
}
static PyObject *
_wrap_gst_structure_foreach (PyGObject *self,
PyObject *args,
PyObject *kwargs)
{
static char *kwlist[] = { "foreach_function", "args", NULL };
PyObject *pyfunc, *pyarg = NULL;
PyGstCustomNotify cunote;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O|O:GstStructure.foreach",
kwlist,
&pyfunc, &pyarg)) {
return NULL;
}
if (!PyCallable_Check(pyfunc)) {
PyErr_SetString(PyExc_TypeError, "foreach_function not callable");
return NULL;
}
cunote.func = pyfunc;
cunote.data = pyarg;
gst_structure_foreach(pyg_boxed_get(self, GstStructure),
pygst_structure_foreach_marshal,
&cunote);
Py_INCREF(Py_None);
return Py_None;
}
%%
override-slot GstStructure.tp_repr
static PyObject *
_wrap_gst_structure_tp_repr (PyGObject *self)
{
char *buf;
PyObject *retval;
buf = g_strdup_printf("<GstStructure (%s) at %lx>",
gst_structure_get_name((GstStructure*)self->obj),
(long)self->obj);
retval = PyString_FromString(buf);
g_free(buf);
return retval;
}
%%
override gst_structure_from_string kwargs
static PyObject *
_wrap_gst_structure_from_string(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = { "string", NULL };
char *string;
GstStructure *ret;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:structure_from_string", kwlist, &string))
return NULL;
ret = gst_structure_from_string(string, NULL);
/* pyg_boxed_new handles NULL checking */
return pyg_boxed_new(GST_TYPE_STRUCTURE, ret, TRUE, TRUE);
}