[Checkins] SVN: grokcore.view/trunk/ do not rebind inline_template_registry and file_template_registry upon ZCA cleanup, as the names could be imported elsewhere, for example where the tests suites are built and a test setup would like to register an ignore pattern. Clean the registries instead.

Jan-Wijbrand Kolman janwijbrand at gmail.com
Mon Feb 28 09:36:51 EST 2011


Log message for revision 120604:
  do not rebind inline_template_registry and file_template_registry upon ZCA cleanup, as the names could be imported elsewhere, for example where the tests suites are built and a test setup would like to register an ignore pattern. Clean the registries instead.

Changed:
  U   grokcore.view/trunk/CHANGES.txt
  U   grokcore.view/trunk/src/grokcore/view/configure.zcml
  U   grokcore.view/trunk/src/grokcore/view/meta-minimal.zcml
  U   grokcore.view/trunk/src/grokcore/view/meta.zcml
  U   grokcore.view/trunk/src/grokcore/view/templatereg.py
  U   grokcore.view/trunk/src/grokcore/view/testing.py
  U   grokcore.view/trunk/src/grokcore/view/tests/test_all.py
  A   grokcore.view/trunk/src/grokcore/view/zcml.py

-=-
Modified: grokcore.view/trunk/CHANGES.txt
===================================================================
--- grokcore.view/trunk/CHANGES.txt	2011-02-28 14:12:13 UTC (rev 120603)
+++ grokcore.view/trunk/CHANGES.txt	2011-02-28 14:36:50 UTC (rev 120604)
@@ -14,6 +14,10 @@
   subclassing view components that use the grok.template() directive from other
   packages.
 
+- Add a new ZCML directive, ``ignoreTemplates`` that let you configure
+  which template filename pattern should be ignored by the template
+  registry.
+
 2.3 (2011-01-04)
 ----------------
 

Modified: grokcore.view/trunk/src/grokcore/view/configure.zcml
===================================================================
--- grokcore.view/trunk/src/grokcore/view/configure.zcml	2011-02-28 14:12:13 UTC (rev 120603)
+++ grokcore.view/trunk/src/grokcore/view/configure.zcml	2011-02-28 14:36:50 UTC (rev 120604)
@@ -6,4 +6,16 @@
 
   <!-- ZPT support -->
   <grok:grok package=".templatereg" />
+
+  <!-- Configure the ignore patterns for template association -->
+  <grok:ignoreTemplates pattern="~$" />
+  <grok:ignoreTemplates pattern="^\." />
+
+  <!-- Ignore chameleon extra files -->
+  <!-- XXX: this should move to megrok.chameleon -->
+  <grok:ignoreTemplates pattern="\.cpt\.py$" />
+  <grok:ignoreTemplates pattern="\.cpt\.pyc$" />
+  <grok:ignoreTemplates pattern="\.cpt\.pyo$" />
+  <grok:ignoreTemplates pattern="\.cpt\.cache$" />
+
 </configure>

Modified: grokcore.view/trunk/src/grokcore/view/meta-minimal.zcml
===================================================================
--- grokcore.view/trunk/src/grokcore/view/meta-minimal.zcml	2011-02-28 14:12:13 UTC (rev 120603)
+++ grokcore.view/trunk/src/grokcore/view/meta-minimal.zcml	2011-02-28 14:36:50 UTC (rev 120604)
@@ -1,10 +1,20 @@
 <configure
-    xmlns="http://namespaces.zope.org/zope"
-    xmlns:grok="http://namespaces.zope.org/grok">
+   xmlns="http://namespaces.zope.org/zope"
+   xmlns:meta="http://namespaces.zope.org/meta"
+   xmlns:grok="http://namespaces.zope.org/grok">
+
   <include package="grokcore.component" file="meta.zcml" />
   <include package="grokcore.security" file="meta.zcml" />
 
-  <!-- Only load view and template grokkers --> 
+  <meta:directives namespace="http://namespaces.zope.org/grok">
+    <meta:directive
+        name="ignoreTemplates"
+        schema=".zcml.IIgnoreTemplatesDirective"
+        handler=".zcml.ignoreTemplates"
+        />
+  </meta:directives>
+
+  <!-- Only load view and template grokkers -->
   <grok:grok package=".meta.views" />
   <grok:grok package=".meta.templates" />
 

Modified: grokcore.view/trunk/src/grokcore/view/meta.zcml
===================================================================
--- grokcore.view/trunk/src/grokcore/view/meta.zcml	2011-02-28 14:12:13 UTC (rev 120603)
+++ grokcore.view/trunk/src/grokcore/view/meta.zcml	2011-02-28 14:36:50 UTC (rev 120604)
@@ -1,7 +1,18 @@
 <configure
-    xmlns="http://namespaces.zope.org/zope"
-    xmlns:grok="http://namespaces.zope.org/grok">
+   xmlns="http://namespaces.zope.org/zope"
+   xmlns:meta="http://namespaces.zope.org/meta"
+   xmlns:grok="http://namespaces.zope.org/grok">
+
   <include package="grokcore.component" file="meta.zcml" />
   <include package="grokcore.security" file="meta.zcml" />
+
+  <meta:directives namespace="http://namespaces.zope.org/grok">
+    <meta:directive
+        name="ignoreTemplates"
+        schema=".zcml.IIgnoreTemplatesDirective"
+        handler=".zcml.ignoreTemplates"
+        />
+  </meta:directives>
+
   <grok:grok package=".meta" />
 </configure>

Modified: grokcore.view/trunk/src/grokcore/view/templatereg.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/templatereg.py	2011-02-28 14:12:13 UTC (rev 120603)
+++ grokcore.view/trunk/src/grokcore/view/templatereg.py	2011-02-28 14:36:50 UTC (rev 120604)
@@ -1,20 +1,45 @@
+##############################################################################
+#
+# Copyright (c) 2006-2007 Zope Foundation 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.
+#
+##############################################################################
+
 import os
 import warnings
+import re
+
 import zope.component
 import grokcore.component
 import grokcore.view
 from martian.scan import module_info_from_dotted_name
 from martian.error import GrokError
-from grokcore.view.interfaces import ITemplate, ITemplateFileFactory, TemplateLookupError
+from grokcore.view.interfaces import ITemplate, ITemplateFileFactory
+from grokcore.view.interfaces import TemplateLookupError
 from grokcore.view.components import PageTemplate
 
 
 class InlineTemplateRegistry(object):
+    """Registry managing all inline template files.
+    """
+
+    _reg = None
+    _unassociated = None
+
     def __init__(self):
+        self.clear()
+
+    def clear(self):
         self._reg = {}
         self._unassociated = set()
 
-
     def register_inline_template(self, module_info, template_name, template):
         # verify no file template got registered with the same name
         try:
@@ -35,7 +60,8 @@
         self._unassociated.add((module_info.dotted_name, template_name))
 
     def associate(self, module_info, template_name):
-        # Two views in the same module should be able to use the same inline template
+        # Two views in the same module should be able to use the same
+        # inline template
         try:
             self._unassociated.remove((module_info.dotted_name, template_name))
         except KeyError:
@@ -44,7 +70,8 @@
     def lookup(self, module_info, template_name, mark_as_associated=False):
         result = self._reg.get((module_info.dotted_name, template_name))
         if result is None:
-            raise TemplateLookupError("inline template '%s' in '%s' cannot be found" % (
+            raise TemplateLookupError(
+                "inline template '%s' in '%s' cannot be found" % (
                     template_name, module_info.dotted_name))
         if mark_as_associated:
             self.associate(module_info, template_name)
@@ -53,12 +80,28 @@
     def unassociated(self):
         return self._unassociated
 
+
 class FileTemplateRegistry(object):
+    """Registry managing all template files.
+    """
+
+    _reg = None
+    _unassociated = None
+    _registered_directories = None
+    _ignored_patterns = None
+
     def __init__(self):
+        self.clear()
+
+    def clear(self):
         self._reg = {}
         self._unassociated = set()
         self._registered_directories = set()
+        self._ignored_patterns = []
 
+    def ignore_templates(self, pattern):
+        self._ignored_patterns.append(re.compile(pattern))
+
     def register_directory(self, module_info):
         # we cannot register a templates dir for a package
         if module_info.isPackage():
@@ -81,14 +124,12 @@
 
     def _register_template_file(self, module_info, template_path):
         template_dir, template_file = os.path.split(template_path)
+        for pattern in self._ignored_patterns:
+            if pattern.search(template_file):
+                return
 
-        if template_file.startswith('.') or template_file.endswith('~'):
-            return
-        if template_file.endswith('.cache'):
-            # chameleon creates '<tpl_name>.cache' files on the fly
-            return
+        template_name, extension = os.path.splitext(template_file)
 
-        template_name, extension = os.path.splitext(template_file)
         if (template_dir, template_name) in self._reg:
             raise GrokError("Conflicting templates found for name '%s' "
                             "in directory '%s': multiple templates with "
@@ -107,6 +148,7 @@
                              template_dir), None)
 
         extension = extension[1:] # Get rid of the leading dot.
+
         template_factory = zope.component.queryUtility(
             grokcore.view.interfaces.ITemplateFileFactory,
             name=extension)
@@ -136,10 +178,12 @@
         template_dir = self.get_template_dir(module_info)
         result = self._reg.get((template_dir, template_name))
         if result is None:
-            raise TemplateLookupError("template '%s' in '%s' cannot be found" % (
+            raise TemplateLookupError(
+                "template '%s' in '%s' cannot be found" % (
                     template_name, template_dir))
         if mark_as_associated:
-            registered_template_path = self._reg.get((template_dir, template_name)).__grok_location__
+            registered_template_path = self._reg.get(
+                (template_dir, template_name)).__grok_location__
             self.associate(registered_template_path)
         return result
 
@@ -159,17 +203,16 @@
 file_template_registry = FileTemplateRegistry()
 
 def register_inline_template(module_info, template_name, template):
-    return inline_template_registry.register_inline_template(module_info, template_name, template)
+    return inline_template_registry.register_inline_template(
+        module_info, template_name, template)
 
 def register_directory(module_info):
     return file_template_registry.register_directory(module_info)
 
 def _clear():
     """Remove the registries (for use by tests)."""
-    global inline_template_registry
-    global file_template_registry
-    inline_template_registry = InlineTemplateRegistry()
-    file_template_registry = FileTemplateRegistry()
+    inline_template_registry.clear()
+    file_template_registry.clear()
 
 try:
     from zope.testing.cleanup import addCleanUp
@@ -182,10 +225,12 @@
 
 def lookup(module_info, template_name, mark_as_associated=False):
     try:
-        return file_template_registry.lookup(module_info, template_name, mark_as_associated)
+        return file_template_registry.lookup(
+            module_info, template_name, mark_as_associated)
     except TemplateLookupError, e:
         try:
-            return inline_template_registry.lookup(module_info, template_name, mark_as_associated)
+            return inline_template_registry.lookup(
+                module_info, template_name, mark_as_associated)
         except TemplateLookupError, e2:
             # re-raise first error again
             raise e
@@ -249,7 +294,8 @@
 
     # Lookup for a template in the registry
     try:
-        factory.template = lookup(module_info, template_name, mark_as_associated=True)
+        factory.template = lookup(
+            module_info, template_name, mark_as_associated=True)
         factory_have_template = True
     except TemplateLookupError:
         pass

Modified: grokcore.view/trunk/src/grokcore/view/testing.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/testing.py	2011-02-28 14:12:13 UTC (rev 120603)
+++ grokcore.view/trunk/src/grokcore/view/testing.py	2011-02-28 14:36:50 UTC (rev 120604)
@@ -17,8 +17,8 @@
 import grokcore.view
 from zope.configuration.config import ConfigurationMachine
 from grokcore.component import zcml
+from grokcore.view.zcml import file_template_registry
 
-
 def grok(module_name):
     config = ConfigurationMachine()
     zcml.do_grok('grokcore.component.meta', config)

Modified: grokcore.view/trunk/src/grokcore/view/tests/test_all.py
===================================================================
--- grokcore.view/trunk/src/grokcore/view/tests/test_all.py	2011-02-28 14:12:13 UTC (rev 120603)
+++ grokcore.view/trunk/src/grokcore/view/tests/test_all.py	2011-02-28 14:36:50 UTC (rev 120604)
@@ -3,13 +3,15 @@
 from pkg_resources import resource_listdir
 from zope.testing import doctest, cleanup, renormalizing
 import zope.component.eventtesting
+from grokcore.view.templatereg import file_template_registry
 
 optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
 
-def setUpZope(test):
+def setUp(test):
     zope.component.eventtesting.setUp(test)
+    file_template_registry.ignore_templates('.svn')
 
-def cleanUpZope(test):
+def cleanUp(test):
     cleanup.cleanUp()
 
 checker = renormalizing.RENormalizing([
@@ -30,14 +32,13 @@
             continue
         if filename == '__init__.py':
             continue
-
         dottedname = 'grokcore.view.tests.%s.%s' % (name, filename[:-3])
-        test = doctest.DocTestSuite(dottedname,
-                                    setUp=setUpZope,
-                                    tearDown=cleanUpZope,
-                                    checker=checker,
-                                    optionflags=optionflags)
-
+        test = doctest.DocTestSuite(
+            dottedname,
+            setUp=setUp,
+            tearDown=cleanUp,
+            checker=checker,
+            optionflags=optionflags)
         suite.addTest(test)
     return suite
 
@@ -45,9 +46,10 @@
     suite = unittest.TestSuite()
     for name in ['view', 'skin', 'template', 'directoryresource']:
         suite.addTest(suiteFromPackage(name))
-    suite.addTest(doctest.DocFileSuite('../templatereg.txt',
-                                       optionflags=optionflags,
-                                       setUp=setUpZope,
-                                       tearDown=cleanUpZope,
-                                       ))
+    suite.addTest(doctest.DocFileSuite(
+        '../templatereg.txt',
+        optionflags=optionflags,
+        setUp=setUp,
+        tearDown=cleanUp,
+        ))
     return suite

Copied: grokcore.view/trunk/src/grokcore/view/zcml.py (from rev 120476, grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/zcml.py)
===================================================================
--- grokcore.view/trunk/src/grokcore/view/zcml.py	                        (rev 0)
+++ grokcore.view/trunk/src/grokcore/view/zcml.py	2011-02-28 14:36:50 UTC (rev 120604)
@@ -0,0 +1,34 @@
+##############################################################################
+#
+# Copyright (c) 2006-2007 Zope Foundation 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.
+#
+##############################################################################
+"""Grok ZCML directives."""
+
+from zope.interface import Interface
+from zope.schema import TextLine
+
+from grokcore.view.templatereg import file_template_registry
+
+
+class IIgnoreTemplatesDirective(Interface):
+    """Ignore a template pattern.
+    """
+
+    pattern = TextLine(
+        title=u"Pattern",
+        description=u"Pattern of template to ignore.",
+        required=True)
+
+def ignoreTemplates(_context, pattern):
+    file_template_registry.ignore_templates(pattern)
+
+



More information about the checkins mailing list