[CMF-checkins] CVS: CMF/CMFSetup - skins.py:1.5

Tres Seaver tseaver at zope.com
Sat May 29 22:12:04 EDT 2004


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

Modified Files:
	skins.py 
Log Message:


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

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


=== CMF/CMFSetup/skins.py 1.4 => 1.5 ===
--- CMF/CMFSetup/skins.py:1.4	Thu May 27 00:19:33 2004
+++ CMF/CMFSetup/skins.py	Sat May 29 22:12:03 2004
@@ -23,67 +23,97 @@
 from utils import _resolveDottedName
 from utils import _xmldir
 
-class _SkinsParser( HandlerBase ):
+#
+#   Entry points
+#
+_FILENAME = 'skins.xml'
 
-    def __init__( self, site, encoding='latin-1' ):
+def importSkinsTool( context ):
 
-        self._site = site
-        self._skins_tool = getToolByName( site, 'portal_skins' )
-        self._encoding = encoding
-        self._skin_dirs = []
-        self._skin_paths = []
-        self._default_skin = None
-        self._request_var = None
-        self._allow_arbitrary = False
-        self._persist_cookie = False
+    """ Import skins tool FSDirViews and skin paths from an XML file
 
-    def startElement( self, name, attrs ):
+    o 'context' must implement IImportContext.
 
-        if name == 'skins-tool':
-            self._default_skin = self._extract( attrs, 'default_skin' )
-            self._request_var = self._extract( attrs, 'request_varname' )
-            self._allow_arbitrary = self._extractBoolean( attrs
-                                                        , 'allow_any'
-                                                        , False
-                                                        )
-            self._persist_cookie = self._extractBoolean( attrs
-                                                       , 'cookie_persistence'
-                                                       , False
-                                                       )
+    o Register via Python:
 
-        elif name == 'skin-directory':
+      registry = site.portal_setup.getImportStepRegistry()
+      registry.registerStep( 'importSkinsTool'
+                           , '20040518-01'
+                           , Products.CMFSetup.skins.importSkinsTool
+                           , ()
+                           , 'Skins Tool import'
+                           , 'Import skins tool FSDVs and skin paths.'
+                           )
 
-            self._skin_dirs.append( ( self._extract( attrs, 'id' )
-                                    , self._extract( attrs, 'directory' )
-                                    ) )
+    o Register via XML:
+ 
+      <setup-step id="importSkinsTool"
+                  version="20040524-01"
+                  handler="Products.CMFSetup.skins.importSkinsTool"
+                  title="Skins Tool import"
+      >Import skins tool FSDVs and skin paths.</setup-step>
 
-        elif name == 'skin-path':
+    """
+    site = context.getSite()
+    encoding = context.getEncoding()
 
-            path_name = self._extract( attrs,'id' )
-            self._skin_paths.append( ( path_name, [] ) )
+    skins_tool = getToolByName( site, 'portal_skins' )
 
-        elif name == 'layer':
+    if context.shouldPurge():
 
-            path_name, layers = self._skin_paths[ -1 ]
-            layers.append( self._extract( attrs, 'name' ) )
+        skins_tool._getSelections().clear()
 
-        else:
-            raise ValueError, 'Unknown element %s' % name
+        for id in skins_tool.objectIds( DirectoryView.meta_type ):
+            skins_tool._delObject(id)
 
-    def endDocument( self ):
+    text = context.readDataFile( _FILENAME )
 
-        tool = self._skins_tool
-        tool.default_skin = str( self._default_skin )
-        tool.request_varname = str( self._request_var )
-        tool.allow_any =  self._allow_arbitrary and 1 or 0
-        tool.cookie_persistence =  self._persist_cookie and 1 or 0
+    if text is not None:
 
-        for id, directory in self._skin_dirs:
+        stc = SkinsToolConfigurator( site ).__of__( site )
+        stc.parseXML( text, encoding )
 
-            createDirectoryView( tool, directory, id )
+    #
+    #   Purge and rebuild the skin path, now that we have added our stuff.
+    #
+    site._v_skindata = None
+    skins_tool.setupCurrentSkin( site.REQUEST )
+
+    return 'Skins tool imported'
+
+
+def exportSkinsTool( context ):
+
+    """ Export skins tool FSDVs and skin paths as an XML file
+
+    o 'context' must implement IExportContext.
+
+    o Register via Python:
+
+      registry = site.portal_setup.getExportStepRegistry()
+      registry.registerStep( 'exportSkinsTool'
+                           , Products.CMFSetup.skins.exportSkinsTool
+                           , 'Skins Tool export'
+                           , 'Export skins tool FSDVs and skin paths.'
+                           )
+
+    o Register via XML:
+ 
+      <export-script id="exportSkinsTool"
+                     version="20040518-01"
+                     handler="Products.CMFSetup.skins.exportSkinsTool"
+                     title="Skins Tool export"
+      >Export skins tool FSDVs and skin paths.</export-script>
+
+    """
+    site = context.getSite()
+    stc = SkinsToolConfigurator( site ).__of__( site )
+    text = stc.generateXML()
+
+    context.writeDataFile( _FILENAME, text, 'text/xml' )
+
+    return 'Skins tool exported.'
 
-        for path_name, layers in self._skin_paths:
-            tool.addSkinSelection( path_name, ', '.join( layers ) )
 
 class SkinsToolConfigurator( Implicit ):
 
@@ -176,7 +206,7 @@
         return self._skinsConfig()
 
     security.declareProtected( ManagePortal, 'parseXML' )
-    def parseXML( self, text ):
+    def parseXML( self, text, encoding=None ):
 
         """ Pseudo API.
         """
@@ -185,7 +215,22 @@
         if reader is not None:
             text = reader()
 
-        parseString( text, _SkinsParser( self._site ) )
+        parser = _SkinsParser( encoding )
+        parseString( text, parser )
+
+        tool = getToolByName( self._site, 'portal_skins' )
+
+        tool.default_skin = str( parser._default_skin )
+        tool.request_varname = str( parser._request_var )
+        tool.allow_any =  parser._allow_arbitrary and 1 or 0
+        tool.cookie_persistence =  parser._persist_cookie and 1 or 0
+
+        for id, directory in parser._skin_dirs:
+
+            createDirectoryView( tool, directory, id )
+
+        for path_name, layers in parser._skin_paths:
+            tool.addSkinSelection( path_name, ', '.join( layers ) )
 
     #
     #   Helper methods
@@ -199,88 +244,53 @@
 InitializeClass( SkinsToolConfigurator )
 
 
-_FILENAME = 'skins.xml'
-
-def importSkinsTool( context ):
-
-    """ Import skins tool FSDirViews and skin paths from an XML file
-
-    o 'context' must implement IImportContext.
-
-    o Register via Python:
-
-      registry = site.portal_setup.getImportStepRegistry()
-      registry.registerStep( 'importSkinsTool'
-                           , '20040518-01'
-                           , Products.CMFSetup.skins.importSkinsTool
-                           , ()
-                           , 'Skins Tool import'
-                           , 'Import skins tool FSDVs and skin paths.'
-                           )
-
-    o Register via XML:
- 
-      <setup-step id="importSkinsTool"
-                  version="20040524-01"
-                  handler="Products.CMFSetup.skins.importSkinsTool"
-                  title="Skins Tool import"
-      >Import skins tool FSDVs and skin paths.</setup-step>
-
-    """
-    site = context.getSite()
-    skins_tool = getToolByName( site, 'portal_skins' )
-
-    if context.shouldPurge():
-
-        skins_tool._getSelections().clear()
-
-        for id in skins_tool.objectIds( DirectoryView.meta_type ):
-            skins_tool._delObject(id)
+class _SkinsParser( HandlerBase ):
 
-    text = context.readDataFile( _FILENAME )
+    security = ClassSecurityInfo()
+    security.declareObjectPrivate()
+    security.setDefaultAccess( 'deny' )
 
-    if text is not None:
+    def __init__( self, encoding ):
 
-        stc = SkinsToolConfigurator( site ).__of__( site )
-        stc.parseXML( text )
-
-    #
-    #   Purge and rebuild the skin path, now that we have added our stuff.
-    #
-    site._v_skindata = None
-    skins_tool.setupCurrentSkin( site.REQUEST )
-
-    return 'Skins tool imported'
+        self._encoding = encoding
+        self._skin_dirs = []
+        self._skin_paths = []
+        self._default_skin = None
+        self._request_var = None
+        self._allow_arbitrary = False
+        self._persist_cookie = False
 
+    def startElement( self, name, attrs ):
 
-def exportSkinsTool( context ):
+        if name == 'skins-tool':
+            self._default_skin = self._extract( attrs, 'default_skin' )
+            self._request_var = self._extract( attrs, 'request_varname' )
+            self._allow_arbitrary = self._extractBoolean( attrs
+                                                        , 'allow_any'
+                                                        , False
+                                                        )
+            self._persist_cookie = self._extractBoolean( attrs
+                                                       , 'cookie_persistence'
+                                                       , False
+                                                       )
 
-    """ Export skins tool FSDVs and skin paths as an XML file
+        elif name == 'skin-directory':
 
-    o 'context' must implement IExportContext.
+            self._skin_dirs.append( ( self._extract( attrs, 'id' )
+                                    , self._extract( attrs, 'directory' )
+                                    ) )
 
-    o Register via Python:
+        elif name == 'skin-path':
 
-      registry = site.portal_setup.getExportStepRegistry()
-      registry.registerStep( 'exportSkinsTool'
-                           , Products.CMFSetup.skins.exportSkinsTool
-                           , 'Skins Tool export'
-                           , 'Export skins tool FSDVs and skin paths.'
-                           )
+            path_name = self._extract( attrs,'id' )
+            self._skin_paths.append( ( path_name, [] ) )
 
-    o Register via XML:
- 
-      <export-script id="exportSkinsTool"
-                     version="20040518-01"
-                     handler="Products.CMFSetup.skins.exportSkinsTool"
-                     title="Skins Tool export"
-      >Export skins tool FSDVs and skin paths.</export-script>
+        elif name == 'layer':
 
-    """
-    site = context.getSite()
-    stc = SkinsToolConfigurator( site ).__of__( site )
-    text = stc.generateXML()
+            path_name, layers = self._skin_paths[ -1 ]
+            layers.append( self._extract( attrs, 'name' ) )
 
-    context.writeDataFile( _FILENAME, text, 'text/xml' )
+        else:
+            raise ValueError, 'Unknown element %s' % name
 
-    return 'Skins tool exported.'
+InitializeClass( _SkinsParser )




More information about the CMF-checkins mailing list