[CMF-checkins] CVS: CMF/CMFSetup - rolemap.py:1.8

Tres Seaver tseaver at zope.com
Sat May 29 21:52:30 EDT 2004


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

Modified Files:
	rolemap.py 
Log Message:


  - Refactoring:  *Don't* modify while parsing!

  - Move "primary" objects up, and "helpers" down, in the module.


=== CMF/CMFSetup/rolemap.py 1.7 => 1.8 ===
--- CMF/CMFSetup/rolemap.py:1.7	Tue May 25 00:12:33 2004
+++ CMF/CMFSetup/rolemap.py	Sat May 29 21:52:30 2004
@@ -14,57 +14,95 @@
 from utils import HandlerBase
 from utils import _xmldir
 
-class _RolemapParser( HandlerBase ):
+#
+#   Configurator entry points
+#
+_FILENAME = 'rolemap.xml'
 
-    def __init__( self, site, encoding='latin-1' ):
+def importRolemap( context ):
 
-        self._site = site
-        self._encoding = encoding
-        self._roles = []
-        self._permissions = []
+    """ Export roles / permission map as an XML file
 
-    def startElement( self, name, attrs ):
+    o 'context' must implement IImportContext.
 
-        if name == 'role':
-            self._roles.append( self._extract( attrs, 'name' )  )
+    o Register via Python:
 
-        elif name == 'permission':
+      registry = site.portal_setup.setup_steps
+      registry.registerStep( 'importRolemap'
+                           , '20040518-01'
+                           , Products.CMFSetup.rolemap.importRolemap
+                           , ()
+                           , 'Role / Permission import'
+                           , 'Import additional roles, and map '
+                           'roles to permissions'
+                           )
 
-            acquire = self._extract( attrs, 'acquire' ).lower()
-            acquire = acquire in ( '1', 'true', 'yes' )
+    o Register via XML:
+ 
+      <setup-step id="importRolemap"
+                  version="20040518-01"
+                  handler="Products.CMFSetup.rolemap.importRolemap"
+                  title="Role / Permission import"
+      >Import additional roles, and map roles to permissions.</setup-step>
 
-            info = { 'name'     : self._extract( attrs, 'name' )
-                   , 'roles'    : self._extract( attrs, 'roles' ).split()
-                   , 'acquire'  : acquire
-                   }
+    """
+    site = context.getSite()
 
-            self._permissions.append( info )
+    if context.shouldPurge():
 
-        elif name not in ( 'rolemap', 'permissions', 'roles' ):
-            raise ValueError, 'Unknown element: %s' % name
+        items = site.__dict__.items()
 
-    def endDocument( self ):
+        for k, v in items: # XXX: WAAA
 
-        immediate_roles = list( getattr( self._site, '__ac_roles__', [] ) )[:]
-        already = {}
-        for role in self._site.valid_roles():
-            already[ role ] = 1
+            if k == '__ac_roles__':
+                delattr( site, k )
 
-        for role in self._roles:
+            if k.startswith( '_' ) and k.endswith( '_Permission' ):
+                delattr( site, k )
 
-            if already.get( role ) is None:
-                immediate_roles.append( role )
-                already[ role ] = 1
+    text = context.readDataFile( _FILENAME )
 
-        immediate_roles.sort()
-        self._site.__ac_roles__ = tuple( immediate_roles )
+    if text is not None:
 
-        for permission in self._permissions:
+        rc = RolemapConfigurator( site ).__of__( site )
+        rc.parseXML( text )
+
+    return 'Role / permission map imported.'
+
+
+def exportRolemap( context ):
+
+    """ Export roles / permission map as an XML file
+
+    o 'context' must implement IExportContext.
+
+    o Register via Python:
+
+      registry = site.portal_setup.export_steps
+      registry.registerStep( 'exportRolemap'
+                           , Products.CMFSetup.rolemap.exportRolemap
+                           , 'Role / Permission export'
+                           , 'Export additional roles, and '
+                             'role / permission map '
+                           )
+
+    o Register via XML:
+ 
+      <export-script id="exportRolemap"
+                     version="20040518-01"
+                     handler="Products.CMFSetup.rolemap.exportRolemap"
+                     title="Role / Permission export"
+      >Export additional roles, and role / permission map.</export-script>
+
+    """
+    site = context.getSite()
+    rc = RolemapConfigurator( site ).__of__( site )
+    text = rc.generateXML()
+
+    context.writeDataFile( _FILENAME, text, 'text/xml' )
+
+    return 'Role / permission map exported.'
 
-            self._site.manage_permission( permission[ 'name' ]
-                                        , permission[ 'roles' ]
-                                        , permission[ 'acquire' ]
-                                        )
 
 class RolemapConfigurator( Implicit ):
 
@@ -142,96 +180,57 @@
         if reader is not None:
             text = reader()
 
-        parseString( text, _RolemapParser( self._site ) )
+        parser = _RolemapParser()
+        parseString( text, parser )
 
-InitializeClass( RolemapConfigurator )
-
-
-#
-#   Configurator entry points
-#
-_FILENAME = 'rolemap.xml'
-
-def importRolemap( context ):
-
-    """ Export roles / permission map as an XML file
-
-    o 'context' must implement IImportContext.
-
-    o Register via Python:
-
-      registry = site.portal_setup.setup_steps
-      registry.registerStep( 'importRolemap'
-                           , '20040518-01'
-                           , Products.CMFSetup.rolemap.importRolemap
-                           , ()
-                           , 'Role / Permission import'
-                           , 'Import additional roles, and map '
-                           'roles to permissions'
-                           )
-
-    o Register via XML:
- 
-      <setup-step id="importRolemap"
-                  version="20040518-01"
-                  handler="Products.CMFSetup.rolemap.importRolemap"
-                  title="Role / Permission import"
-      >Import additional roles, and map roles to permissions.</setup-step>
-
-    """
-    site = context.getSite()
-
-    if context.shouldPurge():
-
-        items = site.__dict__.items()
+        immediate_roles = list( getattr( self._site, '__ac_roles__', [] ) )[:]
+        already = {}
+        for role in self._site.valid_roles():
+            already[ role ] = 1
 
-        for k, v in items: # XXX: WAAA
+        for role in parser._roles:
 
-            if k == '__ac_roles__':
-                delattr( site, k )
+            if already.get( role ) is None:
+                immediate_roles.append( role )
+                already[ role ] = 1
 
-            if k.startswith( '_' ) and k.endswith( '_Permission' ):
-                delattr( site, k )
+        immediate_roles.sort()
+        self._site.__ac_roles__ = tuple( immediate_roles )
 
-    text = context.readDataFile( _FILENAME )
+        for permission in parser._permissions:
 
-    if text is not None:
+            self._site.manage_permission( permission[ 'name' ]
+                                        , permission[ 'roles' ]
+                                        , permission[ 'acquire' ]
+                                        )
 
-        rc = RolemapConfigurator( site ).__of__( site )
-        rc.parseXML( text )
+InitializeClass( RolemapConfigurator )
 
-    return 'Role / permission map imported.'
 
+class _RolemapParser( HandlerBase ):
 
-def exportRolemap( context ):
+    def __init__( self, encoding='latin-1' ):
 
-    """ Export roles / permission map as an XML file
+        self._encoding = encoding
+        self._roles = []
+        self._permissions = []
 
-    o 'context' must implement IExportContext.
+    def startElement( self, name, attrs ):
 
-    o Register via Python:
+        if name == 'role':
+            self._roles.append( self._extract( attrs, 'name' )  )
 
-      registry = site.portal_setup.export_steps
-      registry.registerStep( 'exportRolemap'
-                           , Products.CMFSetup.rolemap.exportRolemap
-                           , 'Role / Permission export'
-                           , 'Export additional roles, and '
-                             'role / permission map '
-                           )
+        elif name == 'permission':
 
-    o Register via XML:
- 
-      <export-script id="exportRolemap"
-                     version="20040518-01"
-                     handler="Products.CMFSetup.rolemap.exportRolemap"
-                     title="Role / Permission export"
-      >Export additional roles, and role / permission map.</export-script>
+            acquire = self._extract( attrs, 'acquire' ).lower()
+            acquire = acquire in ( '1', 'true', 'yes' )
 
-    """
-    site = context.getSite()
-    rc = RolemapConfigurator( site ).__of__( site )
-    text = rc.generateXML()
+            info = { 'name'     : self._extract( attrs, 'name' )
+                   , 'roles'    : self._extract( attrs, 'roles' ).split()
+                   , 'acquire'  : acquire
+                   }
 
-    context.writeDataFile( _FILENAME, text, 'text/xml' )
+            self._permissions.append( info )
 
-    return 'Role / permission map exported.'
+        elif name not in ( 'rolemap', 'permissions', 'roles' ):
+            raise ValueError, 'Unknown element: %s' % name




More information about the CMF-checkins mailing list