move over some of the extend stuff that I want to use right now

Original commit message from CVS:

* configure.ac:
* gst/Makefile.am:
* gst/extend/Makefile.am:
* gst/extend/__init__.py:
* gst/extend/pygobject.py:
* gst/extend/utils.py:
move over some of the extend stuff that I want to use right now
This commit is contained in:
Thomas Vander Stichele 2005-10-04 14:59:07 +00:00
parent 3241bda36a
commit 74782ee345
7 changed files with 285 additions and 0 deletions

View file

@ -1,3 +1,13 @@
2005-10-04 Thomas Vander Stichele <thomas at apestaart dot org>
* configure.ac:
* gst/Makefile.am:
* gst/extend/Makefile.am:
* gst/extend/__init__.py:
* gst/extend/pygobject.py:
* gst/extend/utils.py:
move over some of the extend stuff that I want to use right now
2005-10-04 Edward Hervey <edward@fluendo.com>
* codegen/codegen.py:

View file

@ -146,6 +146,7 @@ AC_OUTPUT([
Makefile
codegen/Makefile
gst/Makefile
gst/extend/Makefile
examples/Makefile
pkgconfig/Makefile
pkgconfig/gst-python.pc

View file

@ -77,3 +77,5 @@ interfaces.c: $(INTERFACES_DEFS) $(INTERFACES_OVERRIDES) $(GEN_FILES)
--prefix py$* $*.defs) > gen-$*.c \
&& cp gen-$*.c $*.c \
&& rm -f gen-$*.c
SUBDIRS = extend

4
gst/extend/Makefile.am Normal file
View file

@ -0,0 +1,4 @@
pkgpythondir = $(pythondir)/gst/extend
pygstdir = $(pkgpythondir)
pygst_PYTHON = __init__.py pygobject.py utils.py

21
gst/extend/__init__.py Normal file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env python
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# GStreamer python bindings
# Copyright (C) 2002 David I. Lehn <dlehn@users.sourceforge.net>
# 2004 Johan Dahlin <johan@gnome.org>
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

172
gst/extend/pygobject.py Normal file
View file

@ -0,0 +1,172 @@
#!/usr/bin/env python
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# GStreamer python bindings
# Copyright (C) 2004 Johan Dahlin <johan at gnome dot org>
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
PyGTK helper functions
"""
import sys
import gobject
def gobject_set_property(object, property, value):
"""
Set the given property to the given value on the given object.
@type object: L{gobject.GObject}
@type property: string
@param value: value to set property to
"""
for pspec in gobject.list_properties(object):
if pspec.name == property:
break
else:
raise errors.PropertyError(
"Property '%s' in element '%s' does not exist" % (
property, object.get_property('name')))
if pspec.value_type in (gobject.TYPE_INT, gobject.TYPE_UINT,
gobject.TYPE_INT64, gobject.TYPE_UINT64):
try:
value = int(value)
except ValueError:
msg = "Invalid value given for property '%s' in element '%s'" % (
property, object.get_property('name'))
raise errors.PropertyError(msg)
elif pspec.value_type == gobject.TYPE_BOOLEAN:
if value == 'False':
value = False
elif value == 'True':
value = True
else:
value = bool(value)
elif pspec.value_type in (gobject.TYPE_DOUBLE, gobject.TYPE_FLOAT):
value = float(value)
elif pspec.value_type == gobject.TYPE_STRING:
value = str(value)
# FIXME: this is superevil ! we really need to find a better way
# of checking if this property is a param enum
# also, we only allow int for now
elif repr(pspec.__gtype__).startswith("<GType GParamEnum"):
value = int(value)
else:
raise errors.PropertyError('Unknown property type: %s' %
pspec.value_type)
object.set_property(property, value)
def gsignal(name, *args):
"""
Add a GObject signal to the current object.
To be used from class definition scope.
@type name: string
@type args: mixed
"""
frame = sys._getframe(1)
_locals = frame.f_locals
if not '__gsignals__' in _locals:
_dict = _locals['__gsignals__'] = {}
else:
_dict = _locals['__gsignals__']
_dict[name] = (gobject.SIGNAL_RUN_FIRST, None, args)
PARAM_CONSTRUCT = 1<<9
def with_construct_properties(__init__):
"""
Wrap a class' __init__ method in a procedure that will construct
gobject properties. This is necessary because pygtk's object
construction is a bit broken.
Usage::
class Foo(GObject):
def __init__(self):
GObject.__init(self)
__init__ = with_construct_properties(__init__)
"""
frame = sys._getframe(1)
_locals = frame.f_locals
gproperties = _locals['__gproperties__']
def hacked_init(self, *args, **kwargs):
__init__(self, *args, **kwargs)
self.__gproperty_values = {}
for p, v in gproperties.items():
if v[-1] & PARAM_CONSTRUCT:
self.set_property(p, v[3])
return hacked_init
def gproperty(type_, name, desc, *args, **kwargs):
"""
Add a GObject property to the current object.
To be used from class definition scope.
@type type_: type object
@type name: string
@type desc: string
@type args: mixed
"""
frame = sys._getframe(1)
_locals = frame.f_locals
flags = 0
def _do_get_property(self, prop):
try:
return self._gproperty_values[prop.name]
except (AttributeError, KeyError):
raise AttributeError('Property was never set', self, prop)
def _do_set_property(self, prop, value):
if not getattr(self, '_gproperty_values', None):
self._gproperty_values = {}
self._gproperty_values[prop.name] = value
_locals['do_get_property'] = _do_get_property
_locals['do_set_property'] = _do_set_property
if not '__gproperties__' in _locals:
_dict = _locals['__gproperties__'] = {}
else:
_dict = _locals['__gproperties__']
for i in 'readable', 'writable':
if not i in kwargs:
kwargs[i] = True
for k, v in kwargs.items():
if k == 'construct':
flags |= PARAM_CONSTRUCT
elif k == 'construct_only':
flags |= gobject.PARAM_CONSTRUCT_ONLY
elif k == 'readable':
flags |= gobject.PARAM_READABLE
elif k == 'writable':
flags |= gobject.PARAM_WRITABLE
elif k == 'lax_validation':
flags |= gobject.PARAM_LAX_VALIDATION
else:
raise Exception('Invalid GObject property flag: %r=%r' % (k, v))
_dict[name] = (type_, name, desc) + args + tuple((flags,))

75
gst/extend/utils.py Normal file
View file

@ -0,0 +1,75 @@
import os
import gobject
import gst
def gst_dump(bin):
dump_element (bin, 0)
def dump_bin (bin, indent):
# Iterate the children
for child in bin.get_list():
dump_element (child, indent + 2)
def dump_element (element, indent):
states = { 1: 'NULL', 2: 'READY',
4: 'PAUSED', 8: 'PLAYING' }
state = 'UNKNOWN'
try:
state = states[element.get_state()]
except KeyError:
state = 'UNKNOWN (%d)' % element.get_state()
c = element.get_clock()
if c is None:
clock_str = "clock - None"
else:
clock_str = "clock - %s" % (c.get_name())
out = "%s (%s): state %s, %s" % (element.get_name(),
gobject.type_name (element.__gtype__), state, clock_str)
print out.rjust(len(out) + indent)
tmp = { True: 'active', False: 'inactive' }
for curpad in element.get_pad_list():
if curpad.get_direction() == gst.PAD_SRC:
if curpad.is_linked():
peer = curpad.get_peer()
out = " - %s:%s (%s) => %s:%s (%s)" % (
curpad.get_parent().get_name(), curpad.get_name(),
tmp[curpad.is_active()],
peer.get_parent().get_name(), peer.get_name(),
tmp[peer.is_active()])
print out.rjust(len(out) + indent)
if isinstance (element, gst.Bin):
dump_bin (element, indent + 2)
elif isinstance (element, gst.Queue):
out = " - time_level: %ld" % (element.get_property('current-level-time'))
print out.rjust(len(out) + indent)
out = " - bytes_level: %ld" % (element.get_property('current-level-bytes'))
print out.rjust(len(out) + indent)
def gc_collect(reason=None):
"""
Garbage-collect if GST_GC env var is set.
This helps in debugging object refcounting.
Sprinkle liberally around checkpoints.
"""
env = os.environ.get('GST_GC', None)
if not env:
return
import gc
if env == 'DEBUG_LEAK':
gc.set_debug(gc.DEBUG_LEAK)
gst.debug('collecting garbage')
if reason:
gst.debug('because of %s' % reason)
count = gc.collect()
gst.debug('collected garbage, %d objects collected, %d left' % (
count, len(gc.get_objects())))