[CMF-checkins] CVS: CMF/CMFSetup - actions.py:1.1 rolemap.py:1.6
Tres Seaver
tseaver at zope.com
Mon May 24 18:44:22 EDT 2004
Update of /cvs-repository/CMF/CMFSetup
In directory cvs.zope.org:/tmp/cvs-serv24152
Modified Files:
rolemap.py
Added Files:
actions.py
Log Message:
- rolemap:
o Tweak import order.
o Coding style for attribute names.
o Docstring fixes.
- tests/*.png:
o Rename for better command-line expansion karma.
- actions.py:
o Export / import actions tool's providers and their actions.
=== Added File CMF/CMFSetup/actions.py ===
""" Classes: ActionsProviderConfigurator
$Id: actions.py,v 1.1 2004/05/24 22:43:50 tseaver Exp $
"""
from xml.sax import parseString
from xml.sax.handler import ContentHandler
from AccessControl import ClassSecurityInfo
from Acquisition import Implicit
from Globals import InitializeClass
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Products.CMFCore.utils import getToolByName
from permissions import ManagePortal
from utils import _xmldir
# Dummy object for passing to listActions
class _FauxContent:
def __init__( self, **kw ):
self.__dict__.update( kw )
class _ActionProviderParser( ContentHandler ):
def __init__( self, site, encoding='latin-1' ):
self._site = site
self._encoding = encoding
self._provider_info = {}
self._provider_ids = []
def _extract( self, attrs, key ):
return attrs[ key ].encode( self._encoding )
def startElement( self, name, attrs ):
if name == 'actions-tool':
pass
elif name == 'action-provider':
id = self._extract( attrs, 'id' )
if id not in self._provider_ids:
self._provider_ids.append( id )
elif name == 'action':
provider_id = self._provider_ids[ -1 ]
actions = self._provider_info.setdefault( provider_id, [] )
info = { 'action_id' : self._extract( attrs, 'action_id' )
, 'category' : self._extract( attrs, 'category' )
, 'name' : self._extract( attrs, 'title' )
, 'action' : self._extract( attrs, 'action_expr' )
, 'condition' : self._extract( attrs, 'condition_expr' )
, 'permission' : self._extract( attrs, 'permission' )
, 'category' : self._extract( attrs, 'category' )
, 'visible' : self._extract( attrs, 'visible' )
}
actions.append( info )
else:
raise ValueError, 'Unknown element %s' % name
def endElement( self, name ):
if name in ( 'actions-tool', 'action' ):
pass
def endDocument( self ):
actions_tool = getToolByName( self._site, 'portal_actions' )
for provider_id in self._provider_ids:
if provider_id not in actions_tool.listActionProviders():
actions_tool.addActionProvider( provider_id )
provider = getToolByName( self._site, provider_id )
provider._actions = ()
for info in self._provider_info.get( provider_id, () ):
provider.addAction( id=info[ 'action_id' ]
, name=info[ 'name' ]
, action=info[ 'action' ]
, condition=info[ 'condition' ]
, permission=info[ 'permission' ]
, category=info[ 'category' ]
, visible=info[ 'visible' ]
)
class ActionProvidersConfigurator( Implicit ):
""" Synthesize XML description of site's action providers.
"""
security = ClassSecurityInfo()
security.setDefaultAccess( 'allow' )
def __init__( self, site ):
self._site = site
_providers = PageTemplateFile( 'apcExport.xml'
, _xmldir
, __name__='_providers'
)
security.declareProtected( ManagePortal, 'listProviderInfo' )
def listProviderInfo( self ):
""" Return a sequence of mappings for each action provider.
"""
actions_tool = getToolByName( self._site, 'portal_actions' )
faux = _FauxContent( content=None, isAnonymous=1 )
result = []
for provider_id in actions_tool.listActionProviders():
provider_info = { 'id' : provider_id, 'actions' : [] }
result.append( provider_info )
append = provider_info[ 'actions' ].append
provider = getToolByName( self._site, provider_id )
actions = provider.listActions( info=faux ) or []
for action in actions:
ainfo = {}
ainfo[ 'id'] = action.getId()
ainfo[ 'name'] = action.Title()
p = action.getPermissions()
if p:
ainfo[ 'permission'] = p[ 0 ]
else:
ainfo[ 'permission'] = ''
ainfo[ 'category'] = action.getCategory() or 'object'
ainfo[ 'visible'] = action.getVisibility()
ainfo[ 'action'] = action.getActionExpression()
ainfo[ 'condition'] = action.getCondition()
append( ainfo )
return result
security.declareProtected( ManagePortal, 'generateXML' )
def generateXML( self ):
""" Pseudo API.
"""
return self._providers()
security.declareProtected( ManagePortal, 'parseXML' )
def parseXML( self, text ):
""" Pseudo API.
"""
reader = getattr( text, 'read', None )
if reader is not None:
text = reader()
parseString( text, _ActionProviderParser( self._site ) )
InitializeClass( ActionProvidersConfigurator )
#
# Configurator entry points
#
_FILENAME = 'actions.xml'
def importActionProviders( 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( 'importActionProviders'
, '20040518-01'
, Products.CMFSetup.actions.importActionProviders
, ()
, 'Action Provider import'
, 'Import action providers registered with '
'the actions tool, and their actions.'
)
o Register via XML:
<setup-step id="importActionProviders"
version="20040524-01"
handler="Products.CMFSetup.actions.importActionProviders"
title="Action Provider import"
>Import action providers registered with the actions tool,
and their actions.</setup-step>
"""
site = context.getSite()
if context.shouldPurge():
actions_tool = getToolByName( site, 'portal_actions' )
for provider_id in actions_tool.listActionProviders():
actions_tool.deleteActionProvider( provider_id )
text = context.readDataFile( _FILENAME )
if text is not None:
apc = ActionProvidersConfigurator( site ).__of__( site )
apc.parseXML( text )
return 'Action providers imported.'
def exportActionProviders( context ):
""" Export action providers and their actions as an XML file
o 'context' must implement IExportContext.
o Register via Python:
registry = site.portal_setup.export_steps
registry.registerStep( 'exportActionProviders'
, Products.CMFSetup.rolemap.exportActionProviders
, 'Action Provider export'
, 'Export action providers registered with '
'the actions tool, and their actions.'
)
o Register via XML:
<export-script id="exportActionProviders"
version="20040518-01"
handler="Products.CMFSetup.rolemap.exportActionProviders"
title="Action Provider export"
>Export action providers registered with the actions tool,
and their actions.</export-script>
"""
site = context.getSite()
apc = ActionProvidersConfigurator( site ).__of__( site )
text = apc.generateXML()
context.writeDataFile( _FILENAME, text, 'text/xml' )
return 'Action providers exported.'
=== CMF/CMFSetup/rolemap.py 1.5 => 1.6 ===
--- CMF/CMFSetup/rolemap.py:1.5 Sun May 23 23:12:08 2004
+++ CMF/CMFSetup/rolemap.py Mon May 24 18:43:50 2004
@@ -2,6 +2,8 @@
$Id$
"""
+from xml.sax import parseString
+from xml.sax.handler import ContentHandler
from AccessControl import ClassSecurityInfo
from AccessControl.Permission import Permission
@@ -12,12 +14,6 @@
from permissions import ManagePortal
from utils import _xmldir
-#
-# Import
-#
-from xml.sax import parseString
-from xml.sax.handler import ContentHandler
-
class _RolemapParser( ContentHandler ):
def __init__( self, site, encoding='latin-1' ):
@@ -74,7 +70,7 @@
security.setDefaultAccess( 'allow' )
def __init__( self, site ):
- self.site = site
+ self._site = site
_rolemap = PageTemplateFile( 'rmeExport.xml'
, _xmldir
@@ -86,7 +82,7 @@
""" List the valid role IDs for our site.
"""
- return self.site.valid_roles()
+ return self._site.valid_roles()
security.declareProtected( ManagePortal, 'listPermissions' )
def listPermissions( self ):
@@ -109,10 +105,10 @@
permissions = []
valid_roles = self.listRoles()
- for perm in self.site.ac_inherited_permissions( 1 ):
+ for perm in self._site.ac_inherited_permissions( 1 ):
name = perm[ 0 ]
- p = Permission( name, perm[ 1 ], self.site )
+ p = Permission( name, perm[ 1 ], self._site )
roles = p.getRoles( default=[] )
acquire = isinstance( roles, list ) # tuple means don't acquire
roles = [ r for r in roles if r in valid_roles ]
@@ -142,7 +138,7 @@
if reader is not None:
text = reader()
- parseString( text, _RolemapParser( self.site ) )
+ parseString( text, _RolemapParser( self._site ) )
InitializeClass( RolemapConfigurator )
@@ -161,9 +157,9 @@
o Register via Python:
registry = site.portal_setup.setup_steps
- registry.registerStep( 'importRoleMap'
+ registry.registerStep( 'importRolemap'
, '20040518-01'
- , Products.CMFSetup.rolemap.importRoleMap
+ , Products.CMFSetup.rolemap.importRolemap
, ()
, 'Role / Permission import'
, 'Import additional roles, and map '
@@ -172,9 +168,9 @@
o Register via XML:
- <setup-step id="importRoleMap"
+ <setup-step id="importRolemap"
version="20040518-01"
- handler="Products.CMFSetup.rolemap.importRoleMap"
+ handler="Products.CMFSetup.rolemap.importRolemap"
title="Role / Permission import"
>Import additional roles, and map roles to permissions.</setup-step>
@@ -212,10 +208,8 @@
o Register via Python:
registry = site.portal_setup.export_steps
- registry.registerStep( 'exportRoleMap'
- , '20040518-01'
- , Products.CMFSetup.rolemap.exportRoleMap
- , ()
+ registry.registerStep( 'exportRolemap'
+ , Products.CMFSetup.rolemap.exportRolemap
, 'Role / Permission export'
, 'Export additional roles, and '
'role / permission map '
@@ -223,9 +217,9 @@
o Register via XML:
- <export-script id="exportRoleMap"
+ <export-script id="exportRolemap"
version="20040518-01"
- handler="Products.CMFSetup.rolemap.exportRoleMap"
+ handler="Products.CMFSetup.rolemap.exportRolemap"
title="Role / Permission export"
>Export additional roles, and role / permission map.</export-script>
More information about the CMF-checkins
mailing list