[Checkins] SVN: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/te Checkpoint checkin: test conflict between inline and file templates

Martijn Faassen faassen at startifact.com
Sat Jul 4 11:14:39 EDT 2009


Log message for revision 101545:
  Checkpoint checkin: test conflict between inline and file templates
  (part 1)
  

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
  U   grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir.py

-=-
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 15:03:29 UTC (rev 101544)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.py	2009-07-04 15:14:39 UTC (rev 101545)
@@ -30,7 +30,6 @@
     def unassociated(self):
         return self._unassociated
 
-
 class FileTemplateRegistry(object):
     def __init__(self):
         self._reg = {}
@@ -41,7 +40,7 @@
         if module_info.isPackage():
             return
 
-        template_dir = self._get_template_dir(module_info)
+        template_dir = self.get_template_dir(module_info)
         self.register_directory(template_dir)
         
     def register_directory(self, template_dir):
@@ -111,15 +110,42 @@
     def unassociated(self):
         return self._unassociated
         
-    def _get_template_dir(self, module_info):
+    def get_template_dir(self, module_info):
         template_dir_name = grokcore.view.templatedir.bind().get(
             module=module_info.getModule())
         if template_dir_name is None:
             template_dir_name = module_info.name + '_templates'
 
-            template_dir = module_info.getResourcePath(template_dir_name)
+        template_dir = module_info.getResourcePath(template_dir_name)
         return template_dir
 
+inline_template_registry = InlineTemplateRegistry()
+file_template_registry = FileTemplateRegistry()
+
+def register_inline_template(module_info, template_name, template):
+    template_dir = file_template_registry.get_template_dir(module_info)
+
+    try:
+        existing_template = file_template_registry.lookup(
+            template_dir, template_name)
+    except LookupError:
+        pass # we actually want a LookupError as the template shouldn't exist
+    else:
+        raise GrokError("Conflicting templates found for name '%s': "
+                        "the inline template in module '%s' conflicts "
+                        "with the file template in directory '%s'" %
+                        (template_name, module_info.dotted_name,
+                         template_dir), None)
+    inline_template_registry.register_inline_template(
+        module_info, template_name, template)
+    
+
+def register_directory(template_dir):
+    file_template_registry.register_directory(template_dir)
+
+
+
+    
 all_directory_templates_registries = {}
 
 

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-04 15:03:29 UTC (rev 101544)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.txt	2009-07-04 15:14:39 UTC (rev 101545)
@@ -25,11 +25,11 @@
 For testing purposes we will create a directory with two templates in it::
 
   >>> import os, tempfile
-  >>> templates_dir = tempfile.mkdtemp()
-  >>> f = open(os.path.join(templates_dir, 'foo.template'), 'w')
+  >>> template_dir = tempfile.mkdtemp()
+  >>> f = open(os.path.join(template_dir, 'foo.template'), 'w')
   >>> f.write('foo')
   >>> f.close()
-  >>> f = open(os.path.join(templates_dir, 'bar.template'), 'w')
+  >>> f = open(os.path.join(template_dir, 'bar.template'), 'w')
   >>> f.write('bar')
   >>> f.close()
 
@@ -64,18 +64,27 @@
   >>> component.provideUtility(TestTemplateFactory(), ITemplateFileFactory, 
   ...   name='template')
 
+Registration functions
+----------------------
+
+Below we are going to manually create and test template registries. In
+a normal run of the application, the global registration functions are
+used to register templates. These are ``register_inline_template`` for
+inline templates in Python code, and ``register_directory`` for
+templates in directories.
+
 Registering a directory
 -----------------------
 
 We can now register the filesystem templates with the registry::
 
-  >>> reg.register_directory(templates_dir)
+  >>> reg.register_directory(template_dir)
 
 We can look up the templates in the registry now::
 
-  >>> reg.lookup(templates_dir, 'foo')
+  >>> reg.lookup(template_dir, 'foo')
   <Template 'foo.template' in '...'>
-  >>> reg.lookup(templates_dir, 'bar')
+  >>> reg.lookup(template_dir, 'bar')
   <Template 'bar.template' in '...'>
 
 If we try to look up a template in a directory that doesn't exist, we get
@@ -88,7 +97,7 @@
 
 We get this error for templates that do not exist as well::
 
-  >>> reg.lookup(templates_dir, 'doesntexist')
+  >>> reg.lookup(template_dir, 'doesntexist')
   Traceback (most recent call last):
     ...
   LookupError: template 'doesntexist' in ... cannot be found
@@ -101,7 +110,7 @@
 
 Now we use a template, so we mark it as associated::
 
-  >>> reg.associate(os.path.join(templates_dir, 'foo.template'))
+  >>> reg.associate(os.path.join(template_dir, 'foo.template'))
 
 There is only a single unassociated template left now::
 
@@ -115,8 +124,8 @@
 the system::
 
   >>> import os, tempfile
-  >>> templates_dir2 = tempfile.mkdtemp()
-  >>> f = open(os.path.join(templates_dir2, 'foo.unknown'), 'w')
+  >>> template_dir2 = tempfile.mkdtemp()
+  >>> f = open(os.path.join(template_dir2, 'foo.unknown'), 'w')
   >>> f.write('unknown')
   >>> f.close()
 
@@ -130,7 +139,7 @@
 
 We register the directory now, and we get the warning::
 
-  >>> reg.register_directory(templates_dir2)
+  >>> reg.register_directory(template_dir2)
   From grok.testing's warn():
   ... UserWarning: File 'foo.unknown' has an unrecognized extension in directory '...'
   ...
@@ -141,7 +150,7 @@
 
 This file will not be loaded as a template::
 
-  >>> reg.lookup(templates_dir2, 'foo.unknown')
+  >>> reg.lookup(template_dir2, 'foo.unknown')
   Traceback (most recent call last):
     ...
   LookupError: template 'foo.unknown' in '...' cannot be found
@@ -161,17 +170,17 @@
 Grok won't know which one it should use::
 
   >>> import os, tempfile
-  >>> templates_dir3 = tempfile.mkdtemp()
-  >>> f = open(os.path.join(templates_dir3, 'foo.1'), 'w')
+  >>> template_dir3 = tempfile.mkdtemp()
+  >>> f = open(os.path.join(template_dir3, 'foo.1'), 'w')
   >>> f.write('1')
   >>> f.close()
-  >>> f = open(os.path.join(templates_dir3, 'foo.2'), 'w')
+  >>> f = open(os.path.join(template_dir3, 'foo.2'), 'w')
   >>> f.write('2')
   >>> f.close()
 
 We expect an error when we register this directory::
 
-  >>> reg.register_directory(templates_dir3)
+  >>> reg.register_directory(template_dir3)
   Traceback (most recent call last):
     ...
   GrokError: Conflicting templates found for name 'foo' in directory '...': 
@@ -179,6 +188,7 @@
 
 Inline templates
 ----------------
+
 Inline templates are defined in a Python module instead of on the
 filesystem.
 
@@ -186,16 +196,28 @@
 
    >>> class ModuleInfo(object):
    ...     dotted_name = 'module'
+   ...     name = 'module'
+   ...     def __init__(self, dotted_name, name, dir):
+   ...         self.dotted_name = dotted_name
+   ...         self.name = name
+   ...         self.dir = dir
+   ...     def getModule(self):
+   ...         return None
+   ...     def getResourcePath(self, template_dir_name):
+   ...         return os.path.join(self.dir, template_dir_name)
 
 an create an instance of this class::
 
-   >>> module_info = ModuleInfo()
+   >>> module_info = ModuleInfo('module', 'module', None)
 
 Let's create a class for inline template and create an instance::
 
   >>> class InlineTemplate(object):
-  ...     pass
-  >>> cavepainting = InlineTemplate()
+  ...     def __init__(self, name):
+  ...         self.name = name
+  ...     def __repr__(self):
+  ...         return "<InlineTemplate '%s'>" % self.name
+  >>> cavepainting = InlineTemplate('cavepainting')
 
 We create our global inline template registry once::
 
@@ -206,9 +228,64 @@
 
   >>> inline_reg.register_inline_template(module_info, 'cavepainting', cavepainting)
 
-Since no templates have yet been associated, retrieving the unassociated
-templates will get us all registered inline templates::
+We can look it up now::
 
+  >>> inline_reg.lookup(module_info, 'cavepainting')
+  <InlineTemplate 'cavepainting'>
+  
+If we cannot find the template we get an error::
+
+  >>> inline_reg.lookup(module_info, 'unknown')
+  Traceback (most recent call last):
+    ...
+  LookupError: inline template 'unknown' in 'module' cannot be found
+
+Since no templates have yet been associated, retrieving the
+unassociated templates will get us all registered inline templates::
+
   >>> sorted(inline_reg.unassociated())
   [('module', 'cavepainting')]
 
+Conflicts between inline templates and file templates
+-----------------------------------------------------
+
+
+We construct a fake templates directory that's associated with the fictional
+``module`` module::
+
+  >>> import os, tempfile
+  >>> package_dir = tempfile.mkdtemp()
+  >>> module_template_dir = os.path.join(package_dir, 'module_templates')
+  >>> os.mkdir(module_template_dir)
+
+And we create the module info for this::
+
+   >>> module_info = ModuleInfo('module', 'module', package_dir)
+
+We create a template with the name ``foo`` in it::
+
+  >>> f = open(os.path.join(module_template_dir, 'foo.template'), 'w')
+  >>> f.write('foo')
+  >>> f.close()
+
+We register this directory, using the global registration functionality::
+
+  >>> from grokcore.view.templatereg import register_directory
+  >>> register_directory(module_template_dir)
+
+We now also try to register an inline template with the same name
+(``foo``), but this fails due to a conflict with the file template::
+
+  >>> from grokcore.view.templatereg import register_inline_template
+  >>> register_inline_template(module_info, 'foo', InlineTemplate('foo'))
+  Traceback (most recent call last):
+     ...
+  GrokError: Conflicting templates found for name 'foo': the inline template 
+  in module 'module' conflicts with the file template in directory 
+  '...module_templates'
+
+
+
+
+
+XXX a common lookup function that looks up both inline or filesystem?

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 15:03:29 UTC (rev 101544)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/tests/view/shared_template_dir.py	2009-07-04 15:14:39 UTC (rev 101545)
@@ -17,4 +17,5 @@
   ...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)...
+
 """



More information about the Checkins mailing list