Back to index

plone3  3.1.7
zcml.py
Go to the documentation of this file.
00001 
00002 #
00003 # Copyright (c) 2006-2007 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """GenericSetup ZCML directives.
00014 
00015 $Id: zcml.py 82937 2008-01-17 21:55:32Z hannosch $
00016 """
00017 
00018 from zope.configuration.fields import GlobalObject
00019 from zope.configuration.fields import MessageID
00020 from zope.configuration.fields import Path
00021 from zope.configuration.fields import PythonIdentifier
00022 from zope.interface import Interface
00023 
00024 from interfaces import BASE
00025 from registry import _import_step_registry
00026 from registry import _export_step_registry
00027 from registry import _profile_registry
00028 from upgrade import _upgrade_registry
00029 
00030 #### genericsetup:registerProfile
00031 
00032 class IRegisterProfileDirective(Interface):
00033 
00034     """Register profiles with the global registry.
00035     """
00036 
00037     name = PythonIdentifier(
00038         title=u'Name',
00039         description=u"If not specified 'default' is used.",
00040         default=u'default',
00041         required=False)
00042 
00043     title = MessageID(
00044         title=u'Title',
00045         description=u'Optional title for the profile.',
00046         default=None,
00047         required=False)
00048 
00049     description = MessageID(
00050         title=u'Description',
00051         description=u'Optional description for the profile.',
00052         default=None,
00053         required=False)
00054 
00055     directory = Path(
00056         title=u'Path',
00057         description=u"If not specified 'profiles/<name>' is used.",
00058         required=False)
00059 
00060     provides = GlobalObject(
00061         title=u'Type',
00062         description=u"If not specified 'BASE' is used.",
00063         default=BASE,
00064         required=False)
00065 
00066     for_ = GlobalObject(
00067         title=u'Site Interface',
00068         description=u'If not specified the profile is always available.',
00069         default=None,
00070         required=False)
00071 
00072 
00073 _profile_regs = []
00074 def registerProfile(_context, name=u'default', title=None, description=None,
00075                     directory=None, provides=BASE, for_=None):
00076     """ Add a new profile to the registry.
00077     """
00078     product = _context.package.__name__
00079     if directory is None:
00080         directory = 'profiles/%s' % name
00081 
00082     if title is None:
00083         title = u"Profile '%s' from '%s'" % (name, product)
00084 
00085     if description is None:
00086         description = u''
00087 
00088     _profile_regs.append('%s:%s' % (product, name))
00089 
00090     _context.action(
00091         discriminator = ('registerProfile', product, name),
00092         callable = _profile_registry.registerProfile,
00093         args = (name, title, description, directory, product, provides, for_)
00094         )
00095 
00096 
00097 #### genericsetup:exportStep
00098 
00099 class IExportStepDirective(Interface):
00100     name = PythonIdentifier(
00101         title=u'Name',
00102         description=u'',
00103         required=True)
00104 
00105     title = MessageID(
00106         title=u'Title',
00107         description=u'',
00108         required=True)
00109 
00110     description = MessageID(
00111         title=u'Description',
00112         description=u'',
00113         required=True)
00114 
00115     handler = GlobalObject(
00116         title=u'Handler',
00117         description=u'',
00118         required=True)
00119 
00120 
00121 _export_step_regs = []
00122 
00123 def exportStep(context, name, handler, title=None, description=None):
00124     global _export_step_regs
00125     _export_step_regs.append(name)
00126 
00127     context.action(
00128         discriminator = ('exportStep', name),
00129         callable = _export_step_registry.registerStep,
00130         args = (name, handler, title, description),
00131         )
00132 #### genericsetup:importStep
00133 
00134 class IImportStepDirective(Interface):
00135 
00136     """Register import steps with the global registry.
00137     """
00138 
00139     name = PythonIdentifier(
00140         title=u'Name',
00141         description=u'',
00142         required=True)
00143 
00144     title = MessageID(
00145         title=u'Title',
00146         description=u'',
00147         required=True)
00148 
00149     description = MessageID(
00150         title=u'Description',
00151         description=u'',
00152         required=True)
00153 
00154     handler = GlobalObject(
00155         title=u'Handler',
00156         description=u'',
00157         required=True)
00158 
00159 
00160 class IImportStepDependsDirective(Interface):
00161     name = PythonIdentifier(
00162         title=u'Name',
00163         description=u'Name of another import step that has to be run first',
00164         required=True)
00165 
00166 _import_step_regs = []
00167 
00168 class importStep:
00169     def __init__(self, context, name, title, description, handler):
00170         """ Add a new import step to the registry.
00171         """
00172         self.context=context
00173         self.discriminator = ('importStep', name),
00174         self.name=name
00175         self.handler=handler
00176         self.title=title
00177         self.description=description
00178         self.dependencies=()
00179 
00180 
00181     def depends(self, context, name):
00182         self.dependencies+=(name,)
00183 
00184 
00185     def __call__(self):
00186         global _import_step_regs
00187         _import_step_regs.append(self.name)
00188 
00189         self.context.action(
00190             discriminator = self.discriminator,
00191             callable = _import_step_registry.registerStep,
00192             args = (self.name, None, self.handler, self.dependencies,
00193                         self.title, self.description),
00194             )
00195 
00196 #### genericsetup:upgradeStep
00197 
00198 import zope.schema
00199 from upgrade import UpgradeStep
00200 from upgrade import _registerUpgradeStep
00201 from upgrade import _registerNestedUpgradeStep
00202 
00203 class IUpgradeStepsDirective(Interface):
00204     """
00205     Define multiple upgrade steps without repeating all of the parameters
00206     """
00207     source = zope.schema.ASCII(
00208         title=u"Source version",
00209         required=False)
00210 
00211     destination = zope.schema.ASCII(
00212         title=u"Destination version",
00213         required=False)
00214 
00215     sortkey = zope.schema.Int(
00216         title=u"Sort key",
00217         required=False)
00218 
00219     profile = zope.schema.TextLine(
00220         title=u"GenericSetup profile id",
00221         required=True)
00222 
00223 class IUpgradeStepsStepSubDirective(Interface):
00224     """
00225     Subdirective to IUpgradeStepsDirective
00226     """
00227     title = zope.schema.TextLine(
00228         title=u"Title",
00229         required=True)
00230 
00231     description = zope.schema.TextLine(
00232         title=u"Upgrade step description",
00233         required=True)
00234 
00235     handler = GlobalObject(
00236         title=u"Upgrade handler",
00237         required=True)
00238 
00239     checker = GlobalObject(
00240         title=u"Upgrade checker",
00241         required=False)
00242 
00243 class IUpgradeStepDirective(IUpgradeStepsDirective, IUpgradeStepsStepSubDirective):
00244     """
00245     Define multiple upgrade steps without repeating all of the parameters
00246     """
00247 
00248 
00249 def upgradeStep(_context, title, profile, handler, description=None, source='*',
00250                 destination='*', sortkey=0, checker=None):
00251     step = UpgradeStep(title, profile, source, destination, description, handler,
00252                        checker, sortkey)
00253     _context.action(
00254         discriminator = ('upgradeStep', source, destination, handler, sortkey),
00255         callable = _registerUpgradeStep,
00256         args = (step,),
00257         )
00258 
00259 class upgradeSteps(object):
00260     """
00261     Allows nested upgrade steps.
00262     """
00263     def __init__(self, _context, profile, source='*', destination='*', sortkey=0):
00264         self.profile = profile
00265         self.source = source
00266         self.dest = destination
00267         self.sortkey = sortkey
00268         self.id = None
00269 
00270     def upgradeStep(self, _context, title, description, handler, checker=None):
00271         step = UpgradeStep(title, self.profile, self.source, self.dest, description,
00272                            handler, checker, self.sortkey)
00273         if self.id is None:
00274             self.id = str(abs(hash('%s%s%s%s' % (title, self.source, self.dest,
00275                                                  self.sortkey))))
00276         _context.action(
00277             discriminator = ('upgradeStep', self.source, self.dest, handler,
00278                              self.sortkey),
00279             callable = _registerNestedUpgradeStep,
00280             args = (step, self.id),
00281             )
00282 
00283     def __call__(self):
00284         return ()
00285 
00286 
00287 #### cleanup
00288 
00289 def cleanUpProfiles():
00290     global _profile_regs
00291     for profile_id in _profile_regs:
00292         del _profile_registry._profile_info[profile_id]
00293         _profile_registry._profile_ids.remove(profile_id)
00294     _profile_regs = []
00295 
00296     _upgrade_registry.clear()
00297 
00298 
00299 def cleanUpImportSteps():
00300     global _import_step_regs
00301     for name in  _import_step_regs:
00302         try:
00303              _import_step_registry.unregisterStep( name )
00304         except KeyError:
00305             pass
00306 
00307     _import_step_regs=[]
00308 
00309 def cleanUpExportSteps():
00310     global _export_step_regs
00311     for name in  _export_step_regs:
00312         try:
00313              _export_step_registry.unregisterStep( name )
00314         except KeyError:
00315             pass
00316 
00317     _export_step_regs=[]
00318 
00319 from zope.testing.cleanup import addCleanUp
00320 addCleanUp(cleanUpProfiles)
00321 addCleanUp(cleanUpImportSteps)
00322 addCleanUp(cleanUpExportSteps)
00323 del addCleanUp