[pygobject] Add GTypeClass methods as Python GObject class methods



commit bf84915f89fd5fd502b4fb162eef7bc0a48c8783
Author: Johan Dahlin <johan gnome org>
Date:   Mon Oct 1 06:42:24 2012 -0700

    Add GTypeClass methods as Python GObject class methods
    
    Take all the methods from an objects type classs and add them
    as class methods. For instance, GObject.ObjectClass.list_properties
    is available as GObject.Object.list_properties().
    
    Co-Authored-By: Simon Feltman <sfeltman src gnome org>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=685218

 gi/types.py             |   13 +++++++++++++
 tests/test_typeclass.py |   13 +++++++++++++
 2 files changed, 26 insertions(+), 0 deletions(-)
---
diff --git a/gi/types.py b/gi/types.py
index 6007e53..aa5bcb4 100644
--- a/gi/types.py
+++ b/gi/types.py
@@ -55,6 +55,17 @@ class MetaClassHelper(object):
         for method_info in cls.__info__.get_methods():
             setattr(cls, method_info.__name__, method_info)
 
+    def _setup_class_methods(cls):
+        info = cls.__info__
+        class_struct = info.get_class_struct()
+        if class_struct is None:
+            return
+        for method_info in class_struct.get_methods():
+            name = method_info.__name__
+            # Don't mask regular methods or base class methods with TypeClass methods.
+            if not hasattr(cls, name):
+                setattr(cls, name, classmethod(method_info))
+
     def _setup_fields(cls):
         for field_info in cls.__info__.get_fields():
             name = field_info.get_name().replace('-', '_')
@@ -211,6 +222,8 @@ class GObjectMeta(_GObjectMetaBase, MetaClassHelper):
         if is_python_defined:
             cls._setup_vfuncs()
         elif is_gi_defined:
+            if isinstance(cls.__info__, ObjectInfo):
+                cls._setup_class_methods()
             cls._setup_methods()
             cls._setup_constants()
             cls._setup_native_vfuncs()
diff --git a/tests/test_typeclass.py b/tests/test_typeclass.py
index 300fe81..3ece684 100644
--- a/tests/test_typeclass.py
+++ b/tests/test_typeclass.py
@@ -63,5 +63,18 @@ class TestCoercion(unittest.TestCase):
             GObject.ObjectClass.find_property(42, 'some-int')
 
 
+class TestTypeClassMethodsMovedToClass(unittest.TestCase):
+    def test_list_child_properties(self):
+        pspecs = GIMarshallingTests.PropertiesObject.list_properties()
+        pnames = [pspec.name for pspec in pspecs]
+        self.assertTrue('some-int' in pnames)
+        self.assertTrue('some-float' in pnames)
+        self.assertTrue('some-char' in pnames)
+
+    def test_find_child_property(self):
+        pspec = GIMarshallingTests.PropertiesObject.find_property('some-int')
+        self.assertEqual(pspec.name, 'some-int')
+
+
 if __name__ == '__main__':
     unittest.main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]