[Checkins] SVN: Sandbox/janwijbrand/grokcore.view/src/grokcore/view/ sketches of how things could work after discussions w/faassen

Jan-Wijbrand Kolman janwijbrand at gmail.com
Thu Sep 18 16:03:14 EDT 2008


Log message for revision 91238:
  sketches of how things could work after discussions w/faassen

Changed:
  U   Sandbox/janwijbrand/grokcore.view/src/grokcore/view/directive.py
  U   Sandbox/janwijbrand/grokcore.view/src/grokcore/view/meta.py

-=-
Modified: Sandbox/janwijbrand/grokcore.view/src/grokcore/view/directive.py
===================================================================
--- Sandbox/janwijbrand/grokcore.view/src/grokcore/view/directive.py	2008-09-18 20:01:57 UTC (rev 91237)
+++ Sandbox/janwijbrand/grokcore.view/src/grokcore/view/directive.py	2008-09-18 20:03:13 UTC (rev 91238)
@@ -13,6 +13,8 @@
 ##############################################################################
 """Grok directives.
 """
+import os.path
+
 import martian
 from martian.error import GrokImportError
 from martian.directive import StoreOnce
@@ -24,6 +26,14 @@
     store = martian.ONCE
     validate = martian.validateText
 
+    def factory(self, value):
+        # Details, details, details: we can get to the filename of the
+        # frame. We could've used the inspect module, but its trying
+        # so hard to solve all kinds of different cases, it probabley
+        # doing to much.
+        path, _ = os.path.splitext(self.frame.f_code.co_filename)
+        return (path, value)
+
 class templatedir(martian.Directive):
     scope = martian.MODULE
     store = martian.ONCE

Modified: Sandbox/janwijbrand/grokcore.view/src/grokcore/view/meta.py
===================================================================
--- Sandbox/janwijbrand/grokcore.view/src/grokcore/view/meta.py	2008-09-18 20:01:57 UTC (rev 91237)
+++ Sandbox/janwijbrand/grokcore.view/src/grokcore/view/meta.py	2008-09-18 20:03:13 UTC (rev 91238)
@@ -40,12 +40,30 @@
 def default_fallback_to_name(factory, module, name, **data):
     return name
 
+def find_template_dir(viewclass):
+    templatedir = grokcore.view.templatedir.bind().get(viewclass)
+    if templatedir is None:
+        # Think of a template dir ourselves based on the path of the
+        # factory's module.
+        modulename = viewclass.__module__.split('.')[-1]
+        templatedir = modulename + '_templates'
+    return templatedir
 
+def find_implicit_template(viewclass):
+    templatedir = find_template_dir(viewclass)
+    templatename = viewclass.__class__.__name__.lower()
+
+    # If the found template exists, we're done. Else we walk up the mro
+    # to find possible implictely associated templates.
+
+    return templatedir, templatename
+
 class ViewGrokker(martian.ClassGrokker):
     martian.component(components.View)
     martian.directive(grokcore.component.context)
     martian.directive(grokcore.view.layer, default=IDefaultBrowserLayer)
     martian.directive(grokcore.component.name, get_default=default_view_name)
+    martian.directive(grokcore.view.template)
 
     def grok(self, name, factory, module_info, **kw):
         # Need to store the module info object on the view class so that it
@@ -53,7 +71,16 @@
         factory.module_info = module_info
         return super(ViewGrokker, self).grok(name, factory, module_info, **kw)
 
-    def execute(self, factory, config, context, layer, name, **kw):
+    def execute(self, factory, config, context, layer, name, template, **kw):
+        if template is not None:
+            templatedir = find_template_dir(factory)
+        else:
+            # Search implicitely associated template for factory or one of
+            # its baseclasses. The more specific the better.
+            templatedir, template = find_implicit_template(factory)
+
+        # now do something with the found template!
+
         # find templates
         templates = factory.module_info.getAnnotation('grok.templates', None)
         if templates is not None:



More information about the Checkins mailing list