commit-gnue
[Top][All Lists]
Advanced

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

[gnue] r8421 - trunk/gnue-common/src/logic


From: reinhard
Subject: [gnue] r8421 - trunk/gnue-common/src/logic
Date: Tue, 18 Apr 2006 18:36:23 -0500 (CDT)

Author: reinhard
Date: 2006-04-18 18:36:23 -0500 (Tue, 18 Apr 2006)
New Revision: 8421

Modified:
   trunk/gnue-common/src/logic/GTriggerCore.py
   trunk/gnue-common/src/logic/NamespaceCore.py
Log:
Moved namespace object creation into GTriggerCore to make this object more
self-contained.


Modified: trunk/gnue-common/src/logic/GTriggerCore.py
===================================================================
--- trunk/gnue-common/src/logic/GTriggerCore.py 2006-04-18 23:25:16 UTC (rev 
8420)
+++ trunk/gnue-common/src/logic/GTriggerCore.py 2006-04-18 23:36:23 UTC (rev 
8421)
@@ -20,10 +20,15 @@
 # - Suite 330, Boston, MA 02111-1307, USA.
 #
 # $Id$
+
 """
 Base Classes to derive from to make use of the action/trigger system.
 """
 
+import types
+
+from gnue.common.apps import errors
+
 __all__ = ['GTriggerCore']
 
 
@@ -78,12 +83,270 @@
         self._triggerFunctions = {}
         self._triggerProperties = {}
 
-        # Dictionary representing this object's
-        # local namespace.  Populated as part of
-        # GTriggerNamespace.constructTriggerObject
-        # FIXME: Belongs into GTriggerExtension?
-        self._localTriggerNamespace = {}
-    
-        # Dict of triggers that are valid for this specific object
-        # FIXME: Belongs into GTriggerExtension?
-        self._validTriggers = {}
+        self.namespace_object = None
+
+
+    # -------------------------------------------------------------------------
+    # Construct a namespace object tree for an XML object tree
+    # -------------------------------------------------------------------------
+
+    def create_namespace_object(self, global_namespace):
+        """
+        Construct a namespace object tree from an XML (L{GObject.GObj}) object
+        tree.
+
+        This function creates a L{NamespaceElement} object for each
+        L{GObject.GObj} in the object.
+        """
+
+        # Do not create a namespace object for xml objects with no "name"
+        # property.
+        if not hasattr(self, 'name') or self.name is None:
+            return None
+
+        # Create dictionary with methods as defined
+        methods = {}
+        for (name, definition) in self._triggerFunctions.items():
+            function = definition.get('function')
+            assert isinstance(function, types.MethodType)
+            methods[name] = function
+            # Add this function to global namespace if the GObj requests it
+            if definition.get('global', False):
+                global_namespace[name] = function
+
+        # Create dictionary with properties as defined
+        properties = {}
+        for (name, definition) in self._triggerProperties.items():
+            get_function = definition.get('get')
+            set_function = definition.get('set')
+            assert isinstance(get_function, types.MethodType)
+            if set_function is not None:
+                assert isinstance(set_function, types.MethodType)
+            properties[name] = (get_function, set_function)
+
+        # Create the namespace object
+        namespace_object = NamespaceElement(
+                xml_object = self,
+                get_method = self._triggerGet,
+                set_method = self._triggerSet,
+                methods    = methods,
+                properties = properties)
+
+        # Process the children of this xml object
+        if len(self._children):
+            self.__add_children(self._children, namespace_object,
+                    global_namespace)
+
+        # Remember the namespace object
+        self.namespace_object = namespace_object
+
+        # Add the namespace object to global namespace if the xml object
+        # requests it
+        if self._triggerGlobal:
+            global_namespace[self.name] = namespace_object
+
+        return namespace_object
+
+
+    # -------------------------------------------------------------------------
+    # Add children to a Namespace object matching the children of the GObj
+    # -------------------------------------------------------------------------
+
+    def __add_children(self, children, namespace_object, global_namespace):
+        """
+        Create child namespace objects according to child XML objects
+        """
+        for child in children:
+            if not isinstance(child, GTriggerCore):
+                continue
+
+            child_object = child.create_namespace_object (global_namespace)
+
+            # Add this objects children to its namespace by their name
+            if child_object:
+                # skip on GRPassThru objects
+                if child.name.startswith('__'):
+                    if len(child._children):
+                        self.__add_children(child._children, namespace_object,
+                                global_namespace)
+                else:
+                    namespace_object.__dict__[child.name] = child_object
+
+
+# =============================================================================
+# Exceptions used by namespace objects
+# =============================================================================
+
+class NoSetValueError(errors.ApplicationError):
+    """
+    Cannot set value of this object.
+    """
+    def __init__(self, name):
+        errors.ApplicationError.__init__(self,
+                u_("Cannot set value of object '%s'") % name)
+
+# -----------------------------------------------------------------------------
+
+class ReadonlyPropertyError(errors.ApplicationError):
+    """
+    Cannot set readonly property.
+    """
+    def __init__(self, name):
+        errors.ApplicationError.__init__(self,
+                u_("Cannot set readonly property '%s'") % name)
+
+
+# =============================================================================
+# Namespace object
+# =============================================================================
+
+class NamespaceElement(object):
+    """
+    Proxy object that represents an object from an XML tree within the
+    action/trigger namespace.
+    """
+
+    # -------------------------------------------------------------------------
+    # Constructor
+    # -------------------------------------------------------------------------
+
+    def __init__(self, xml_object, get_method, set_method, methods,
+            properties):
+        """
+        Initialize a namespace proxy object.
+        """
+
+        checktype (xml_object, GTriggerCore)
+
+        self.__xml_object = xml_object
+        self.__get_method = get_method
+        self.__set_method = set_method
+        self.__methods    = methods
+        self.__properties = properties
+
+        # FIXME: gnue.forms.GFForm.GFForm.triggerSetFocus() still uses this!
+        # Remove here once it is removed in Forms!
+        self._object = xml_object
+
+
+    # -------------------------------------------------------------------------
+    # Nice string representation
+    # -------------------------------------------------------------------------
+
+    def __repr__(self):
+        return self.__xml_object.name
+
+
+    # -------------------------------------------------------------------------
+    # Getting and setting attributes
+    # -------------------------------------------------------------------------
+
+    def __getattr__(self, name):
+
+        # Handle methods and properties. Child objects (which are stored in the
+        # native __dict__ of the object) have highest priority.
+
+        if self.__methods.has_key(name):
+            return self.__methods[name]
+        elif self.__properties.has_key(name):
+            return self.__properties[name][0]()
+
+    # -------------------------------------------------------------------------
+
+    def __setattr__(self, name, value):
+
+        # This is called when trying to write something inside a trigger object
+        # namespace.  It checks to see if the var name is already linked to a
+        # NamespaceElement based object and calls that objects _triggerSet if
+        # it exists.
+        #
+        # Example: form.block1.entry1 = "foo"
+        #
+        # The __setattr__ will execute at the block1 and call the functions
+        # that are part of the entry1 object to set it's value.
+        #
+        # Apart from that, this also handles properties.
+
+        # Directly set for private variables, otherwise we will recurse in
+        # __init__.
+        if name.startswith('_NamespaceElement__'):
+            self.__dict__[name] = value
+            return
+
+        attr = self.__dict__.get(name)
+
+        if isinstance(attr, NamespaceElement):
+            if attr.__set_method is None:
+                raise NoSetValueError, name
+            attr.__set_method(value)
+
+        elif self.__properties.has_key(name):
+            set_function = self.__properties[name][1]
+            if set_function is None:
+                raise ReadonlyPropertyError, name
+            set_function(value)
+
+        else:
+            self.__dict__[name] = value
+
+
+    # -------------------------------------------------------------------------
+    # String and Integer values
+    # -------------------------------------------------------------------------
+
+    def __str__(self):
+
+        # This executes at a different level than __setattr__.
+        # While __setattr__ is executed in the parent object to protect its
+        # namespace object links, this routine is called by the referenced
+        # object.
+        #
+        # Example: foo = str(form.block1.entry1)
+        #
+        # This __str__ would execute for the entry1 object.
+
+        if self.__get_method:
+            return str(self.__get_method())
+        else:
+            return self.__xml_object.name
+
+    # -------------------------------------------------------------------------
+
+    def __int__(self):
+
+        if self.__get_method:
+            return int(self.__get_method())
+        else:
+            return 0
+
+    # -------------------------------------------------------------------------
+
+    def __cmp__(self, other):
+
+        # Forces the system to compare the string values of NamespaceElement
+        # objects rather than their instances
+
+        return cmp(str(self), str(other))
+
+    # -------------------------------------------------------------------------
+
+    def __nonzero__(self):
+        return True
+
+    # -------------------------------------------------------------------------
+
+    def __len__(self):
+        return len(str(self))
+
+    # -------------------------------------------------------------------------
+
+    def __getitem__(self, key) :
+        return str(self)[key.start:key.stop]
+
+
+    # -------------------------------------------------------------------------
+    # Iterator support in case the underlying GObj object supports it
+    # -------------------------------------------------------------------------
+
+    def __iter__(self):
+        return iter(self.__xml_object)

Modified: trunk/gnue-common/src/logic/NamespaceCore.py
===================================================================
--- trunk/gnue-common/src/logic/NamespaceCore.py        2006-04-18 23:25:16 UTC 
(rev 8420)
+++ trunk/gnue-common/src/logic/NamespaceCore.py        2006-04-18 23:36:23 UTC 
(rev 8421)
@@ -28,15 +28,11 @@
 functions and properties that the objects explicitly want to provide.
 """
 
-import types
-
 from gnue.common.definitions.GObjects import GObj
 
-from gnue.common.logic.GTriggerCore import GTriggerCore
+__all__ = ['GObjNamespace']
 
-__all__ = ['GObjNamespace', 'NamespaceElement']
 
-
 # =============================================================================
 # GObjNamespace
 # =============================================================================
@@ -73,242 +69,9 @@
     def constructTriggerObject(self, xml_object):
         """
         Construct a namespace object tree from an XML (L{GObject.GObj}) object
-        tree.
+        tree. DEPRECIATED.
 
-        This function creates a L{NamespaceElement} object for each
-        L{GObject.GObj} in the object.
+        Depreciated. Use xml_object.create_namespace_object instead.
         """
 
-        # Do not create a namespace object for xml objects that are not trigger
-        # enabled.
-        if not isinstance(xml_object, GTriggerCore):
-            return None
-
-        # Do not create a namespace object for xml objects with no "name"
-        # property.
-        if not hasattr(xml_object, 'name') or xml_object.name is None:
-            return None
-
-        # Create dictionary with methods as defined
-        methods = {}
-        for (name, definition) in xml_object._triggerFunctions.items():
-            function = definition.get('function')
-            assert isinstance(function, types.MethodType)
-            methods[name] = function
-            # Add this function to global namespace if the GObj requests it
-            if definition.get('global', False):
-                self._globalNamespace[name] = function
-
-        # Create dictionary with properties as defined
-        properties = {}
-        for (name, definition) in xml_object._triggerProperties.items():
-            get_function = definition.get('get')
-            set_function = definition.get('set')
-            assert isinstance(get_function, types.MethodType)
-            if set_function is not None:
-                assert isinstance(set_function, types.MethodType)
-            properties[name] = (get_function, set_function)
-
-        # Create the namespace object
-        namespace_object = NamespaceElement(
-                xml_object = xml_object,
-                get_method = xml_object._triggerGet,
-                set_method = xml_object._triggerSet,
-                methods    = methods,
-                properties = properties)
-
-        # Process the children of this xml object
-        if len(xml_object._children):
-            self.__add_children(xml_object._children, namespace_object)
-
-        # Add the namespace object to global namespace if the xml object
-        # requests it
-        if xml_object._triggerGlobal:
-            self._globalNamespace[xml_object.name] = namespace_object
-
-        # populate the GObj's _localTriggerNamespace
-        # FIXME: This would only have to be done for all objects that can have
-        # triggers attached, not for all objects that can appear in the trigger
-        # namespace.
-        xml_object._localTriggerNamespace = {'self': namespace_object}
-
-        return namespace_object
-
-
-    # -------------------------------------------------------------------------
-    # Add children to a Namespace object matching the children of the GObj
-    # -------------------------------------------------------------------------
-
-    def __add_children(self, children, namespace_object):
-        """
-        Create child namespace objects according to child XML objects
-        """
-        for child in children:
-            child_object = self.constructTriggerObject(child)
-
-            # Add this objects children to it's namespace by their name
-            if child_object:
-                # skip on GRPassThru objects
-                if child.name.startswith('__'):
-                    if len(child._children):
-                        self.__add_children(child._children, namespace_object)
-                else:
-                    namespace_object.__dict__[child.name] = child_object
-
-
-# =============================================================================
-# Namespace object
-# =============================================================================
-
-class NamespaceElement(object):
-    """
-    Proxy object that represents an object from an XML tree within the
-    action/trigger namespace.
-    """
-
-    # -------------------------------------------------------------------------
-    # Constructor
-    # -------------------------------------------------------------------------
-
-    def __init__(self, xml_object, get_method, set_method, methods,
-            properties):
-        """
-        Initialize a namespace proxy object.
-        """
-
-        checktype (xml_object, GTriggerCore)
-
-        self.__xml_object = xml_object
-        self.__get_method = get_method
-        self.__set_method = set_method
-        self.__methods    = methods
-        self.__properties = properties
-
-        # FIXME: gnue.forms.GFForm.GFForm.triggerSetFocus() still uses this!
-        # Remove here once it is removed in Forms!
-        self._object = xml_object
-
-
-    # -------------------------------------------------------------------------
-    # Nice string representation
-    # -------------------------------------------------------------------------
-
-    def __repr__(self):
-        return self.__xml_object.name
-
-
-    # -------------------------------------------------------------------------
-    # Getting and setting attributes
-    # -------------------------------------------------------------------------
-
-    def __getattr__(self, name):
-
-        # Handle methods and properties. Child objects (which are stored in the
-        # native __dict__ of the object) have highest priority.
-
-        if self.__methods.has_key(name):
-            return self.__methods[name]
-        elif self.__properties.has_key(name):
-            return self.__properties[name][0]()
-
-    # -------------------------------------------------------------------------
-
-    def __setattr__(self, name, value):
-
-        # This is called when trying to write something inside a trigger object
-        # namespace.  It checks to see if the var name is already linked to a
-        # NamespaceElement based object and calls that objects _triggerSet if
-        # it exists.
-        #
-        # Example: form.block1.entry1 = "foo"
-        #
-        # The __setattr__ will execute at the block1 and call the functions
-        # that are part of the entry1 object to set it's value.
-        #
-        # Apart from that, this also handles properties.
-
-        # Directly set for private variables, otherwise we will recurse in
-        # __init__.
-        if name.startswith('_NamespaceElement__'):
-            self.__dict__[name] = value
-            return
-
-        attr = self.__dict__.get(name)
-
-        if isinstance(attr, NamespaceElement):
-            if attr.__set_method is None:
-                # TODO: Make this a good exception
-                raise "Cannot set value of %s" % name
-            attr.__set_method(value)
-
-        elif self.__properties.has_key(name):
-            set_function = self.__properties[name][1]
-            if set_function is None:
-                # TODO: Make this a good exception
-                raise "Cannot set read only property %s" % name
-            set_function(value)
-
-        else:
-            self.__dict__[name] = value
-
-
-    # -------------------------------------------------------------------------
-    # String and Integer values
-    # -------------------------------------------------------------------------
-
-    def __str__(self):
-
-        # This executes at a different level than __setattr__.
-        # While __setattr__ is executed in the parent object to protect its
-        # namespace object links, this routine is called by the referenced
-        # object.
-        #
-        # Example: foo = str(form.block1.entry1)
-        #
-        # This __str__ would execute for the entry1 object.
-
-        if self.__get_method:
-            return str(self.__get_method())
-        else:
-            return self.__xml_object.name
-
-    # -------------------------------------------------------------------------
-
-    def __int__(self):
-
-        if self.__get_method:
-            return int(self.__get_method())
-        else:
-            return 0
-
-    # -------------------------------------------------------------------------
-
-    def __cmp__(self, other):
-
-        # Forces the system to compare the string values of NamespaceElement
-        # objects rather than their instances
-
-        return cmp(str(self), str(other))
-
-    # -------------------------------------------------------------------------
-
-    def __nonzero__(self):
-        return True
-
-    # -------------------------------------------------------------------------
-
-    def __len__(self):
-        return len(str(self))
-
-    # -------------------------------------------------------------------------
-
-    def __getitem__(self, key) :
-        return str(self)[key.start:key.stop]
-
-
-    # -------------------------------------------------------------------------
-    # Iterator support in case the underlying GObj object supports it
-    # -------------------------------------------------------------------------
-
-    def __iter__(self):
-        return iter(self.__xml_object)
+        return xml_object.create_namespace_object(self._globalNamespace)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]