[Checkins] SVN: grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg. Renamed TemplateRegistry by FileTemplateRegistry and created an InlineTemplateInstanceRegistry. Now the lookup method of each registry should call lookup from the other registry and raise an error if there is a template registered with the same name in the other registry.

Vincent Fretin vincent.fretin at gmail.com
Sat Jul 4 10:22:57 EDT 2009


Log message for revision 101528:
  Renamed TemplateRegistry by FileTemplateRegistry and created an InlineTemplateInstanceRegistry. Now the lookup method of each registry should call lookup from the other registry and raise an error if there is a template registered with the same name in the other registry.

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-04 14:22:39 UTC (rev 101527)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.py	2009-07-04 14:22:56 UTC (rev 101528)
@@ -8,11 +8,34 @@
 from grokcore.view.components import PageTemplate
         
 
-class TemplateRegistry(object):
+class InlineTemplateRegistry(object):
     def __init__(self):
         self._reg = {}
         self._unassociated = set()
 
+    def register_inline_template(self, module_info, template_name, template):
+        self._reg[(module_info.dotted_name, template_name)] = template
+        self._unassociated.add((module_info.dotted_name, template_name))
+
+    def associate(self, module_info, template_name):
+        self._unassociated.remove((module_info.dotted_name, template_name))
+
+    def lookup(self, module_info, template_name):
+        result = self._reg.get((module_info.dotted_name, template_name))
+        if result is None:
+            raise LookupError("inline template '%s' in '%s' cannot be found" % (
+                    template_name, module_info.dotted_name))
+        return result
+    
+    def unassociated(self):
+        return self._unassociated
+
+
+class FileTemplateRegistry(object):
+    def __init__(self):
+        self._reg = {}
+        self._unassociated = set()
+
     def register_directory_for_module(self, module_info):
         # we cannot register a templates dir for a package
         if module_info.isPackage():
@@ -75,9 +98,8 @@
         self._reg[(template_dir, template_name)] = template
         self._unassociated.add(template_path)
         
-        
-    def associate(self, template_file):
-        self._unassociated.remove(template_file)
+    def associate(self, template_path):
+        self._unassociated.remove(template_path)
 
     def lookup(self, template_dir, template_name):
         result = self._reg.get((template_dir, template_name))

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 14:22:39 UTC (rev 101527)
+++ grokcore.view/branches/shared_templates_userwarning/src/grokcore/view/templatereg.txt	2009-07-04 14:22:56 UTC (rev 101528)
@@ -19,8 +19,8 @@
 
 We create our global template registry once::
 
-  >>> from grokcore.view.templatereg import TemplateRegistry
-  >>> reg = TemplateRegistry()
+  >>> from grokcore.view.templatereg import FileTemplateRegistry
+  >>> reg = FileTemplateRegistry()
 
 For testing purposes we will create a directory with two templates in it::
 
@@ -179,17 +179,36 @@
 
 Inline templates
 ----------------
-
 Inline templates are defined in a Python module instead of on the
 filesystem.
 
-We create an inline template::
+First we create a fake ModuleInfo class:
 
+   >>> class ModuleInfo(object):
+   ...     dotted_name = 'module'
 
+an create an instance of this class::
+
+   >>> module_info = ModuleInfo()
+
+Let's create a class for inline template and create an instance::
+
+  >>> class InlineTemplate(object):
+  ...     pass
+  >>> cavepainting = InlineTemplate()
+
+We create our global inline template registry once::
+
+  >>> from grokcore.view.templatereg import InlineTemplateRegistry
+  >>> inline_reg = InlineTemplateRegistry()
+
 Let's register an inline template with the registry::
 
+  >>> 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::
 
-It is an error to have an inline template with the same name as a filesystem
-template::
+  >>> sorted(inline_reg.unassociated())
+  [('module', 'cavepainting')]
 



More information about the Checkins mailing list