[pygobject] add DynamicGLibModule which works like DynamicGObjectModule



commit 191ef79315f8a5641699536fde58da18e23ef904
Author: John (J5) Palmieri <johnp redhat com>
Date:   Fri Jul 22 14:11:53 2011 -0400

    add DynamicGLibModule which works like DynamicGObjectModule
    
    https://bugzilla.gnome.org/show_bug.cgi?id=642048

 gi/importer.py |    7 +++++--
 gi/module.py   |   32 +++++++++++++++++++++++++++++---
 2 files changed, 34 insertions(+), 5 deletions(-)
---
diff --git a/gi/importer.py b/gi/importer.py
index 180d1c3..75ba177 100644
--- a/gi/importer.py
+++ b/gi/importer.py
@@ -25,7 +25,7 @@ import logging
 import sys
 
 from ._gi import Repository, RepositoryError
-from .module import DynamicModule, DynamicGObjectModule
+from .module import DynamicModule, DynamicGObjectModule, DynamicGLibModule
 
 
 repository = Repository.get_default()
@@ -59,10 +59,13 @@ class DynamicImporter(object):
 
         path, namespace = fullname.rsplit('.', 1)
 
-        # Workaround for GObject
+        # Workaround for GObject and GLib
         if namespace == 'GObject':
             sys.modules[fullname] = DynamicGObjectModule()
             return sys.modules[fullname]
+        elif namespace == "GLib":
+            sys.modules[fullname] = DynamicGLibModule()
+            return sys.modules[fullname]
 
         dynamic_module = DynamicModule(namespace)
         modules[namespace] = dynamic_module
diff --git a/gi/module.py b/gi/module.py
index 4493bd0..b5629a1 100644
--- a/gi/module.py
+++ b/gi/module.py
@@ -24,6 +24,7 @@ from __future__ import absolute_import
 
 import os
 from . import _gobject
+from . import _glib
 
 try:
     maketrans = ''.maketrans
@@ -210,11 +211,37 @@ class IntrospectionModule(object):
 
         return list(result)
 
+class DynamicGLibModule(IntrospectionModule):
+    """Wrapper for the internal GLib module
+
+    This class allows us to access both the static internal PyGLib module and the GI GLib module
+    through the same interface.  It is returned when by importing GLib from the gi repository:
+
+    from gi.repository import GLib
+
+    We use this because some PyGI interfaces generated from the GIR require GLib types not wrapped
+    by the static bindings.  This also allows access to module attributes in a way that is more
+    familiar to GI application developers.
+    """
+
+    def __init__(self):
+        IntrospectionModule.__init__(self, namespace='GLib')
+
+    def __getattr__(self, name):
+        # first see if this attr is in the internal _gobject module
+        attr = getattr(_glib, name, None)
+
+        # if not in module assume request for an attr exported through GI
+        if attr is None:
+            attr = super(DynamicGLibModule, self).__getattr__(name)
+
+        return attr
+
 
 class DynamicGObjectModule(IntrospectionModule):
-    """Wrapper for the GObject module
+    """Wrapper for the internal GObject module
 
-    This class allows us to access both the static PyGObject module and the GI GObject module
+    This class allows us to access both the static internal PyGObject module and the GI GObject module
     through the same interface.  It is returned when by importing GObject from the gi repository:
 
     from gi.repository import GObject
@@ -240,7 +267,6 @@ class DynamicGObjectModule(IntrospectionModule):
 
         return attr
 
-
 class DynamicModule(object):
     def __init__(self, namespace):
         self._namespace = namespace



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