[Checkins] SVN: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg. Introduce common lookup function, modify API of file template registry

Martijn Faassen faassen at startifact.com
Tue Jul 21 10:22:09 EDT 2009


Log message for revision 102054:
  Introduce common lookup function, modify API of file template registry 
  somewhat.
  

Changed:
  U   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.py
  U   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.txt

-=-
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-21 11:51:43 UTC (rev 102053)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.py	2009-07-21 14:22:09 UTC (rev 102054)
@@ -16,14 +16,13 @@
 
     def register_inline_template(self, module_info, template_name, template):
         # verify no file template got registered with the same name
-        template_dir = file_template_registry.get_template_dir(module_info)
-
         try:
             existing_template = file_template_registry.lookup(
-                template_dir, template_name)
+                module_info, template_name)
         except TemplateLookupError:
             pass
         else:
+            template_dir = file_template_registry.get_template_dir(module_info)
             raise GrokError("Conflicting templates found for name '%s': "
                             "the inline template in module '%s' conflicts "
                             "with the file template in directory '%s'" %
@@ -116,7 +115,8 @@
     def associate(self, template_path):
         self._unassociated.remove(template_path)
 
-    def lookup(self, template_dir, template_name):
+    def lookup(self, module_info, template_name):
+        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" % (
@@ -144,6 +144,15 @@
     
 all_directory_templates_registries = {}
 
+def lookup(module_info, template_name):
+    try:
+        return file_template_registry.lookup(module_info, template_name)
+    except TemplateLookupError, e:
+        try:
+            return inline_template_registry.lookup(module_info, template_name)
+        except TemplateLookupError, e2:
+            # re-raise first error again
+            raise e
 
 class BaseTemplateRegistry(object):
 

Modified: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.txt
===================================================================
--- grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.txt	2009-07-21 11:51:43 UTC (rev 102053)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.txt	2009-07-21 14:22:09 UTC (rev 102054)
@@ -53,7 +53,7 @@
   ...        f = open(os.path.join(_prefix, filename), 'r')
   ...        data = f.read()
   ...        f.close()
-  ...        return TestTemplate(filename, data)
+  ...        return TestTemplate(os.path.join(_prefix, filename), data)
 
   >>> from zope import component
   >>> component.provideUtility(TestTemplateFactory(), ITemplateFileFactory, 
@@ -75,6 +75,9 @@
    ...         return False
 
    >>> import os, tempfile
+   >>> def create_module_info(module_name):
+   ...     package_dir = tempfile.mkdtemp()
+   ...     return ModuleInfo(module_name, package_dir)
    >>> def create_module_info_with_templates(module_name):
    ...     package_dir = tempfile.mkdtemp()
    ...     templates_dir = os.path.join(package_dir, module_name + '_templates')
@@ -105,22 +108,23 @@
 
 We can look up the templates in the registry now::
 
-  >>> reg.lookup(template_dir, 'foo')
+  >>> reg.lookup(module_info, 'foo')
   <Template 'foo.template' in '...'>
-  >>> reg.lookup(template_dir, 'bar')
+  >>> reg.lookup(module_info, 'bar')
   <Template 'bar.template' in '...'>
 
 If we try to look up a template in a directory that doesn't exist, we get
 a TemplateLookupError::
   
-  >>> reg.lookup('certainlydoesntexist', 'foo')
+  >>> nonexistent_module_info = create_module_info('nonexistent')
+  >>> reg.lookup(nonexistent_module_info, 'foo')
   Traceback (most recent call last):
     ...
-  TemplateLookupError: template 'foo' in 'certainlydoesntexist' cannot be found
+  TemplateLookupError: template 'foo' in '...' cannot be found
 
 We get this error for templates that do not exist as well::
 
-  >>> reg.lookup(template_dir, 'doesntexist')
+  >>> reg.lookup(module_info, 'doesntexist')
   Traceback (most recent call last):
     ...
   TemplateLookupError: template 'doesntexist' in ... cannot be found
@@ -173,7 +177,7 @@
 
 This file will not be loaded as a template::
 
-  >>> reg.lookup(template_dir2, 'foo.unknown')
+  >>> reg.lookup(module_info2, 'foo.unknown')
   Traceback (most recent call last):
     ...
   TemplateLookupError: template 'foo.unknown' in '...' cannot be found
@@ -248,6 +252,37 @@
   >>> sorted(inline_reg.unassociated())
   [('module4', 'cavepainting')]
 
+A common template lookup function
+---------------------------------
+
+There is a single lookup function available that can used to look up
+both filesystem templates as well as inline templates.
+
+  >>> lookuptest_info, lookuptest_template_dir = create_module_info_with_templates('lookuptest')
+  >>> f = open(os.path.join(lookuptest_template_dir, 'foo.template'), 'w')
+  >>> f.write('foo')
+  >>> f.close()
+  >>> register_directory(lookuptest_info)
+
+  >>> from grokcore.view.templatereg import lookup
+  >>> lookup(lookuptest_info, 'foo')
+  <Template 'foo.template' in '...'>
+
+This can also be used to look up inline templates::
+
+  >>> bar = InlineTemplate('bar')
+  >>> register_inline_template(lookuptest_info, 'bar', bar)
+  >>> lookup(lookuptest_info, 'bar')
+  <InlineTemplate 'bar'>
+
+If we look up a template that doesn't exist, we get an error (about it
+missing on the filesystem)::
+
+  >>> lookup(lookuptest_info, 'qux')
+  Traceback (most recent call last):
+    ...
+  TemplateLookupError: template 'qux' in '...' cannot be found
+
 Conflicts between inline templates and file templates
 -----------------------------------------------------
 



More information about the Checkins mailing list