? WIP-something.diff
? foo.diff
? pygobject-2.10.0-py2.4-linux-i686.egg
? pygobject-2.10.0.tar.gz
? pygobject-2.10.1-py2.4-linux-i686.egg
? pygobject-2.10.1.tar.gz
? pygobject-2.11.0.tar.gz
? pygobject-2.8.0.tar.gz
? pygobject-2.9.0.tar.gz
? pygobject-2.9.2.tar.gz
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gnome-python/pygobject/ChangeLog,v
retrieving revision 1.28
diff -u -d -p -r1.28 ChangeLog
--- ChangeLog	11 Apr 2006 15:14:35 -0000	1.28
+++ ChangeLog	11 Apr 2006 16:05:20 -0000
@@ -1,5 +1,11 @@
 2006-04-11  Johan Dahlin  <jdahlin@async.com.br>
 
+	* gobject/utils.py: 
+	Add simplified wrappers for creating
+	signals and properties.
+
+2006-04-11  Johan Dahlin  <jdahlin@async.com.br>
+
 	* gobject/.cvsignore:
 	* gobject/Makefile.am:
 	* gobject/__init__.py:
Index: examples/properties.py
===================================================================
RCS file: /cvs/gnome/gnome-python/pygobject/examples/properties.py,v
retrieving revision 1.1
diff -u -d -p -r1.1 properties.py
--- examples/properties.py	18 Dec 2001 08:19:51 -0000	1.1
+++ examples/properties.py	11 Apr 2006 16:05:20 -0000
@@ -1,16 +1,14 @@
 import gobject
+from gobject.utils import gproperty
 
 class MyObject(gobject.GObject):
-    __gproperties__ = {
-        'foo': (gobject.TYPE_STRING, 'foo property', 'the foo of the object',
-                'bar', gobject.PARAM_READWRITE),
-        'boolprop': (gobject.TYPE_BOOLEAN, 'bool prop', 'a test boolean prop',
-                     0, gobject.PARAM_READABLE),
-    }
+    gproperty('foo', str, default='bar')
+    gproperty('boolprop', bool, default=False)
 
     def __init__(self):
-        self.__gobject_init__()
+        gobject.GObject.__init__(self)
         self.foo = 'bar'
+
     def do_set_property(self, pspec, value):
         print '    do_set_property called for %s=%r' % (pspec.name, value)
         if pspec.name == 'foo':
Index: examples/signal.py
===================================================================
RCS file: /cvs/gnome/gnome-python/pygobject/examples/signal.py,v
retrieving revision 1.6
diff -u -d -p -r1.6 signal.py
--- examples/signal.py	11 Apr 2006 14:11:09 -0000	1.6
+++ examples/signal.py	11 Apr 2006 16:05:20 -0000
@@ -1,13 +1,9 @@
-import pygtk; pygtk.require("2.0")
 import gobject
+from gobject.utils import gsignal
 
 class C(gobject.GObject):
-    __gsignals__ = {
-        'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
-                      (gobject.TYPE_INT,))
-    }
-    def __init__(self):
-        self.__gobject_init__() # default constructor using our new GType
+    gsignal('my-signal', int)
+
     def do_my_signal(self, arg):
         print "C: class closure for `my_signal' called with argument", arg
 
Index: gobject/Makefile.am
===================================================================
RCS file: /cvs/gnome/gnome-python/pygobject/gobject/Makefile.am,v
retrieving revision 1.7
diff -u -d -p -r1.7 Makefile.am
--- gobject/Makefile.am	11 Apr 2006 15:14:36 -0000	1.7
+++ gobject/Makefile.am	11 Apr 2006 16:05:20 -0000
@@ -15,7 +15,7 @@ endif
 
 # gobject python scripts
 pygobjectdir = $(pkgpythondir)/gtk
-pygobject_PYTHON = __init__.py
+pygobject_PYTHON = __init__.py utils.py
 
 _gobject_la_CFLAGS = $(GLIB_CFLAGS)
 _gobject_la_LDFLAGS = $(common_ldflags) -export-symbols-regex init_gobject
Index: gobject/utils.py
===================================================================
RCS file: gobject/utils.py
diff -N gobject/utils.py
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gobject/utils.py	11 Apr 2006 16:05:20 -0000
@@ -0,0 +1,164 @@
+# -*- Mode: Python; py-indent-offset: 4 -*-
+# pygtk - Python bindings for the GTK toolkit.
+# Copyright (C) 1998-2003  James Henstridge
+#
+#   gobject/utils.py: initialisation file for gtk package.
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA
+#
+# Author(s): Johan Dahlin <jdahlin@async.com.br>
+#
+
+"""GObject utilities and addons"""
+
+import struct
+import sys
+
+import gobject
+
+def gsignal(name, *args, **kwargs):
+    """
+    Add a GObject signal to the current object.
+    It current supports the following types:
+      - str, int, float, long, object, enum
+    @param name:     name of the signal
+    @type name:      string
+    @param args:     types for signal parameters,
+      if the first one is a string 'override', the signal will be
+      overridden and must therefor exists in the parent GObject.
+    @keyword flags: One of the following:
+      - gobject.SIGNAL_RUN_FIRST
+      - gobject.SIGNAL_RUN_LAST
+      - gobject.SIGNAL_RUN_CLEANUP
+      - gobject.SIGNAL_NO_RECURSE
+      - gobject.SIGNAL_DETAILED
+      - gobject.SIGNAL_ACTION
+      - gobject.SIGNAL_NO_HOOKS
+    @keyword retval: return value in signal callback
+    """
+
+    frame = sys._getframe(1)
+    try:
+        locals = frame.f_locals
+    finally:
+        del frame
+
+    dict = locals.setdefault('__gsignals__', {})
+
+    if args and args[0] == 'override':
+        dict[name] = 'override'
+    else:
+        retval = kwargs.get('retval', None)
+        if retval is None:
+            default_flags = gobject.SIGNAL_RUN_FIRST
+        else:
+            default_flags = gobject.SIGNAL_RUN_LAST
+
+        flags = kwargs.get('flags', default_flags)
+        if retval is not None and flags != gobject.SIGNAL_RUN_LAST:
+            raise TypeError(
+                "You cannot use a return value without setting flags to "
+                "gobject.SIGNAL_RUN_LAST")
+
+        dict[name] = (flags, retval, args)
+
+def _max(c):
+    # Python 2.3 does not like bitshifting here
+    return 2 ** ((8 * struct.calcsize(c)) - 1) - 1
+
+_MAX_VALUES = {int : _max('i'),
+               float : _max('f'),
+               long : _max('l') }
+_DEFAULT_VALUES = {str : '', float : 0.0, int : 0, long : 0L}
+
+def gproperty(name, ptype, default=None, nick='', blurb='',
+              flags=gobject.PARAM_READWRITE, **kwargs):
+    """
+    Add a GObject property to the current object.
+    @param name:   name of property
+    @type name:    string
+    @param ptype:   type of property
+    @type ptype:    type
+    @param default:  default value
+    @param nick:     short description
+    @param blurb:    long description
+    @param flags:    parameter flags, one of:
+      - PARAM_READABLE
+      - PARAM_READWRITE
+      - PARAM_WRITABLE
+      - PARAM_CONSTRUCT
+      - PARAM_CONSTRUCT_ONLY
+      - PARAM_LAX_VALIDATION
+    Optional, only for int, float, long types:
+    @keyword minimum:  minimum allowed value
+    @keyword maximum:  maximum allowed value
+    """
+
+    # General type checking
+    if default is None:
+        default = _DEFAULT_VALUES.get(ptype)
+    elif not isinstance(default, ptype):
+        raise TypeError("default must be of type %s, not %r" % (
+            ptype, default))
+    if not isinstance(nick, str):
+        raise TypeError('nick for property %s must be a string, not %r' % (
+            name, nick))
+    nick = nick or name
+    if not isinstance(blurb, str):
+        raise TypeError('blurb for property %s must be a string, not %r' % (
+            name, blurb))
+
+    # Specific type checking
+    if ptype == int or ptype == float or ptype == long:
+        default = (kwargs.get('minimum', ptype(0)),
+                   kwargs.get('maximum', _MAX_VALUES[ptype]),
+                   default)
+    elif ptype == bool:
+        if (default is not True and
+            default is not False):
+            raise TypeError("default must be True or False, not %r" % (
+                default,))
+        default = default,
+    elif gobject.type_is_a(ptype, gobject.GEnum):
+        if default is None:
+            raise TypeError("enum properties needs a default value")
+        elif not isinstance(default, ptype):
+            raise TypeError("enum value %s must be an instance of %r" %
+                            (default, ptype))
+        default = default,
+    elif ptype == str:
+        default = default,
+    elif ptype == object:
+        if default is not None:
+            raise TypeError("object types does not have default values")
+        default = ()
+    else:
+        raise NotImplementedError("type %r" % (ptype,))
+
+    if flags not in (gobject.PARAM_READABLE, gobject.PARAM_READWRITE,
+                     gobject.PARAM_WRITABLE, gobject.PARAM_CONSTRUCT,
+                     gobject.PARAM_CONSTRUCT_ONLY,
+                     gobject.PARAM_LAX_VALIDATION):
+        raise TypeError("invalid flag value: %r" % (flags,))
+
+    frame = sys._getframe(1)
+    try:
+        locals = frame.f_locals
+        dict = locals.setdefault('__gproperties__', {})
+    finally:
+        del frame
+
+    dict[name] = (ptype, nick, blurb) + default + (flags,)
