[Checkins] SVN: grokcore.view/branches/sylvain-template-warning-improvements/ Add an ignoreTemplate ZCML directive.

Sylvain Viollon sylvain at infrae.com
Sun Feb 20 16:15:53 EST 2011


Log message for revision 120476:
  Add an ignoreTemplate ZCML directive.
  

Changed:
  U   grokcore.view/branches/sylvain-template-warning-improvements/CHANGES.txt
  U   grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/configure.zcml
  U   grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/meta-minimal.zcml
  U   grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/meta.zcml
  U   grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/templatereg.py
  A   grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/zcml.py

-=-
Modified: grokcore.view/branches/sylvain-template-warning-improvements/CHANGES.txt
===================================================================
--- grokcore.view/branches/sylvain-template-warning-improvements/CHANGES.txt	2011-02-20 21:13:55 UTC (rev 120475)
+++ grokcore.view/branches/sylvain-template-warning-improvements/CHANGES.txt	2011-02-20 21:15:53 UTC (rev 120476)
@@ -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/branches/sylvain-template-warning-improvements/src/grokcore/view/configure.zcml
===================================================================
--- grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/configure.zcml	2011-02-20 21:13:55 UTC (rev 120475)
+++ grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/configure.zcml	2011-02-20 21:15:53 UTC (rev 120476)
@@ -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/branches/sylvain-template-warning-improvements/src/grokcore/view/meta-minimal.zcml
===================================================================
--- grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/meta-minimal.zcml	2011-02-20 21:13:55 UTC (rev 120475)
+++ grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/meta-minimal.zcml	2011-02-20 21:15:53 UTC (rev 120476)
@@ -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/branches/sylvain-template-warning-improvements/src/grokcore/view/meta.zcml
===================================================================
--- grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/meta.zcml	2011-02-20 21:13:55 UTC (rev 120475)
+++ grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/meta.zcml	2011-02-20 21:15:53 UTC (rev 120476)
@@ -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/branches/sylvain-template-warning-improvements/src/grokcore/view/templatereg.py
===================================================================
--- grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/templatereg.py	2011-02-20 21:13:55 UTC (rev 120475)
+++ grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/templatereg.py	2011-02-20 21:15:53 UTC (rev 120476)
@@ -1,20 +1,39 @@
+##############################################################################
+#
+# 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.
+    """
+
     def __init__(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 +54,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 +64,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 +74,20 @@
     def unassociated(self):
         return self._unassociated
 
+
 class FileTemplateRegistry(object):
+    """Registry managing all template files.
+    """
+
     def __init__(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():
@@ -82,11 +111,9 @@
     def _register_template_file(self, module_info, template_path):
         template_dir, template_file = os.path.split(template_path)
 
-        if template_file.startswith('.') or template_file.endswith('~'):
-            return
-        if template_file.endswith('.cache'):
-            # chameleon creates '<tpl_name>.cache' files on the fly
-            return
+        for pattern in self._ignored_patterns:
+            if pattern.search(template_file):
+                return
 
         template_name, extension = os.path.splitext(template_file)
         if (template_dir, template_name) in self._reg:
@@ -136,10 +163,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,7 +188,8 @@
 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)
@@ -182,10 +212,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 +281,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

Added: grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/zcml.py
===================================================================
--- grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/zcml.py	                        (rev 0)
+++ grokcore.view/branches/sylvain-template-warning-improvements/src/grokcore/view/zcml.py	2011-02-20 21:15:53 UTC (rev 120476)
@@ -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