[CMF-checkins] CVS: CMF/CMFSetup - utils.py:1.9 workflow.py:1.9

Tres Seaver tseaver at zope.com
Tue Jun 8 20:37:18 EDT 2004


Update of /cvs-repository/CMF/CMFSetup
In directory cvs.zope.org:/tmp/cvs-serv24427

Modified Files:
	utils.py workflow.py 
Log Message:


  - utils.py:

    o Add '_query*' form of node attribute lookup.

  - workflow.py:

    o Ensure we use encoding when passed.

    o Add parsing for dc-workflow transitions.


=== CMF/CMFSetup/utils.py 1.8 => 1.9 ===
--- CMF/CMFSetup/utils.py:1.8	Tue Jun  8 19:56:03 2004
+++ CMF/CMFSetup/utils.py	Tue Jun  8 20:36:47 2004
@@ -110,14 +110,34 @@
 #
 #   DOM parsing utilities
 #
-def _getNodeAttribute( node, attr_name, encoding=None ):
+_marker = object()
+
+def _queryNodeAttribute( node, attr_name, default, encoding=None ):
 
     """ Extract a string-valued attribute from node.
+
+    o Return 'default' if the attribute is not present.
     """
-    value = node.attributes[ attr_name ].nodeValue
+    attr_node = node.attributes.get( attr_name, _marker )
+
+    if attr_node is _marker:
+        return default
+
+    value = attr_node.nodeValue
 
     if encoding is not None:
         value = value.encode( encoding )
+
+    return value
+
+def _getNodeAttribute( node, attr_name, encoding=None ):
+
+    """ Extract a string-valued attribute from node.
+    """
+    value = _queryNodeAttribute( node, attr_name, _marker, encoding )
+
+    if value is _marker:
+        raise ValueError, 'Invaid attribute: %s' % attr_name
 
     return value
 


=== CMF/CMFSetup/workflow.py 1.8 => 1.9 ===
--- CMF/CMFSetup/workflow.py:1.8	Tue Jun  8 19:56:03 2004
+++ CMF/CMFSetup/workflow.py	Tue Jun  8 20:36:47 2004
@@ -17,6 +17,7 @@
 from utils import HandlerBase
 from utils import _xmldir
 from utils import _getNodeAttribute
+from utils import _queryNodeAttribute
 from utils import _getNodeAttributeBoolean
 from utils import _coalesceTextNodeChildren
 
@@ -225,7 +226,7 @@
         initial_state = _getNodeAttribute( root, 'initial_state', encoding )
 
         states = _extractStateNodes( root )
-        transitions = []
+        transitions = _extractTransitionNodes( root )
         variables = []
         worklists = []
         permissions = []
@@ -684,7 +685,8 @@
                , 'description' : _coalesceTextNodeChildren( s_node, encoding )
                }
 
-        info[ 'transitions' ] = [ _getNodeAttribute( x, 'transition_id' )
+        info[ 'transitions' ] = [ _getNodeAttribute( x, 'transition_id'
+                                                   , encoding )
                                   for x in s_node.getElementsByTagName(
                                                         'exit-transition' ) ]
 
@@ -695,7 +697,7 @@
             name = _getNodeAttribute( p_map, 'name', encoding )
             acquired = _getNodeAttributeBoolean( p_map, 'acquired' )
 
-            roles = [ _coalesceTextNodeChildren( x )
+            roles = [ _coalesceTextNodeChildren( x, encoding )
                         for x in p_map.getElementsByTagName(
                                             'permission-role' ) ]
 
@@ -710,7 +712,7 @@
 
             name = _getNodeAttribute( g_map, 'name', encoding )
 
-            roles = [ _coalesceTextNodeChildren( x )
+            roles = [ _coalesceTextNodeChildren( x, encoding )
                         for x in g_map.getElementsByTagName(
                                             'group-role' ) ]
 
@@ -720,10 +722,85 @@
 
         for assignment in s_node.getElementsByTagName( 'assignment' ):
 
-            name = _getNodeAttribute( assignment, 'name' )
-            value = _coalesceTextNodeChildren( assignment )
+            name = _getNodeAttribute( assignment, 'name', encoding )
+            value = _coalesceTextNodeChildren( assignment, encoding )
             var_map[ name ] = value # XXX: type lost, only know strings???
 
         result.append( info )
 
     return result
+
+def _extractTransitionNodes( root, encoding=None ):
+
+    result = []
+
+    for t_node in root.getElementsByTagName( 'transition' ):
+
+        info = { 'transition_id' : _getNodeAttribute( t_node, 'transition_id'
+                                                    , encoding )
+               , 'title' : _getNodeAttribute( t_node, 'title', encoding )
+               , 'description' : _coalesceTextNodeChildren( t_node, encoding )
+               , 'new_state' : _getNodeAttribute( t_node, 'new_state'
+                                                , encoding )
+               , 'trigger' : _getNodeAttribute( t_node, 'trigger', encoding )
+               , 'before_script' : _getNodeAttribute( t_node, 'before_script'
+                                                  , encoding )
+               , 'after_script' : _getNodeAttribute( t_node, 'after_script'
+                                                   , encoding )
+               , 'action' : _extractActionNode( t_node, encoding )
+               , 'guard' : _extractGuardNode( t_node, encoding )
+               }
+
+        info[ 'variables' ] = var_map = {}
+
+        for assignment in t_node.getElementsByTagName( 'assignment' ):
+
+            name = _getNodeAttribute( assignment, 'name', encoding )
+            value = _coalesceTextNodeChildren( assignment, encoding )
+            var_map[ name ] = value # XXX: type lost, only know strings???
+
+        result.append( info )
+
+    return result
+
+def _extractActionNode( parent, encoding=None ):
+
+    nodes = parent.getElementsByTagName( 'action' )
+    assert len( nodes ) <= 1, nodes
+
+    if len( nodes ) < 1:
+        return { 'name' : '', 'url' : '', 'category' : '' }
+
+    node = nodes[ 0 ]
+
+    return { 'name' : _coalesceTextNodeChildren( node, encoding )
+           , 'url' : _getNodeAttribute( node, 'url', encoding )
+           , 'category' : _getNodeAttribute( node, 'category', encoding )
+           }
+
+def _extractGuardNode( parent, encoding=None ):
+
+    nodes = parent.getElementsByTagName( 'guard' )
+    assert len( nodes ) <= 1, nodes
+
+    if len( nodes ) < 1:
+        return { 'permissions' : (), 'roles' : (), 'groups' : (), 'expr' : '' }
+
+    node = nodes[ 0 ]
+
+    expr_nodes = node.getElementsByTagName( 'expression' )
+    assert( len( expr_nodes ) <= 1 )
+
+    expr_text = expr_nodes and _coalesceTextNodeChildren( expr_nodes[ 0 ]
+                                                        , encoding
+                                                        ) or ''
+
+    return { 'permissions' : [ _coalesceTextNodeChildren( x, encoding )
+                                for x in node.getElementsByTagName(
+                                                            'permission' ) ]
+           , 'roles' : [ _coalesceTextNodeChildren( x, encoding )
+                          for x in node.getElementsByTagName( 'role' ) ]
+           , 'groups' : [ _coalesceTextNodeChildren( x, encoding )
+                          for x in node.getElementsByTagName( 'group' ) ]
+           , 'expression' : expr_text
+           }




More information about the CMF-checkins mailing list