[Zope-Checkins] CVS: Zope3/lib/python/Zope/Configuration/tests - Directives.py:1.1.2.1 testMultipleXML.py:1.1.2.1 testMeta.py:1.1.2.5 testXML.py:1.1.2.4

Jim Fulton jim@zope.com
Thu, 3 Jan 2002 14:29:25 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Configuration/tests
In directory cvs.zope.org:/tmp/cvs-serv16747/Configuration/tests

Modified Files:
      Tag: Zope-3x-branch
	testMeta.py testXML.py 
Added Files:
      Tag: Zope-3x-branch
	Directives.py testMultipleXML.py 
Log Message:
Refactored configuration framework:

- Configuration directives must be written to a 
  a different framework. See
  ConfigurationDirectiveInterfaces.

- Configuration directives now don't take actions immediately.
  Instead, they return a sequence of discriminators and callables
  objects with arguments.  This allows configuration to be defered to
  allow overriding and conflct detection.

- Can now detect conflicting directives

- Can override directives. Directives in including configuration files
  override directives in included files. Conflicting directives are
  decided based on discriminators.

- Added new directives for defining directives. All directives, except
  for a few bootstrap irectives, are now configurable in the
  configuration file. This makes directives a little more discoverable
  and facilitates extension of directives.


=== Added File Zope3/lib/python/Zope/Configuration/tests/Directives.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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
# 
##############################################################################
"""
Test class for use by test modules

Revision information: $Id: Directives.py,v 1.1.2.1 2002/01/03 19:29:24 jim Exp $
"""

from Zope.Configuration.ConfigurationDirectiveInterfaces \
     import INonEmptyDirective

protections=[]

class protectClass:

    __implements__ = INonEmptyDirective

    def __init__(self, name, permission=None, methods=None, method=None):
        self._name=name
        self._permission=permission
        self._methods=methods
        self._method=method
        self._children=[]

    def __call__(self):
        if not self._children:
            p = self._name, self._permission, self._method, self._methods
            d = self._name, self._method, self._methods
            return [(d, protections.append, (p,))]
        else:
            return ()
            
    def protect(self, permission=None, method=None, methods=None):
        if permission is None: permission=self._permission
        if permission is None: raise 'no perm'
        p=self._name, permission, method, methods
        d=self._name, method, methods
        self._children.append(p)
        return [(d, protections.append, (p,))]

done = []

def doit(name):
    return [('d', done.append, (name,))]

def clearDirectives():
    del protections[:]
    del done[:]


=== Added File Zope3/lib/python/Zope/Configuration/tests/testMultipleXML.py ===
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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
# 
##############################################################################

import unittest, sys, os
from tempfile import mktemp
from Zope.Configuration.tests.Directives \
     import protections, clearDirectives, done
from Zope.Configuration.xmlconfig import XMLConfig


template = """<zopeConfigure
   xmlns='http://namespaces.zope.org/zope'
   xmlns:test='http://www.zope.org/NS/Zope3/test'>
   %s
   %s
   </zopeConfigure>"""

ns='http://www.zope.org/NS/Zope3/test'


class Test(unittest.TestCase):

    def tearDown(self):
        from Zope.Configuration.meta import _clear
        _clear()
        clearDirectives()
        
    def testNormal(self):
        f2=tfile(template % ('',
            '''
            <test:protectClass
            name=".Contact" permission="splat" method="update"
            />
            <test:protectClass
            name=".Contact" permission="splat" method="update2"
            />            
            '''), 'f2')

        f1=tfile(template % (
            '''<directive name="protectClass" namespace="%s"
            handler="Zope.Configuration.tests.Directives.protectClass">
            <subdirective name="protect" namespace="%s" />
            </directive>''' % (ns, ns),
            '''
            <test:protectClass
            name=".Contact" permission="splat" method="update"
            />
            <include file="%s"/>
            ''' % f2), 'f1')

        XMLConfig(str(f1))()
        
        self.assertEquals(protections, [
            (".Contact", "splat", 'update', None),
            (".Contact", "splat", 'update2', None),
            ])
        
    def testConflicting(self):
        f2=tfile(template % ('',
            '''
            <test:protectClass
            name=".Contact" permission="splat" method="update"
            />
            <test:protectClass
            name=".Contact" permission="splat" method="update2"
            />            
            '''), 'f2')
        f3=tfile(template % ('',
            '''
            <test:protectClass
            name=".Contact" permission="splat" method="update2"
            />            
            '''), 'f3')

        f1=tfile(template % (
            '''<directive name="protectClass" namespace="%s"
            handler="Zope.Configuration.tests.Directives.protectClass">
            <subdirective name="protect" namespace="%s" />
            </directive>''' % (ns, ns),
            '''
            <test:protectClass
            name=".Contact" permission="splat" method="update"
            />
            <include file="%s"/>
            <include file="%s"/>
            ''' % (f2, f3)), 'f1')

        x=XMLConfig(str(f1))

        from Zope.Configuration.xmlconfig import ZopeConfigurationConflictError

        self.assertRaises(ZopeConfigurationConflictError, x)
        
        self.assertEquals(protections, [])
        


class tfile:
    
    def __init__(self, string, suffix):
        self.name = mktemp(suffix)
        file = open(self.name, 'w')
        file.write(string)
        file.close()

    def __str__(self): return self.name

    def __del__(self): os.remove(self.name)

def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Zope3/lib/python/Zope/Configuration/tests/testMeta.py 1.1.2.4 => 1.1.2.5 ===
 
 import unittest
+from Zope.Configuration.tests.Directives \
+     import protectClass, protections, doit, done, clearDirectives
 
 ns='http://www.zope.org/NS/Zope3/test'
 
@@ -17,72 +19,66 @@
     def tearDown(self):
         from Zope.Configuration.meta import _clear
         _clear()
-        global protections
-        protections=[]
+        clearDirectives()
         
     def testDirective(self):
-        from Zope.Configuration.meta import register, execute, InvalidDirective
-        
-        x=[]
-        def d(name, x=x): x.append(name)
+        from Zope.Configuration.meta \
+             import register, registersub, begin, end, InvalidDirective
 
-        self.assertRaises(InvalidDirective, execute, ns, 'doit')
+        self.assertRaises(InvalidDirective, begin, None, (ns, 'doit'))
 
-        register(ns, 'doit', d)
+        register((ns, 'doit'), doit)
 
-        self.failUnless(execute(ns, 'doit', name='splat') is None)
+        subs = begin(None, (ns, 'doit'), name='splat')
+        (des, callable, args, kw), = end(subs)
+        self.assertEqual(des, 'd')
+        callable(*args)
 
-        self.failUnless(x==['splat'])
+        self.failUnless(done==['splat'])
         
     def testSimpleComplexDirective(self):
-        from Zope.Configuration.meta import register, execute, InvalidDirective
+        from Zope.Configuration.meta \
+             import register, registersub, begin, end, InvalidDirective
 
-        register(ns, 'protectClass', protectClass)
+        subs = register((ns, 'protectClass'), protectClass)
+        registersub(subs, (ns, 'protect'))
 
-        o=execute(ns, 'protectClass',
-                  name=".Contact", permission="splat", method='update')
+        subs=begin(None, (ns, 'protectClass'),
+                   name=".Contact", permission="splat", method='update')
 
-        o()
+        (des, callable, args, kw), = end(subs)
+        self.assertEqual(des, ('.Contact', 'update', None))
+        callable(*args)
 
         self.assertEquals(protections, [(".Contact", "splat", 'update', None)])
         
     def testComplexDirective(self):
-        from Zope.Configuration.meta import register, execute, InvalidDirective
+        from Zope.Configuration.meta \
+             import register, registersub, begin, sub, end, InvalidDirective
+
+        subs = register((ns, 'protectClass'), protectClass)
+        registersub(subs, (ns, 'protect'))
+
+        subs = begin(None, (ns, 'protectClass'), name=".Contact")
 
-        register(ns, 'protectClass', protectClass)
+        actions = end(sub(subs, (ns, 'protect'),
+                          permission='edit', method='update'))
+        (des, callable, args, kw), = actions
+        self.assertEqual(des, ('.Contact', 'update', None))
+        callable(*args)
+
+        actions = end(sub(subs, (ns, 'protect'),
+                          permission='view', methods='name, email'))
+        (des, callable, args, kw), = actions
+        self.assertEqual(des, ('.Contact', None, 'name, email'))
+        callable(*args)
 
-        o=execute(ns, 'protectClass', name=".Contact")
-        o.protect(permission="edit", method='update')
-        o.protect(permission="view", methods='name, email')
-        o()
+        self.assertEqual(tuple(end(subs)), ())
 
         self.assertEquals(protections, [
             (".Contact", "edit", 'update', None),
             (".Contact", "view", None, 'name, email'),
             ])
-
-protections=[]
-
-class protectClass:
-
-    def __init__(self, name, permission=None, methods=None, method=None):
-        self._name=name
-        self._permission=permission
-        self._methods=methods
-        self._method=method
-        self._children=[]
-
-    def __call__(self):
-        if not self._children:
-            p=self._name, self._permission, self._method, self._methods
-            protections.append(p)
-            
-    def protect(self, permission=None, method=None, methods=None):
-        if permission is None: permission=self._permission
-        if permission is None: raise 'no perm'
-        p=self._name, permission, method, methods
-        protections.append(p)
-        self._children.append(p)
 
 def test_suite():
     loader=unittest.TestLoader()


=== Zope3/lib/python/Zope/Configuration/tests/testXML.py 1.1.2.3 => 1.1.2.4 ===
 from cStringIO import StringIO
 from Zope.Configuration.xmlconfig import xmlconfig, ZopeXMLConfigurationError
+from Zope.Configuration.tests.Directives \
+     import protections, clearDirectives, done
 
 template = """<zopeConfigure
    xmlns='http://namespaces.zope.org/zope'
    xmlns:test='http://www.zope.org/NS/Zope3/test'>
    %s
+   %s
    </zopeConfigure>"""
 
 
@@ -23,50 +26,47 @@
     def tearDown(self):
         from Zope.Configuration.meta import _clear
         _clear()
-        global protections
-        protections=[]
+        clearDirectives()
         
     def testDirective(self):
-        from Zope.Configuration.meta import register, execute, InvalidDirective
-        
-        x=[]
-        def d(name): x.append(name)
-
-        self.assertRaises(InvalidDirective, execute, ns, 'doit')
-
-        register(ns, 'doit', d)
-
-
         xmlconfig(StringIO(
-            template % '<test:doit name="splat" />'
-            ))
+            template % (
+            '''<directive name="doit" namespace="%s"
+                          handler="Zope.Configuration.tests.Directives.doit" />
+                          ''' % ns,
+            '<test:doit name="splat" />'
+            )))
 
-        self.assertEqual(x, ['splat'])
+        self.assertEqual(done, ['splat'])
         
     def testSimpleComplexDirective(self):
-        from Zope.Configuration.meta import register, execute, InvalidDirective
-
-        register(ns, 'protectClass', protectClass)
-
         xmlconfig(StringIO(
-            template % '''<test:protectClass
+            template % (
+            '''<directive name="protectClass" namespace="%s"
+            handler="Zope.Configuration.tests.Directives.protectClass">
+                  <subdirective name="protect" namespace="%s" />
+               </directive>
+                          ''' % (ns, ns),
+            '''<test:protectClass
               name=".Contact" permission="splat" method="update"
               />'''
-            ))
+            )))
 
         self.assertEquals(protections, [(".Contact", "splat", 'update', None)])
         
     def testComplexDirective(self):
-        from Zope.Configuration.meta import register, execute, InvalidDirective
-
-        register(ns, 'protectClass', protectClass)
-
         xmlconfig(StringIO(
-            template % '''<test:protectClass name=".Contact">
+            template % (
+            '''<directive name="protectClass" namespace="%s"
+            handler="Zope.Configuration.tests.Directives.protectClass">
+                  <subdirective name="protect" namespace="%s" />
+               </directive>
+                          ''' % (ns, ns),
+            '''<test:protectClass name=".Contact">
               <test:protect permission="edit" method='update' />
               <test:protect permission="view" methods='name, email' />
             </test:protectClass>'''
-            ))
+            )))
 
         self.assertEquals(protections, [
             (".Contact", "edit", 'update', None),
@@ -74,63 +74,48 @@
             ])
         
     def testBadNoPrefixComplexDirective(self):
-        from Zope.Configuration.meta import register, execute, InvalidDirective
-
-        register(ns, 'protectClass', protectClass)
 
         self.assertRaises(
             ZopeXMLConfigurationError,
             xmlconfig,
             StringIO(
-            template % '''<test:protectClass name=".Contact">
+            template % (
+            '''<directive name="protectClass" namespace="%s"
+                          handler="Zope.Configuration.tests.Directives.protectClass">
+                  <subdirective name="protect" namespace="%s" />
+               </directive>
+                          ''' % (ns, ns),
+
+            '''<test:protectClass name=".Contact">
               <test:protect permission="edit" method='update' />
               <protect permission="view" methods='name, email' />
               </test:protectClass>'''
-            )
-            )
+            )))
         
     def testBadPrefixComplexDirective(self):
-        from Zope.Configuration.meta import register, execute, InvalidDirective
-
-        register(ns, 'protectClass', protectClass)
 
         try:
             xmlconfig(
                 StringIO(
-                template % '''<test:protectClass name=".Contact">
+                template % (
+            '''<directive name="protectClass" namespace="%s"
+                          handler="Zope.Configuration.tests.Directives.protectClass">
+                  <subdirective name="protect" namespace="%s" />
+               </directive>
+                          ''' % (ns, ns),
+
+                '''<test:protectClass name=".Contact">
                 <test2:protect permission="edit" method='update' />
                 </test:protectClass>'''
-                ))
+                )))
         except ZopeXMLConfigurationError, v:
             self.assertEqual(
                 str(v),
-                'Namespace missmatch at line 5 column 16 of <string>')
+                "InvalidDirective: (None, u'test2:protect') at line 10 "
+                "column 16 of <string>")
         else:
             self.fail('Should have raised ZopeXMLConfigurationError')
         
-
-protections=[]
-
-class protectClass:
-
-    def __init__(self, name, permission=None, methods=None, method=None):
-        self._name=name
-        self._permission=permission
-        self._methods=methods
-        self._method=method
-        self._children=[]
-
-    def __call__(self):
-        if not self._children:
-            p=self._name, self._permission, self._method, self._methods
-            protections.append(p)
-            
-    def protect(self, permission=None, method=None, methods=None):
-        if permission is None: permission=self._permission
-        if permission is None: raise 'no perm'
-        p=self._name, permission, method, methods
-        protections.append(p)
-        self._children.append(p)
 
 def test_suite():
     loader=unittest.TestLoader()