# HG changeset patch
# User Chris Lambacher <chris@kateandchris.net>
# Date 1266875077 18000
# Node ID 764e8ea349b56b2810e2b55dfd609bced50ff4d7
# Parent  749b487fb15fd46c3465ccfda42d60497cb25be8
# Parent  0af7aa9ed0df6ee5fea265948c8ce93801bf27b6
Make dependence on ToscaWidgets, Genshi and Mako follow configuration.

diff -r 749b487fb15f -r 764e8ea349b5 tg/configuration.py
--- a/tg/configuration.py	Mon Feb 22 16:33:52 2010 -0500
+++ b/tg/configuration.py	Mon Feb 22 16:44:37 2010 -0500
@@ -7,7 +7,6 @@
 from UserDict import DictMixin
 
 from pylons.i18n import ugettext
-from genshi.filters import Translator
 
 from pylons.configuration import config as pylons_config
 from beaker.middleware import SessionMiddleware, CacheMiddleware
@@ -26,8 +25,6 @@
 from routes.middleware import RoutesMiddleware
 from webob import Request
 
-from tw.api import make_middleware as tw_middleware
-
 log = logging.getLogger(__name__)
 
 class TGConfigError(Exception):pass
@@ -354,7 +351,7 @@
         if use_dotted_templatenames:
             # Support dotted names by injecting a slightly different template
             # lookup system that will return templates from dotted template notation.
-            from tg.dottednamesupport import DottedTemplateLookup
+            from tg.dottednames.mako_lookup import DottedTemplateLookup
             config['pylons.app_globals'].mako_lookup = DottedTemplateLookup(
                 input_encoding='utf-8', output_encoding='utf-8',
                 imports=['from webhelpers.html import escape'],
@@ -392,7 +389,7 @@
         filter, template loader
 
         """
-        from tg.dottednamesupport import GenshiTemplateLoader
+        from tg.dottednames.genshi_lookup import GenshiTemplateLoader
         from tg.render import render_genshi
 
         def template_loaded(template):
@@ -450,6 +447,7 @@
         config['buffet.template_engines'].pop()
         template_location = '%s.templates' % self.package.__name__
         template_location = '%s.templates' % self.package.__name__
+        from genshi.filters import Translator
 
         def template_loaded(template):
             template.filters.insert(0, Translator(ugettext))
@@ -681,6 +679,8 @@
 
 
         """
+        from tw.api import make_middleware as tw_middleware
+
 
         twconfig = {'toscawidgets.framework.default_view': self.default_renderer,
                     'toscawidgets.framework.translator': ugettext,
diff -r 749b487fb15f -r 764e8ea349b5 tg/controllers/decoratedcontroller.py
--- a/tg/controllers/decoratedcontroller.py	Mon Feb 22 16:33:52 2010 -0500
+++ b/tg/controllers/decoratedcontroller.py	Mon Feb 22 16:44:37 2010 -0500
@@ -15,7 +15,9 @@
 from pylons.controllers.util import abort
 
 from repoze.what.predicates import NotAuthorizedError as WhatNotAuthorizedError, not_anonymous
-import tw
+
+# demand load tw (ToscaWidets) if needed
+tw = None
 
 from tg.render import render as tg_render
 from tg.decorators import expose
@@ -267,6 +269,10 @@
 
         #set up the tw renderer
         if engine_name in ('genshi','mako') and config['use_toscawidgets']:
+            global tw
+            if not tw:
+                import tw
+
             tw.framework.default_view = engine_name
 
         # Setup the template namespace, removing anything that the user
diff -r 749b487fb15f -r 764e8ea349b5 tg/dottednames/genshi_lookup.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tg/dottednames/genshi_lookup.py	Mon Feb 22 16:44:37 2010 -0500
@@ -0,0 +1,29 @@
+"""Reimplementation of the Genshi template loader that supports dotted names."""
+
+import os
+import stat
+
+from genshi.template import TemplateLoader
+
+import tg
+
+
+class GenshiTemplateLoader(TemplateLoader):
+    """Genshi template loader that supports
+    zipped applications and dotted filenames as well as path names
+    """
+
+    def load(self, filename, relative_to=None, cls=None, encoding=None):
+        """real loader function. copy paste from the mako template
+        loader.
+        """
+        # TODO: get the template extension from the config!!
+        if not filename.endswith('.html'):
+            filename = tg.config['pylons.app_globals'
+                    ].dotted_filename_finder.get_dotted_filename(
+                            template_name=filename,
+                            template_extension='.html')
+
+        return TemplateLoader.load(self, filename,
+                relative_to=relative_to, cls=cls, encoding=encoding)
+
diff -r 749b487fb15f -r 764e8ea349b5 tg/dottednames/mako_lookup.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tg/dottednames/mako_lookup.py	Mon Feb 22 16:44:37 2010 -0500
@@ -0,0 +1,174 @@
+"""Reimplementation of the Mako template loader that supports dotted names."""
+
+import os
+import stat
+
+try:
+    import threading
+except ImportError:
+    import dummy_threading as threading
+
+
+from mako.template import Template
+from paste.deploy.converters import asbool
+
+import tg
+
+
+class DottedTemplateLookup(object):
+    """Mako template lookup emulation that supports
+    zipped applications and dotted filenames.
+
+    This is an emulation of the Mako template lookup that will handle
+    get_template and support dotted names in Python path notation
+    to support zipped eggs.
+
+    This is necessary because Mako asserts that your project will always
+    be installed in a zip-unsafe manner with all files somewhere on the
+    hard drive.
+
+    This is not the case when you want your application to be deployed
+    in a single zip file (zip-safe). If you want to deploy in a zip
+    file _and_ use the dotted template name notation then this class
+    is necessary because it emulates files on the filesystem for the
+    underlying Mako engine while they are in fact in your zip file.
+
+    """
+
+    def __init__(self, input_encoding, output_encoding,
+            imports, default_filters, module_directory=None):
+
+        self.input_encoding = input_encoding
+        self.output_encoding = output_encoding
+        self.imports = imports
+        self.default_filters = default_filters
+        # implement a cache for the loaded templates
+        self.template_cache = dict()
+        # implement a cache for the filename lookups
+        self.template_filenames_cache = dict()
+        self.module_directory = module_directory
+
+        # a mutex to ensure thread safeness during template loading
+        self._mutex = threading.Lock()
+
+    def adjust_uri(self, uri, relativeto):
+        """Adjust the given uri relative to a filename.
+
+        This method is used by mako for filesystem based reasons.
+        In dotted lookup land we don't adjust uri so we just return
+        the value we are given without any change.
+
+        """
+        if uri.startswith('local:'):
+            uri = tg.config['pylons.package'] + '.' + uri[6:]
+
+        if '.' in uri:
+            # We are in the DottedTemplateLookup system so dots in
+            # names should be treated as a Python path. Since this
+            # method is called by template inheritance we must
+            # support dotted names also in the inheritance.
+            result = tg.config['pylons.app_globals'].\
+                dotted_filename_finder.get_dotted_filename(template_name=uri, template_extension='.mak')
+
+            if not uri in self.template_filenames_cache:
+                # feed our filename cache if needed.
+                self.template_filenames_cache[uri] = result
+
+        else:
+            # no dot detected, just return plain name
+            result = uri
+
+        return result
+
+    def __check(self, template):
+        """private method used to verify if a template has changed
+        since the last time it has been put in cache...
+
+        This method being based on the mtime of a real file this should
+        never be called on a zipped deployed application.
+
+        This method is a ~copy/paste of the original caching system from
+        the Mako lookup loader.
+
+        """
+        if template.filename is None:
+            return template
+
+
+        if not os.path.exists(template.filename):
+            # remove from cache.
+            self.template_cache.pop(template.filename, None)
+            raise exceptions.TemplateLookupException(
+                    "Cant locate template '%s'" % template.filename)
+
+        elif template.module._modified_time < os.stat(
+                template.filename)[stat.ST_MTIME]:
+
+            # cache is too old, remove old template
+            # from cache and reload.
+            self.template_cache.pop(template.filename, None)
+            return self.__load(template.filename)
+
+        else:
+            # cache is correct, use it.
+            return template
+
+    def __load(self, filename):
+        """real loader function. copy paste from the mako template
+        loader.
+
+        """
+        # make sure the template loading from filesystem is only done
+        # one thread at a time to avoid bad clashes...
+        self._mutex.acquire()
+        try:
+            try:
+                # try returning from cache one more time in case
+                # concurrent thread already loaded
+                return self.template_cache[filename]
+
+            except KeyError:
+                # not in cache yet... we can continue normally
+                pass
+
+            try:
+                self.template_cache[filename] = Template(
+                    filename=filename,
+                    module_directory=self.module_directory,
+                    input_encoding=self.input_encoding,
+                    output_encoding=self.output_encoding,
+                    default_filters=self.default_filters,
+                    imports=self.imports,
+                    lookup=self)
+
+                return self.template_cache[filename]
+
+            except:
+                self.template_cache.pop(filename, None)
+                raise
+
+        finally:
+            # _always_ release the lock once done to avoid
+            # "thread lock" effect
+            self._mutex.release()
+
+    def get_template(self, template_name):
+        """this is the emulated method that must return a template
+        instance based on a given template name
+        """
+
+        if not self.template_cache.has_key(template_name):
+            # the template string is not yet loaded into the cache.
+            # Do so now
+            self.__load(template_name)
+
+        if asbool(tg.config.get('templating.mako.reloadfromdisk', 'false')):
+            # AUTO RELOADING will be activated only if user has
+            # explicitly asked for it in the configuration
+            # return the template, but first make sure it's not outdated
+            # and if outdated, refresh the cache.
+            return self.__check(self.template_cache[template_name])
+
+        else:
+            return self.template_cache[template_name]
+
diff -r 749b487fb15f -r 764e8ea349b5 tg/dottednamesupport.py
--- a/tg/dottednamesupport.py	Mon Feb 22 16:33:52 2010 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,196 +0,0 @@
-"""Reimplementation of the Mako template loader that supports dotted names."""
-
-import os
-import stat
-
-try:
-    import threading
-except ImportError:
-    import dummy_threading as threading
-
-
-from pkg_resources import resource_filename
-from mako.template import Template
-from genshi.template import TemplateLoader
-from paste.deploy.converters import asbool
-
-import tg
-
-
-class DottedTemplateLookup(object):
-    """Mako template lookup emulation that supports
-    zipped applications and dotted filenames.
-
-    This is an emulation of the Mako template lookup that will handle
-    get_template and support dotted names in Python path notation
-    to support zipped eggs.
-
-    This is necessary because Mako asserts that your project will always
-    be installed in a zip-unsafe manner with all files somewhere on the
-    hard drive.
-
-    This is not the case when you want your application to be deployed
-    in a single zip file (zip-safe). If you want to deploy in a zip
-    file _and_ use the dotted template name notation then this class
-    is necessary because it emulates files on the filesystem for the
-    underlying Mako engine while they are in fact in your zip file.
-
-    """
-
-    def __init__(self, input_encoding, output_encoding,
-            imports, default_filters, module_directory=None):
-
-        self.input_encoding = input_encoding
-        self.output_encoding = output_encoding
-        self.imports = imports
-        self.default_filters = default_filters
-        # implement a cache for the loaded templates
-        self.template_cache = dict()
-        # implement a cache for the filename lookups
-        self.template_filenames_cache = dict()
-        self.module_directory = module_directory
-
-        # a mutex to ensure thread safeness during template loading
-        self._mutex = threading.Lock()
-
-    def adjust_uri(self, uri, relativeto):
-        """Adjust the given uri relative to a filename.
-
-        This method is used by mako for filesystem based reasons.
-        In dotted lookup land we don't adjust uri so we just return
-        the value we are given without any change.
-
-        """
-        if uri.startswith('local:'):
-            uri = tg.config['pylons.package'] + '.' + uri[6:]
-
-        if '.' in uri:
-            # We are in the DottedTemplateLookup system so dots in
-            # names should be treated as a Python path. Since this
-            # method is called by template inheritance we must
-            # support dotted names also in the inheritance.
-            result = tg.config['pylons.app_globals'].\
-                dotted_filename_finder.get_dotted_filename(template_name=uri, template_extension='.mak')
-
-            if not uri in self.template_filenames_cache:
-                # feed our filename cache if needed.
-                self.template_filenames_cache[uri] = result
-
-        else:
-            # no dot detected, just return plain name
-            result = uri
-
-        return result
-
-    def __check(self, template):
-        """private method used to verify if a template has changed
-        since the last time it has been put in cache...
-
-        This method being based on the mtime of a real file this should
-        never be called on a zipped deployed application.
-
-        This method is a ~copy/paste of the original caching system from
-        the Mako lookup loader.
-
-        """
-        if template.filename is None:
-            return template
-
-
-        if not os.path.exists(template.filename):
-            # remove from cache.
-            self.template_cache.pop(template.filename, None)
-            raise exceptions.TemplateLookupException(
-                    "Cant locate template '%s'" % template.filename)
-
-        elif template.module._modified_time < os.stat(
-                template.filename)[stat.ST_MTIME]:
-
-            # cache is too old, remove old template
-            # from cache and reload.
-            self.template_cache.pop(template.filename, None)
-            return self.__load(template.filename)
-
-        else:
-            # cache is correct, use it.
-            return template
-
-    def __load(self, filename):
-        """real loader function. copy paste from the mako template
-        loader.
-
-        """
-        # make sure the template loading from filesystem is only done
-        # one thread at a time to avoid bad clashes...
-        self._mutex.acquire()
-        try:
-            try:
-                # try returning from cache one more time in case
-                # concurrent thread already loaded
-                return self.template_cache[filename]
-
-            except KeyError:
-                # not in cache yet... we can continue normally
-                pass
-
-            try:
-                self.template_cache[filename] = Template(
-                    filename=filename,
-                    module_directory=self.module_directory,
-                    input_encoding=self.input_encoding,
-                    output_encoding=self.output_encoding,
-                    default_filters=self.default_filters,
-                    imports=self.imports,
-                    lookup=self)
-
-                return self.template_cache[filename]
-
-            except:
-                self.template_cache.pop(filename, None)
-                raise
-
-        finally:
-            # _always_ release the lock once done to avoid
-            # "thread lock" effect
-            self._mutex.release()
-
-    def get_template(self, template_name):
-        """this is the emulated method that must return a template
-        instance based on a given template name
-        """
-
-        if not self.template_cache.has_key(template_name):
-            # the template string is not yet loaded into the cache.
-            # Do so now
-            self.__load(template_name)
-
-        if asbool(tg.config.get('templating.mako.reloadfromdisk', 'false')):
-            # AUTO RELOADING will be activated only if user has
-            # explicitly asked for it in the configuration
-            # return the template, but first make sure it's not outdated
-            # and if outdated, refresh the cache.
-            return self.__check(self.template_cache[template_name])
-
-        else:
-            return self.template_cache[template_name]
-
-
-class GenshiTemplateLoader(TemplateLoader):
-    """Genshi template loader that supports
-    zipped applications and dotted filenames as well as path names
-    """
-
-    def load(self, filename, relative_to=None, cls=None, encoding=None):
-        """real loader function. copy paste from the mako template
-        loader.
-        """
-        # TODO: get the template extension from the config!!
-        if not filename.endswith('.html'):
-            filename = tg.config['pylons.app_globals'
-                    ].dotted_filename_finder.get_dotted_filename(
-                            template_name=filename,
-                            template_extension='.html')
-
-        return TemplateLoader.load(self, filename,
-                relative_to=relative_to, cls=cls, encoding=encoding)
-
diff -r 749b487fb15f -r 764e8ea349b5 tg/render.py
--- a/tg/render.py	Mon Feb 22 16:33:52 2010 -0500
+++ b/tg/render.py	Mon Feb 22 16:44:37 2010 -0500
@@ -1,6 +1,5 @@
 from urllib import quote_plus
 
-from genshi import HTML, XML
 from pylons.configuration import config
 from paste.deploy.converters import asbool
 from pylons import (app_globals, session, tmpl_context, request,
@@ -153,8 +152,14 @@
     return render_genshi(template_name, template_vars, **kwargs)
 
 
+# demand load these items from Genshi if needed
+HTML = XML = None
 def render_genshi(template_name, template_vars, **kwargs):
     """Render the template_vars with the Genshi template"""
+    global HTML,XML
+    if not HTML or not XML:
+        from genshi import HTML,XML
+
     template_vars.update(HTML=HTML, XML=XML)
 
     if config.get('use_dotted_templatenames', False):

