From 7ee9ce29ea424a26fe74ee9a6a0f75aa24059e8f Mon Sep 17 00:00:00 2001 From: Thibault Saunier Date: Wed, 8 Aug 2012 13:59:09 -0400 Subject: [PATCH] Add overrides for GObject Introspection --- Makefile.am | 2 +- configure.ac | 34 +++++++- gi/Makefile.am | 1 + gi/__init__.py | 29 +++++++ gi/overrides/Gst.py | 169 +++++++++++++++++++++++++++++++++++++++ gi/overrides/Makefile.am | 4 + gi/overrides/__init__.py | 4 + 7 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 gi/Makefile.am create mode 100644 gi/__init__.py create mode 100644 gi/overrides/Gst.py create mode 100644 gi/overrides/Makefile.am create mode 100644 gi/overrides/__init__.py diff --git a/Makefile.am b/Makefile.am index df13f68559..f7ee4ae2cb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = common examples testsuite +SUBDIRS = common examples testsuite gi # include before EXTRA_DIST for win32 assignment include $(top_srcdir)/common/win32.mak diff --git a/configure.ac b/configure.ac index 25bcb6f4fd..9dda9a7873 100644 --- a/configure.ac +++ b/configure.ac @@ -7,6 +7,8 @@ AC_INIT(GStreamer GObject Introspectin tests for Python , 0.11.92, http://bugzilla.gnome.org/enter_bug.cgi?product=GStreamer, gst-python) +AG_GST_INIT + dnl initialize automake AM_INIT_AUTOMAKE([-Wno-portability 1.10]) @@ -36,10 +38,9 @@ AC_SUBST(ACLOCAL_AMFLAGS, "-I m4 -I common/m4") dnl required versions of other packages dnl Note that they are runtime requirements AC_SUBST(GST_REQ, 0.11.92) -AC_SUBST(GSTPB_REQ, 0.11.92) dnl check for python -dnl AM_PATH_PYTHON(2.2) +dnl AM_PATH_PYTHON(2.5) AM_PATH_PYTHON AC_MSG_CHECKING(for python >= 2.5) prog=" @@ -55,10 +56,37 @@ then else AC_MSG_ERROR(too old) fi +AS_AC_EXPAND(PYTHONDIR, $pythondir) +AS_AC_EXPAND(PYEXECDIR, $pyexecdir) + +AC_ARG_WITH([pygi_overrides_dir], + AC_HELP_STRING([--with-pygi-overrides-dir], [Path to pygobject overrides directory])) + +AC_MSG_CHECKING(for pygobject overrides directory) +if test "x$with_pygi_overrides_dir" = "x" ; then + overrides_dir="`$PYTHON -c 'import gi; print(gi._overridesdir)' 2>/dev/null`" + # fallback if the previous failed + if test "x$overrides_dir" = "x" ; then + overrides_dir="${pyexecdir}/gi/overrides" + fi +else + overrides_dir="$with_pygi_overrides_dir" +fi + +PYGI_OVERRIDES_DIR="$overrides_dir" +AC_SUBST(PYGI_OVERRIDES_DIR) +AC_MSG_RESULT($PYGI_OVERRIDES_DIR) + +dnl and set the override directory +AC_ARG_WITH([pygi_overrides_dir], + AC_HELP_STRING([--with-pygi-overrides-dir], [Path to pygobject overrides directory])) AG_GST_VALGRIND_CHECK dnl set release date/time +#AG_GST_SET_PACKAGE_RELEASE_DATETIME_WITH_NANO([$PACKAGE_VERSION_NANO], + #["${srcdir}/gst-python.doap"], + #[$PACKAGE_VERSION_MAJOR.$PACKAGE_VERSION_MINOR.$PACKAGE_VERSION_MICRO]) AC_OUTPUT([ Makefile @@ -66,4 +94,6 @@ AC_OUTPUT([ common/m4/Makefile examples/Makefile testsuite/Makefile + gi/Makefile + gi/overrides/Makefile ]) diff --git a/gi/Makefile.am b/gi/Makefile.am new file mode 100644 index 0000000000..eeba093438 --- /dev/null +++ b/gi/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = overrides diff --git a/gi/__init__.py b/gi/__init__.py new file mode 100644 index 0000000000..09d180b676 --- /dev/null +++ b/gi/__init__.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# +# __init__.py +# +# Copyright (C) 2012 Thibault Saunier +# +# This program 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 program 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 program; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. + + +from pkgutil import extend_path + +__path__ = extend_path(__path__, __name__) diff --git a/gi/overrides/Gst.py b/gi/overrides/Gst.py new file mode 100644 index 0000000000..f184bbaa0b --- /dev/null +++ b/gi/overrides/Gst.py @@ -0,0 +1,169 @@ +# -*- Mode: Python; py-indent-offset: 4 -*- +# vim: tabstop=4 shiftwidth=4 expandtab +# +# Gst.py +# +# Copyright (C) 2012 Thibault Saunier +# +# This program 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 program 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 program; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. + +import sys +from ..overrides import override +from ..importer import modules + +if sys.version_info >= (3, 0): + _basestring = str + _callable = lambda c: hasattr(c, '__call__') +else: + _basestring = basestring + _callable = callable + +Gst = modules['Gst']._introspection_module +__all__ = [] + +if Gst._version == '0.10': + import warnings + warn_msg = "You have imported the Gst 0.10 module. Because Gst 0.10 \ +was not designed for use with introspection some of the \ +interfaces and API will fail. As such this is not supported \ +by the GStreamer development team and we encourage you to \ +port your app to Gst 1 or greater. gst-python is the recomended \ +python module to use with Gst 0.10" + + warnings.warn(warn_msg, RuntimeWarning) + +class Caps(Gst.Caps): + + def __new__(cls, *kwargs): + if not kwargs: + return Caps.new_empty() + elif len(kwargs) > 1: + raise TypeError("wrong arguments when creating GstCaps object") + elif isinstance(kwargs[0], str): + return Caps.from_string(kwargs[0]) + elif isinstance(kwargs[0], Caps): + return kwargs[0].copy() + + raise TypeError("wrong arguments when creating GstCaps object") + + def __str__(self): + return self.to_string() + +Caps = override(Caps) +__all__.append('Caps') + +class ElementFactory(Gst.ElementFactory): + + # ElementFactory + def get_longname(self): + return self.get_metadata("long-name") + + def get_description(self): + return self.get_metadata("description") + + def get_klass(self): + return self.get_metadata("klass") + +ElementFactory = override(ElementFactory) +__all__.append('ElementFactory') + +class Fraction(Gst.Fraction): + def __init__(self, num, denom=1): + def __gcd(a, b): + while b != 0: + tmp = a + a = b + b = tmp % b + return abs(a) + + def __simplify(): + num = self.num + denom = self.denom + + if num < 0: + num = -num + denom = -denom + + # Compute greatest common divisor + gcd = __gcd(num, denom) + if gcd != 0: + num /= gcd + denom /= gcd + + self.num = num + self.denom = denom + + self.num = num + self.denom = denom + + __simplify() + self.type = "fraction" + + def __repr__(self): + return '' % (self.num, self.denom) + + def __value__(self): + return self.num / self.denom + + def __eq__(self, other): + if isinstance(other, Fraction): + return self.num * other.denom == other.num * self.denom + return False + + def __ne__(self, other): + return not self.__eq__(other) + + def __mul__(self, other): + if isinstance(other, Fraction): + return Fraction(self.num * other.num, + self.denom * other.denom) + elif isinstance(other, int): + return Fraction(self.num * other, self.denom) + raise TypeError + + __rmul__ = __mul__ + + def __div__(self, other): + if isinstance(other, Fraction): + return Fraction(self.num * other.denom, + self.denom * other.num) + elif isinstance(other, int): + return Fraction(self.num, self.denom * other) + return TypeError + + def __rdiv__(self, other): + if isinstance(other, int): + return Fraction(self.denom * other, self.num) + return TypeError + + def __float__(self): + return float(self.num) / float(self.denom) + +Fraction = override(Fraction) +__all__.append('Fraction') + +initialized, argv = Gst.init_check(sys.argv) +import _gi_gst +print _gi_gst + +sys.argv = list(argv) +if not initialized: + raise RuntimeError("Gst couldn't be initialized") + diff --git a/gi/overrides/Makefile.am b/gi/overrides/Makefile.am new file mode 100644 index 0000000000..b088883afd --- /dev/null +++ b/gi/overrides/Makefile.am @@ -0,0 +1,4 @@ + +# We install everything in the gi/overrides folder +pygioverridesdir = $(PYGI_OVERRIDES_DIR) +pygioverrides_PYTHON = Gst.py diff --git a/gi/overrides/__init__.py b/gi/overrides/__init__.py new file mode 100644 index 0000000000..802630a09d --- /dev/null +++ b/gi/overrides/__init__.py @@ -0,0 +1,4 @@ +from pkgutil import extend_path + +__path__ = extend_path(__path__, __name__) +print __path__, __name__