changeset 4:0be8714e4d6a

use the app and add it to the handler
author k0s <k0scist@gmail.com>
date Fri, 06 Nov 2009 16:47:27 -0500
parents 198d1cb34c14
children 86df7b4dbc9c
files genshi_view/template/+package+/dispatcher.py genshi_view/template/+package+/handlers.py
diffstat 2 files changed, 20 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/genshi_view/template/+package+/dispatcher.py	Thu Nov 05 23:18:22 2009 -0500
+++ b/genshi_view/template/+package+/dispatcher.py	Fri Nov 06 16:47:27 2009 -0500
@@ -19,7 +19,7 @@
         # set instance parameters from kw and defaults
         for key in self.defaults:
             setattr(self, key, kw.get(key, self.defaults[key]))
-#        self.auto_reload = self.auto_reload.lower() == 'true'
+        self.auto_reload = self.auto_reload.lower() == 'true'
 
         self.handlers = [ Index ]
 
@@ -27,9 +27,9 @@
         self.htdocs = resource_filename(__name__, 'static')
 
         # template loader # to move???
-#        templates_dir = resource_filename(__name__, 'templates')
-#        self.loader = TemplateLoader(templates_dir,
-#                                     auto_reload=self.auto_reload)
+        templates_dir = resource_filename(__name__, 'templates')
+        self.loader = TemplateLoader(templates_dir,
+                                     auto_reload=self.auto_reload)
 
     ### methods dealing with HTTP
     def __call__(self, environ, start_response):
@@ -44,7 +44,7 @@
 
         # match the request to a handler
         for h in self.handlers:
-            handler = h.match(request)
+            handler = h.match(self, request)
             if handler is not None:
                 break
         else:
--- a/genshi_view/template/+package+/handlers.py	Thu Nov 05 23:18:22 2009 -0500
+++ b/genshi_view/template/+package+/handlers.py	Fri Nov 06 16:47:27 2009 -0500
@@ -1,25 +1,30 @@
 from urlparse import urlparse
 from webob import Response, exc
 
-# template loader
-from genshi.template import TemplateLoader
-templates_dir = resource_filename(__name__, 'templates')
-loader = TemplateLoader(templates_dir,
-                        auto_reload=True) # XXX should come from .ini file
-
 class HandlerMatchException(Exception):
     """the handler doesn't match the request"""
 
 class Handler(object):
 
+    methods = set(['GET']) # methods to listen to
+    handler_path = [] # path elements to match        
+
     @classmethod
-    def match(cls, request):
+    def match(cls, app, request):
         try:
-            return cls(request)
+            return cls(app, request)
         except HandlerMatchException:
             return None
     
-    def __init__(self, request):
+    def __init__(self, app, request):
+        if request.method not in self.methods:
+            raise HandlerMatchException
+        self.path = request.path.info.strip('/').split('/')
+        if self.path == ['']:
+            self.path = []
+        if path[:len(handler_path)] != self.handler_path:
+            raise HandlerMatchException
+        self.app = app
         self.request = request
         self.application_path = urlparse(request.application_url)[2]
 
@@ -38,24 +43,13 @@
         raise exc.HTTPSeeOther(location=location)
 
 class GenshiHandler(Handler):
-    methods = set(['GET']) # methods to listen to
-    handler_path = [] # path elements to match        
 
     def __init__(self, request):
-        if request.method not in self.methods:
-            raise HandlerMatchException
-        self.path = request.path.info.strip('/').split('/')
-        if self.path == ['']:
-            self.path = []
-        if path[:len(handler_path)] != self.handler_path:
-            raise HandlerMatchException
-
         Handler.__init__(self, request)
-
         self.data = { 'request': request }
 
     def __call__(self):        
-        template = loader.load(self.template)
+        template = self.app.loader.load(self.template)
         return Response(content_type='text/html',
                         body=template.generate(**self.data).render()