[Checkins] SVN: zope.generic/trunk/src/zope/generic/ clean up:
Dominik Huber
dominik.huber at perse.ch
Wed May 24 04:50:23 EDT 2006
Log message for revision 68260:
clean up:
- remove ini-file based stuff
- add cleanup method for toInterface cache
Changed:
U zope.generic/trunk/src/zope/generic/face/helper.py
U zope.generic/trunk/src/zope/generic/informationprovider/README.txt
U zope.generic/trunk/src/zope/generic/informationprovider/api.py
D zope.generic/trunk/src/zope/generic/informationprovider/helper.py
U zope.generic/trunk/src/zope/generic/informationprovider/meta.zcml
U zope.generic/trunk/src/zope/generic/informationprovider/metaconfigure.py
U zope.generic/trunk/src/zope/generic/informationprovider/metadirectives.py
U zope.generic/trunk/src/zope/generic/informationprovider/testing.py
U zope.generic/trunk/src/zope/generic/informationprovider/tests.py
-=-
Modified: zope.generic/trunk/src/zope/generic/face/helper.py
===================================================================
--- zope.generic/trunk/src/zope/generic/face/helper.py 2006-05-23 22:47:16 UTC (rev 68259)
+++ zope.generic/trunk/src/zope/generic/face/helper.py 2006-05-24 08:50:21 UTC (rev 68260)
@@ -21,6 +21,7 @@
from zope.dottedname.resolve import resolve
from zope.generic.face import IUndefinedContext
from zope.generic.face import IUndefinedKeyface
+from zope.testing import cleanup
@@ -33,6 +34,13 @@
# cache
__name_to_component = {}
+# clean cache for test purposes
+def _clear():
+ __name_to_component.clear()
+
+cleanup.addCleanUp(_clear)
+del _clear
+
def toInterface(dottedname):
try:
return __name_to_component[dottedname]
@@ -40,6 +48,7 @@
return __name_to_component.setdefault(dottedname, resolve(dottedname))
+
def toDescription(component, label=None, hint=None):
"""Use the __doc__ attribute for label and hint.
@@ -135,7 +144,7 @@
-def toFaceTuple(identifier):
+def toFaceTuple(identifier, keyface=IUndefinedKeyface, conface=IUndefinedContext):
"""Resolve 'keyface at conface' to (keyface, conface).
>>> from zope.interface import Interface
@@ -182,10 +191,10 @@
if len(parts) == 1:
try:
- return (toInterface(parts[0]), IUndefinedContext)
+ return (toInterface(parts[0]), conface)
except:
- return (IUndefinedKeyface, IUndefinedContext)
+ return (keyface, conface)
else:
try:
@@ -198,5 +207,5 @@
return (keyface, toInterface(parts[1]))
except:
- return (keyface, IUndefinedContext)
+ return (keyface, conface)
Modified: zope.generic/trunk/src/zope/generic/informationprovider/README.txt
===================================================================
--- zope.generic/trunk/src/zope/generic/informationprovider/README.txt 2006-05-23 22:47:16 UTC (rev 68259)
+++ zope.generic/trunk/src/zope/generic/informationprovider/README.txt 2006-05-24 08:50:21 UTC (rev 68260)
@@ -39,8 +39,7 @@
>>> class ISpecialContext(interface.Interface):
... pass
- >>> from zope.interface import Interface
- >>> class IMyFoo(Interface):
+ >>> class IMyFoo(interface.Interface):
... pass
>>> registerDirective('''
@@ -79,7 +78,7 @@
If no information provider is available for a certain interface the default
value is returned. If no default is defined None is returned:
- >>> class IMyBar(Interface):
+ >>> class IMyBar(interface.Interface):
... pass
>>> default = object()
@@ -397,172 +396,3 @@
>>> api.acquireInformationProvider(bar)
<GlobalInformationProvider IBar at IUndefinedContext>
-
-
-Ini-file based configurations for an information provider
----------------------------------------------------------
-
-The configuration file holds several configuration in the ini-file style.
-
- >>> from zope.interface import Interface
- >>> from zope.schema import Text, TextLine
-
- >>> class IOneConfiguration(Interface):
- ... textLine = TextLine(title=u'TextLine')
- ... text = Text(title=u'Text', required=False, default=u'Bla\\n')
-
- >>> registerDirective('''
- ... <generic:configuration
- ... keyface="example.IOneConfiguration"
- ... />
- ... ''')
-
- >>> from zope.schema import Bool, Int
-
- >>> class IOtherConfiguration(Interface):
- ... bool = Bool(title=u'Bool')
- ... int = Int(title=u'Int', required=False, default=42)
-
- >>> registerDirective('''
- ... <generic:configuration
- ... keyface="example.IOtherConfiguration"
- ... />
- ... ''')
-
- >>> from zope.schema import Dict, Object, Tuple
-
- >>> class INestedConfiguration(Interface):
- ... one = Object(title=u'One configuration', schema=IOneConfiguration)
- ... optionalother = Object(title=u'Other configuration', required=False, schema=IOtherConfiguration)
- ... requiredother = Object(title=u'Other configuration', schema=IOtherConfiguration)
- ... tuple = Tuple(title=u'Tuple of Int', value_type=Int())
- ... dict = Dict(title=u'Dict of TextLine', value_type=TextLine())
-
- >>> registerDirective('''
- ... <generic:configuration
- ... keyface="example.INestedConfiguration"
- ... />
- ... ''')
-
- >>> import os, tempfile
- >>> temp_dir = tempfile.mkdtemp()
- >>> iniFile = os.path.join(temp_dir, 'example.ini')
- >>> open(iniFile, 'w').write('''
- ... [example.IOneConfiguration]
- ... textline = Foo
- ... text : Bla bla bla bla.
- ...
- ... [example.IOtherConfiguration]
- ... bool = True
- ... int = 77
- ...
- ... [example.INestedConfiguration]
- ... one.textline = Bingo
- ... one.text = Lotto.
- ... requiredother.bool = False
- ... tuple.0 = 2
- ... tuple.1 = 3
- ... tuple.2 = 19
- ... dict.roger = ineichen
- ... dict.dominik = huber
- ... dict.daniel = meier
- ... ''')
-
-Such a file can be used for the configuration initializiation. Therefore you
-have to declare one or more files within the iniFiles attribute of the
-informaiton subdirective of the informationProvider direcitve:
-
- >>> registerDirective('''
- ... <generic:informationProvider
- ... keyface="example.IFoo"
- ... conface="example.ISpecialContext"
- ... >
- ... <information
- ... iniFiles="%s"
- ... />
- ... </generic:informationProvider>
- ... ''' % iniFile)
-
- >>> one_config = api.getInformation(IOneConfiguration, IFoo, ISpecialContext)
- >>> one_config.textLine
- u'Foo'
-
- >>> one_config.text
- u'Bla bla bla bla.'
-
- >>> other_config = api.getInformation(IOtherConfiguration, IFoo, ISpecialContext)
- >>> other_config.bool
- True
-
- >>> other_config.int
- 77
-
- >>> nested_config = api.getInformation(INestedConfiguration, IFoo, ISpecialContext)
- >>> nested_config.one.textLine
- u'Bingo'
-
- >>> nested_config.one.text
- u'Lotto.'
-
- >>> nested_config.optionalother is None
- True
-
- >>> nested_config.requiredother.bool
- False
-
- >>> nested_config.requiredother.int
- 42
-
- >>> nested_config.tuple
- (2, 3, 19)
-
- >>> nested_config.dict
- {'dominik': u'huber', 'daniel': u'meier', 'roger': u'ineichen'}
-
-Ini-file based configurations for an multi information provider
----------------------------------------------------------------
-
- >>> temp_dir = tempfile.mkdtemp()
- >>> iniFile = os.path.join(temp_dir, 'example2.ini')
- >>> open(iniFile, 'w').write('''
- ... [example.IOneConfiguration:example.IBar at example.IMyContext]
- ... textline = Gaga
- ... text : Bla bla bla bla.
- ...
- ... [example.IOneConfiguration:example.IBarFoo at example.IMyContext]
- ... textline = Gugu
- ... text : Bla bla bla bla.
- ...
- ... [example.INestedConfiguration:example.IBar at example.IMyContext]
- ... one.textline = Gogo
- ... one.text = Lotto.
- ... requiredother.bool = False
- ... tuple.0 = 2
- ... tuple.1 = 3
- ... tuple.2 = 19
- ... dict.roger = ineichen
- ... dict.dominik = huber
- ... dict.daniel = meier
- ... ''')
-
-Such a file can be used for the configuration initializiation. Therefore you
-have to declare one or more files within the iniFiles attribute of the
-informaiton directive:
-
- >>> registerDirective('''
- ... <generic:multiInformationProviders
- ... iniFiles="%s"
- ... />
- ... ''' % iniFile)
-
- >>> api.getInformation(IOneConfiguration, IBar, IMyContext).textLine
- u'Gaga'
-
- >>> api.getInformation(IOneConfiguration, IBarFoo, IMyContext).textLine
- u'Gugu'
-
- >>> api.getInformation(INestedConfiguration, IBar, IMyContext).one.textLine
- u'Gogo'
-
- >>> import shutil
- >>> shutil.rmtree(temp_dir)
Modified: zope.generic/trunk/src/zope/generic/informationprovider/api.py
===================================================================
--- zope.generic/trunk/src/zope/generic/informationprovider/api.py 2006-05-23 22:47:16 UTC (rev 68259)
+++ zope.generic/trunk/src/zope/generic/informationprovider/api.py 2006-05-24 08:50:21 UTC (rev 68260)
@@ -39,7 +39,6 @@
from zope.generic.informationprovider.base import GlobalInformationProvider
from zope.generic.informationprovider.base import LocalInformationProvider
from zope.generic.informationprovider.base import UserDescription
-from zope.generic.informationprovider.helper import toConfigFaceTriple
from zope.generic.informationprovider.metaconfigure import ensureInformationProvider
from zope.generic.informationprovider.metaconfigure import getInformationProvider
Deleted: zope.generic/trunk/src/zope/generic/informationprovider/helper.py
===================================================================
--- zope.generic/trunk/src/zope/generic/informationprovider/helper.py 2006-05-23 22:47:16 UTC (rev 68259)
+++ zope.generic/trunk/src/zope/generic/informationprovider/helper.py 2006-05-24 08:50:21 UTC (rev 68260)
@@ -1,52 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2005, 2006 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-
-"""
-$Id$
-"""
-
-__docformat__ = 'restructuredtext'
-
-from zope.generic.face import IUndefinedContext
-from zope.generic.face import IUndefinedKeyface
-from zope.generic.face.api import toFaceTuple
-from zope.generic.face.api import toInterface
-
-
-
-
-def toConfigFaceTriple(identifier):
- """Split configface:keyface at conface to (configface, keyface, conface).
-
- >>> from zope.interface import Interface
-
- >>> class IA(Interface):
- ... pass
-
- >>> from zope.generic.face.api import toDottedName
-
- >>> api.toConfigFaceTriple(toDottedName(IA) + ':')
- (<InterfaceClass example.IA>, <....IUndefinedKeyface>, <....IUndefinedContext>)
- """
-
- parts = identifier.split(':')
-
- if len(parts) == 1:
- return (toInterface(parts[0]), IUndefinedKeyface, IUndefinedContext)
-
- else:
- keyface, conface = toFaceTuple(parts[1])
- return (toInterface(parts[0]), keyface, conface)
-
-
Modified: zope.generic/trunk/src/zope/generic/informationprovider/meta.zcml
===================================================================
--- zope.generic/trunk/src/zope/generic/informationprovider/meta.zcml 2006-05-23 22:47:16 UTC (rev 68259)
+++ zope.generic/trunk/src/zope/generic/informationprovider/meta.zcml 2006-05-24 08:50:21 UTC (rev 68260)
@@ -17,12 +17,6 @@
</meta:complexDirective>
- <meta:directive
- name="multiInformationProviders"
- schema=".metadirectives.IMultiInformationProvidersDirective"
- handler=".metaconfigure.multiInformationProvidersDirective"
- />
-
</meta:directives>
</configure>
Modified: zope.generic/trunk/src/zope/generic/informationprovider/metaconfigure.py
===================================================================
--- zope.generic/trunk/src/zope/generic/informationprovider/metaconfigure.py 2006-05-23 22:47:16 UTC (rev 68259)
+++ zope.generic/trunk/src/zope/generic/informationprovider/metaconfigure.py 2006-05-24 08:50:21 UTC (rev 68260)
@@ -18,8 +18,6 @@
__docformat__ = 'restructuredtext'
-from ConfigParser import SafeConfigParser
-
from zope.annotation import IAnnotations
from zope.component import getUtility
from zope.component import provideUtility
@@ -27,12 +25,6 @@
from zope.component.interface import provideInterface
from zope.configuration.exceptions import ConfigurationError
from zope.interface import alsoProvides
-from zope.schema.interfaces import IDict
-from zope.schema.interfaces import IFromUnicode
-from zope.schema.interfaces import IList
-from zope.schema.interfaces import IObject
-from zope.schema.interfaces import ISequence
-from zope.schema.interfaces import ITuple
from zope.generic.configuration import IConfigurations
from zope.generic.configuration import IConfigurationType
@@ -48,7 +40,6 @@
from zope.generic.face.api import toInterface
from zope.generic.informationprovider.base import GlobalInformationProvider
-from zope.generic.informationprovider.helper import toConfigFaceTriple
@@ -123,125 +114,7 @@
annotations[annotation_key] = annotation
-def nestedConfigurationData(configparser, section, keyface, prefix=''):
- """Nested configuration support."""
- missedArguments = []
- data = {}
-
- for name in keyface:
- field = keyface[name]
- lookup_name = prefix + name.lower()
- # evalutate name: config parser options are always lower case
- try:
- value = configparser.get(section, lookup_name)
- try:
- data[name] = field.fromUnicode(unicode(value))
-
- except:
- data[name] = IFromUnicode(field).fromUnicode(unicode(value))
-
- except:
- if IObject.providedBy(field) and IConfigurationType.providedBy(field.schema):
- subkeyface = field.schema
- try:
- subdata = nestedConfigurationData(configparser, section, subkeyface, lookup_name + '.')
- except:
- subdata = {}
-
- if subdata or field.required is True:
- try:
- data[name] = createConfiguration(subkeyface, subdata)
- continue
- except:
- if field.required is False:
- continue
-
- elif ISequence.providedBy(field):
- counter = 0
- subfield = field.value_type
- sequence = []
- while True:
- try:
- value = configparser.get(section, lookup_name + '.' + str(counter))
-
- try:
- sequence.append(subfield.fromUnicode(unicode(value)))
-
- except:
- sequence.append(IFromUnicode(subfield).fromUnicode(unicode(value)))
-
- except:
- break
-
- counter += 1
-
- if sequence or field.required is True:
- if ITuple.providedBy(field):
- data[name] = tuple(sequence)
-
- else:
- data[name] = sequence
-
- continue
-
- elif IDict.providedBy(field):
- sublookup_name = lookup_name + '.'
- sublookup_len = len(sublookup_name)
- subfield = field.value_type
- subdict = {}
- for key, value in configparser.items(section):
- if len(key) > sublookup_len and key.startswith(sublookup_name):
- subkey = key[sublookup_len:]
- if subkey.count('.'):
- raise NotImplementedError('Not supported yet!')
-
- try:
- value = configparser.get(section, key)
-
- try:
- subdict[subkey] = subfield.fromUnicode(unicode(value))
-
- except:
- subdict[subkey] = IFromUnicode(subfield).fromUnicode(unicode(value))
-
- except:
- break
-
- if subdict or field.required is True:
- data[name] = subdict
- continue
-
- if field.required is True:
- missedArguments.append(lookup_name)
-
- if missedArguments:
- raise TypeError("__init__ requires '%s' of '%s'." % (', '.join(missedArguments), keyface.__name__))
-
- return data
-
-
-
-_marker = object()
-
-def iniFileToConfiguration(path, strict=True):
- """Parse ini file to an iterator over keyface, configuration pairs."""
-
- configparser = SafeConfigParser()
- configparser.read(path)
-
- for section in configparser.sections():
-
- if strict:
- configuration, keyface, conface = toConfigFaceTriple(section)
- yield (configuration, keyface, conface, nestedConfigurationData(configparser, section, configuration))
-
- else:
- configuration = toInterface(section)
- yield (configuration, nestedConfigurationData(configparser, section, configuration))
-
-
-
class InformationProviderDirective(object):
"""Provide a new information provider."""
@@ -276,25 +149,11 @@
"Handle empty/simple declaration."
return ()
- def information(self, _context, keyface=None, configuration=None, key=None, annotation=None, iniFiles=()):
+ def information(self, _context, keyface=None, configuration=None, key=None, annotation=None):
"""Add a configuration to the information provider."""
-
- # handle ini files
- if iniFiles:
- if keyface or configuration or key or annotation:
- raise ConfigurationError('Attribute iniFiles does not allow other attributes.')
-
- for path in iniFiles:
- for configuration, data in iniFileToConfiguration(path, False):
- _context.action(
- discriminator = (
- 'informationprovider.configuration', self._keyface, self._conface, configuration),
- callable = provideConfiguration,
- args = (self._keyface, self._conface, configuration, data),
- )
# handle configuration
- elif keyface and configuration is not None:
+ if keyface and configuration is not None:
# preconditions
if not (keyface.providedBy(configuration) or type(configuration) is dict):
raise ConfigurationError('Data attribute must provide %s.' % keyface.__name__)
Modified: zope.generic/trunk/src/zope/generic/informationprovider/metadirectives.py
===================================================================
--- zope.generic/trunk/src/zope/generic/informationprovider/metadirectives.py 2006-05-23 22:47:16 UTC (rev 68259)
+++ zope.generic/trunk/src/zope/generic/informationprovider/metadirectives.py 2006-05-24 08:50:21 UTC (rev 68260)
@@ -55,23 +55,6 @@
-class IIniFileDirective(Interface):
- """Ini-file based configurations for multi information provider."""
-
- iniFiles = Tokens(
- title=_('*.ini-like File'),
- description=_('Parse ((ConfigParser) configuration and face interfaces '
- 'from sections resolving the pattern '
- 'configuration:keyface at conface. The configuration data '
- 'are retrieved from the corresponding options using '
- 'IFromUnicode(field).fromUnicode() from the configuration '
- 'fields.'),
- required=False,
- value_type=Path(constraint=lambda v: v.endswith('.ini'))
- )
-
-
-
class IDescriptionDirective(IKeyfaceDirective):
"""Base information provider attributes."""
@@ -94,7 +77,7 @@
-class IInformationSubdirective(IIniFileDirective):
+class IInformationSubdirective(Interface):
"""Declare a certain information of an information provider."""
keyface = GlobalInterface(
@@ -121,8 +104,3 @@
description=_('Annotation component expected undert the key.'),
required=False
)
-
-
-class IMultiInformationProvidersDirective(IIniFileDirective):
- """Ini-file based configurations for multi information providers."""
-
Modified: zope.generic/trunk/src/zope/generic/informationprovider/testing.py
===================================================================
--- zope.generic/trunk/src/zope/generic/informationprovider/testing.py 2006-05-23 22:47:16 UTC (rev 68259)
+++ zope.generic/trunk/src/zope/generic/informationprovider/testing.py 2006-05-24 08:50:21 UTC (rev 68260)
@@ -50,12 +50,6 @@
import zope.generic.informationprovider
XMLConfig('meta.zcml', zope.generic.informationprovider)()
-
- from zope.annotation import IAttributeAnnotatable
- from zope.generic.configuration import IAttributeConfigurable
- from zope.generic.informationprovider.api import GlobalInformationProvider
- from zope.generic.informationprovider.api import LocalInformationProvider
-
def tearDown(doctest=None):
pass
Modified: zope.generic/trunk/src/zope/generic/informationprovider/tests.py
===================================================================
--- zope.generic/trunk/src/zope/generic/informationprovider/tests.py 2006-05-23 22:47:16 UTC (rev 68259)
+++ zope.generic/trunk/src/zope/generic/informationprovider/tests.py 2006-05-24 08:50:21 UTC (rev 68260)
@@ -40,14 +40,6 @@
def test_suite():
return unittest.TestSuite((
doctest.DocTestSuite('zope.generic.informationprovider.base'),
- doctest.DocTestSuite('zope.generic.informationprovider.helper',
- setUp=testing.placelesssetup.setUp,
- tearDown=testing.placelesssetup.tearDown,
- globs={'component': component, 'interface': interface,
- 'registerDirective': registerDirective,
- 'testing': testing, 'api': api},
- optionflags=doctest.NORMALIZE_WHITESPACE+
- doctest.ELLIPSIS),
doctest.DocFileSuite('README.txt',
setUp=testing.placelesssetup.setUp,
tearDown=testing.placelesssetup.tearDown,
More information about the Checkins
mailing list