[Checkins] SVN: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/ Split TemplateRegistry into two classes ModuleTemplateRegistry and DirectoryTemplateRegistry to fix the shared template directory issue.

Vincent Fretin vincent.fretin at gmail.com
Sat Jul 4 04:42:51 EDT 2009


Log message for revision 101471:
  Split TemplateRegistry into two classes ModuleTemplateRegistry and DirectoryTemplateRegistry to fix the shared template directory issue.

Changed:
  U   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/meta/templates.py
  U   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.py
  U   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir.py
  D   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir_fixture.py
  A   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/
  A   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/__init__.py
  A   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/first_module.py
  A   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/second_module.py
  A   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/templates/
  D   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_templates/
  U   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/unassociated.py

-=-
Modified: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/meta/templates.py
===================================================================
--- grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/meta/templates.py	2009-07-04 08:25:25 UTC (rev 101470)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/meta/templates.py	2009-07-04 08:42:51 UTC (rev 101471)
@@ -25,7 +25,7 @@
     martian.priority(1001)
 
     def grok(self, name, module, module_info, config, **kw):
-        module.__grok_templates__ = templatereg.TemplateRegistry()
+        module.__grok_templates__ = templatereg.ModuleTemplateRegistry()
         return True
 
 
@@ -81,7 +81,8 @@
         config.action(
             discriminator=None,
             callable=templates.checkUnassociated,
-            args=(module_info,)
+            args=(module_info,),
+            order=100
             )
         return True
 

Modified: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.py
===================================================================
--- grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.py	2009-07-04 08:25:25 UTC (rev 101470)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.py	2009-07-04 08:42:51 UTC (rev 101471)
@@ -7,8 +7,11 @@
 from grokcore.view.interfaces import ITemplateFileFactory
 from grokcore.view.components import PageTemplate
 
-class TemplateRegistry(object):
+all_directory_templates_registries = {}
 
+
+class BaseTemplateRegistry(object):
+
     def __init__(self):
         self._reg = {}
 
@@ -16,15 +19,48 @@
         self._reg[name] = dict(template=template, associated=False)
 
     def markAssociated(self, name):
-        self._reg[name]['associated'] = True
+        entry = self._getEntry(name)
+        entry['associated'] = True
 
+    def _getEntry(self, name):
+        return self._reg.get(name)
+
     def get(self, name):
+        entry = self._getEntry(name)
+        if entry is None:
+            return None
+        return entry['template']
+
+    def listUnassociated(self):
+        for name, entry in self._reg.iteritems():
+            if not entry['associated']:
+                yield name
+
+
+class ModuleTemplateRegistry(BaseTemplateRegistry):
+
+    _directory_registry = None
+
+    def initializeDirectoryRegistry(self, template_dir):
+        if template_dir is not None:
+            if not template_dir in all_directory_templates_registries:
+                all_directory_templates_registries[template_dir] = DirectoryTemplateRegistry(template_dir)
+            self._directory_registry = all_directory_templates_registries[template_dir]
+
+    def _getEntry(self, name):
+        entry = super(ModuleTemplateRegistry, self)._getEntry(name)
+        # self._directory_registry has not been instanciated when registering instance templates
+        if entry is None and self._directory_registry is not None:
+            entry = self._directory_registry._getEntry(name)
+        return entry
+
+    def getLocal(self, name):
         entry = self._reg.get(name)
         if entry is None:
             return None
         return entry['template']
 
-    def findFilesystem(self, module_info):
+    def _getTemplateDir(self, module_info):
         template_dir_name = grokcore.view.templatedir.bind().get(
             module=module_info.getModule())
         if template_dir_name is None:
@@ -32,9 +68,16 @@
 
         template_dir = module_info.getResourcePath(template_dir_name)
 
+        return template_dir
+
+    def findFilesystem(self, module_info):
+        template_dir = self._getTemplateDir(module_info)
+
         if not os.path.isdir(template_dir):
             return
 
+        self.initializeDirectoryRegistry(template_dir)
+
         if module_info.isPackage():
             return
 
@@ -60,7 +103,7 @@
                               (template_file, template_dir), UserWarning, 2)
                 continue
 
-            inline_template = self.get(template_name)
+            inline_template = self.getLocal(template_name)
             if inline_template:
                 raise GrokError("Conflicting templates found for name '%s' "
                                 "in module %r, either inline and in template "
@@ -69,17 +112,13 @@
                                 % (template_name, module_info.getModule(),
                                    template_dir), inline_template)
 
-            template = template_factory(template_file, template_dir)
-            template_path = os.path.join(template_dir, template_file)
-            template._annotateGrokInfo(template_name, template_path)
+            if self._directory_registry._getEntry(template_name) is None:
+                template = template_factory(template_file, template_dir)
+                template_path = os.path.join(template_dir, template_file)
+                template._annotateGrokInfo(template_name, template_path)
 
-            self.register(template_name, template)
+                self._directory_registry.register(template_name, template)
 
-    def listUnassociated(self):
-        for name, entry in self._reg.iteritems():
-            if not entry['associated']:
-                yield name
-
     def checkUnassociated(self, module_info):
         unassociated = list(self.listUnassociated())
         if unassociated:
@@ -90,6 +129,9 @@
                 module_info.dotted_name, ', '.join(unassociated)))
             warnings.warn(msg, UserWarning, 1)
 
+        if self._directory_registry is not None:
+            self._directory_registry.checkUnassociated(module_info)
+
     def checkTemplates(self, module_info, factory, component_name,
                        has_render, has_no_render):
         factory_name = factory.__name__.lower()
@@ -126,6 +168,28 @@
                                 "'render' method." %
                                 (component_name.title(), factory), factory)
 
+
+class DirectoryTemplateRegistry(BaseTemplateRegistry):
+
+    _checked = False
+
+    def __init__(self, template_dir):
+        super(DirectoryTemplateRegistry, self).__init__()
+        self.template_dir = template_dir
+
+    def checkUnassociated(self, module_info):
+        if not self._checked:
+            self._checked = True
+            unassociated = list(self.listUnassociated())
+            if unassociated:
+                msg = (
+                    "Found the following unassociated template(s) when "
+                    "grokking directory %r: %s.  Define view classes inheriting "
+                    "from grok.View to enable the template(s)." % (
+                    self.template_dir, ', '.join(unassociated)))
+                warnings.warn(msg, UserWarning, 1)
+
+
 class PageTemplateFileFactory(grokcore.component.GlobalUtility):
     grokcore.component.implements(ITemplateFileFactory)
     grokcore.component.name('pt')

Modified: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir.py
===================================================================
--- grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir.py	2009-07-04 08:25:25 UTC (rev 101470)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir.py	2009-07-04 08:42:51 UTC (rev 101471)
@@ -1,33 +1,20 @@
 """
 When modules share a template directory, templates that have not been associated with any view class of a given module issue a UserWarning:
 
+  >>> import grokcore.view as grok
   >>> from grokcore.view.testing import warn
   >>> import warnings
   >>> saved_warn = warnings.warn
   >>> warnings.warn = warn
 
-  >>> import shared_template_dir_fixture
+  >>> import shared_template_fixture 
 
-  >>> grok.testing.grok(__name__)
+  >>> grok.testing.grok(shared_template_fixture.__name__)
   From grok.testing's warn():
   ...UserWarning: Found the following unassociated template(s) when grokking
-  'grokcore.view.tests.view.shared_template_dir': food, unassociated.  Define view classes inheriting from
+  'grokcore.view.tests.view.shared_template_fixture.first_module': unassociated_instance.  Define view classes inheriting from
   grok.View to enable the template(s)...
-
-  >>> grok.testing.grok(shared_template_dir_fixture.__name__)
-  From grok.testing's warn():
-  ...UserWarning: Found the following unassociated template(s) when grokking
-  'grokcore.view.tests.view.shared_template_dir_fixture': cavepainting, unassociated.  Define view classes inheriting from
+  ...UserWarning: Found the following unassociated template(s) when grokking directory
+  '...shared_template_fixture...templates': unassociated.  Define view classes inheriting from
   grok.View to enable the template(s)...
-
 """
-import grokcore.view as grok
-
-
-grok.templatedir("shared_templates")
-
-class Mammoth(grok.Context):
-    pass
-
-class CavePainting(grok.View):
-    pass

Deleted: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir_fixture.py
===================================================================
--- grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir_fixture.py	2009-07-04 08:25:25 UTC (rev 101470)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir_fixture.py	2009-07-04 08:42:51 UTC (rev 101471)
@@ -1,11 +0,0 @@
-"""
-This should issue a UserWarning.
-"""
-import grokcore.view as grok
-from shared_template_dir import Mammoth
-
-grok.templatedir("shared_templates")
-
-class Food(grok.View):
-    grok.context(Mammoth)
-

Copied: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/first_module.py (from rev 101446, grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir.py)
===================================================================
--- grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/first_module.py	                        (rev 0)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/first_module.py	2009-07-04 08:42:51 UTC (rev 101471)
@@ -0,0 +1,14 @@
+import grokcore.view as grok
+
+
+grok.templatedir("templates")
+
+class Mammoth(grok.Context):
+    pass
+
+class CavePainting(grok.View):
+    pass
+
+unassociated_instance = grok.PageTemplate("""
+<html><body><h1>GROK PAINT MAMMOTH!</h1></body></html>
+""")


Property changes on: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/first_module.py
___________________________________________________________________
Added: svn:mergeinfo
   + 

Copied: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/second_module.py (from rev 101446, grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir_fixture.py)
===================================================================
--- grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/second_module.py	                        (rev 0)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/second_module.py	2009-07-04 08:42:51 UTC (rev 101471)
@@ -0,0 +1,11 @@
+"""
+This should issue a UserWarning.
+"""
+import grokcore.view as grok
+from first_module import Mammoth
+
+grok.templatedir("templates")
+
+class Food(grok.View):
+    grok.context(Mammoth)
+


Property changes on: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/second_module.py
___________________________________________________________________
Added: svn:mergeinfo
   + 


Property changes on: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_fixture/templates
___________________________________________________________________
Added: svn:mergeinfo
   + 

Modified: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/unassociated.py
===================================================================
--- grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/unassociated.py	2009-07-04 08:25:25 UTC (rev 101470)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/unassociated.py	2009-07-04 08:42:51 UTC (rev 101471)
@@ -9,8 +9,8 @@
 
   >>> grok.testing.grok(__name__)
   From grok.testing's warn():
-  ...UserWarning: Found the following unassociated template(s) when grokking
-  'grokcore.view.tests.view.unassociated': index.  Define view classes inheriting from
+  ...UserWarning: Found the following unassociated template(s) when grokking directory
+  '...unassociated_templates': index.  Define view classes inheriting from
   grok.View to enable the template(s)...
 
 Also templates of modules named equally as the package name the module



More information about the Checkins mailing list