Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #2395: mako_caching.diff

File mako_caching.diff, 5.1 KB (added by lambacck, 2 years ago)

Fix mako caching problem

  • tg/configuration.py

    # HG changeset patch
    # User Chris Lambacher <chris@kateandchris.net>
    # Date 1266868943 18000
    # Node ID db7a8350abe98958e92e4a9c43f58b3e4c220106
    # Parent  ba1403f922d011ea3c1ac4b842f3c80831c052e1
    Fix module_directory missing in dotted names for Mako
    
    Bug: 2395
    
    diff -r ba1403f922d0 -r db7a8350abe9 tg/configuration.py
    a b  
    309309        """ 
    310310         
    311311        from tg.render import render_mako 
     312 
     313        compiled_dir = tg.config.get('templating.mako.compiled_templates_dir', None) 
     314        if not compiled_dir: 
     315            # Try each given templates path (when are they > 1 ?) for writability.. 
     316            for template_path in self.paths['templates']: 
     317                if os.access(template_path, os.W_OK): 
     318                    compiled_dir = template_path 
     319                    break # first match is as good as any 
     320             
     321            # Last recourse: project-dir/data/templates (pylons' default directory) 
     322            if not compiled_dir: 
     323                root = os.path.dirname(os.path.abspath(self.package.__file__)) 
     324                pylons_default_path = os.path.join(root, '../data/templates') 
     325                if os.access(pylons_default_path, os.W_OK): 
     326                    compiled_dir = pylons_default_path 
     327     
     328                if not compiled_dir: 
     329                    raise IOError("None of your templates directory, %s, are writable for compiled " 
     330                        "templates. Please set the templating.mako.compiled_templates_dir " 
     331                        "variable in your .ini file" % str(self.paths['templates'])) 
     332             
    312333         
    313334        if not use_dotted_templatenames: 
    314335            use_dotted_templatenames = asbool(config.get('use_dotted_templatenames', 'true')) 
     
    320341            config['pylons.app_globals'].mako_lookup = DottedTemplateLookup( 
    321342                input_encoding='utf-8', output_encoding='utf-8', 
    322343                imports=['from webhelpers.html import escape'], 
    323                 default_filters=['escape']) 
     344                default_filters=['escape'], module_directory=compiled_dir) 
    324345 
    325346        else: 
    326347            # If no dotted names support was required we will just setup 
    327348            # a file system based template lookup mechanism. 
    328             compiled_dir = tg.config.get('templating.mako.compiled_templates_dir', None) 
    329349 
    330             if not compiled_dir: 
    331                 # Try each given templates path (when are they > 1 ?) for writability.. 
    332                 for template_path in self.paths['templates']: 
    333                     if os.access(template_path, os.W_OK): 
    334                         compiled_dir = template_path 
    335                         break # first match is as good as any 
    336                  
    337                 # Last recourse: project-dir/data/templates (pylons' default directory) 
    338                 if not compiled_dir: 
    339                     root = os.path.dirname(os.path.abspath(self.package.__file__)) 
    340                     pylons_default_path = os.path.join(root, '../data/templates') 
    341                     if os.access(pylons_default_path, os.W_OK): 
    342                         compiled_dir = pylons_default_path 
    343          
    344                     if not compiled_dir: 
    345                         raise IOError("None of your templates directory, %s, are writable for compiled " 
    346                             "templates. Please set the templating.mako.compiled_templates_dir " 
    347                             "variable in your .ini file" % str(self.paths['templates'])) 
    348              
    349350            from mako.lookup import TemplateLookup 
    350351            config['pylons.app_globals'].mako_lookup = TemplateLookup( 
    351352                directories=self.paths['templates'], 
  • tg/dottednames/mako_lookup.py

    diff -r ba1403f922d0 -r db7a8350abe9 tg/dottednames/mako_lookup.py
    a b  
    3636    """ 
    3737 
    3838    def __init__(self, input_encoding, output_encoding, 
    39             imports, default_filters): 
     39            imports, default_filters, module_directory=None): 
    4040 
    4141        self.input_encoding = input_encoding 
    4242        self.output_encoding = output_encoding 
     
    4646        self.template_cache = dict() 
    4747        # implement a cache for the filename lookups 
    4848        self.template_filenames_cache = dict() 
     49        self.module_directory = module_directory 
    4950 
    5051        # a mutex to ensure thread safeness during template loading 
    5152        self._mutex = threading.Lock() 
     
    131132                pass 
    132133 
    133134            try: 
    134                 self.template_cache[filename] = Template(open(filename).read(), 
     135                self.template_cache[filename] = Template( 
    135136                    filename=filename, 
    136137                    input_encoding=self.input_encoding, 
    137138                    output_encoding=self.output_encoding, 
    138139                    default_filters=self.default_filters, 
    139140                    imports=self.imports, 
     141                    module_directory=self.module_directory, 
    140142                    lookup=self) 
    141143 
    142144                return self.template_cache[filename]