Back to index

plone3  3.1.7
zcml.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 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 """CMFCore ZCML directives.
00014 
00015 $Id: zcml.py 72873 2007-02-27 13:13:44Z yuppie $
00016 """
00017 
00018 from os import path
00019 
00020 from zope.configuration.fields import Bool
00021 from zope.configuration.fields import Path
00022 from zope.configuration.fields import PythonIdentifier
00023 from zope.configuration.fields import Tokens
00024 from zope.interface import Interface
00025 from zope.schema import ASCIILine
00026 
00027 from DirectoryView import _dirreg
00028 from DirectoryView import _generateKey
00029 from DirectoryView import ignore
00030 
00031 
00032 class IRegisterDirectoryDirective(Interface):
00033 
00034     """Register directories with the global registry.
00035     """
00036 
00037     name = PythonIdentifier(
00038         title=u'Name',
00039         description=u'Name of the directory.',
00040         required=True)
00041 
00042     directory = Path(
00043         title=u'Path',
00044         description=u'Path relative to the package. If not specified, '
00045                     u"'skins/<name>' is used.",
00046         required=False)
00047 
00048     recursive = Bool(
00049         title=u'Recursive?',
00050         description=u'False by default. If true, register all subdirectories '
00051                     u'as well.',
00052         required=False)
00053 
00054     ignore = Tokens(
00055         title=u'Ignore',
00056         description=u'Files and subdirectories that should be ignored. If '
00057                     u"not specified, 'CVS' and '.svn' are ignored.",
00058         value_type=ASCIILine(),
00059         required=False)
00060 
00061 
00062 _directory_regs = []
00063 def registerDirectory(_context, name, directory=None, recursive=False,
00064                       ignore=ignore):
00065     """ Add a new directory to the registry.
00066     """
00067     if directory is None:
00068         subdir = 'skins/%s' % str(name)
00069         filepath = path.join(_context.package.__path__[0], 'skins', str(name))
00070     else:
00071         subdir = str(directory[len(_context.package.__path__[0])+1:])
00072         filepath = str(directory)
00073 
00074     reg_key = _generateKey(_context.package.__name__, subdir)
00075     _directory_regs.append(reg_key)
00076 
00077     _context.action(
00078         discriminator = ('registerDirectory', reg_key),
00079         callable = _dirreg.registerDirectoryByKey,
00080         args = (filepath, reg_key, int(recursive), ignore)
00081         )
00082 
00083 
00084 def cleanUp():
00085     global _directory_regs
00086     for reg_key in _directory_regs:
00087         for key in _dirreg._directories.keys():
00088             if key.startswith(reg_key):
00089                del _dirreg._directories[key]
00090     _directory_regs = []
00091 
00092 from zope.testing.cleanup import addCleanUp
00093 addCleanUp(cleanUp)
00094 del addCleanUp