From c04d8a6ff65caa4adea783a863a8d67155db43a3 Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Tue, 8 Jan 2013 23:00:48 -0800 Subject: [PATCH] Remove legacy buffet template renderer abstraction layer. --- r2/r2/config/environment.py | 43 +++++++++++++++++++++------------ r2/r2/controllers/error.py | 3 +-- r2/r2/lib/manager/tp_manager.py | 8 +++--- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/r2/r2/config/environment.py b/r2/r2/config/environment.py index 26d281467..1bed06d9b 100644 --- a/r2/r2/config/environment.py +++ b/r2/r2/config/environment.py @@ -23,6 +23,8 @@ import os import mimetypes +from mako.lookup import TemplateLookup +from pylons.error import handle_mako_error from pylons import config import r2.config @@ -72,28 +74,39 @@ def load_environment(global_conf={}, app_conf={}, setup_globals=True): #override the default response options config['pylons.response_options']['headers'] = {} - # The following template options are passed to your template engines - tmpl_options = config['buffet.template_options'] - tmpl_options['mako.filesystem_checks'] = getattr(g, 'reload_templates', False) - tmpl_options['mako.default_filters'] = ["mako_websafe"] - tmpl_options['mako.imports'] = \ - ["from r2.lib.filters import websafe, unsafe, mako_websafe", - "from pylons import c, g, request", - "from pylons.i18n import _, ungettext"] - # when mako loads a previously compiled template file from its cache, it # doesn't check that the original template path matches the current path. # in the event that a new plugin defines a template overriding a reddit # template, unless the mtime newer, mako doesn't update the compiled # template. as a workaround, this makes mako store compiled templates with # the original path in the filename, forcing it to update with the path. - def mako_module_path(filename, uri): - module_directory = tmpl_options['mako.module_directory'] - filename = filename.lstrip('/').replace('/', '-') - path = os.path.join(module_directory, filename + ".py") - return os.path.abspath(path) + if "cache_dir" in app_conf: + module_directory = os.path.join(app_conf['cache_dir'], 'templates') - tmpl_options['mako.modulename_callable'] = mako_module_path + def mako_module_path(filename, uri): + filename = filename.lstrip('/').replace('/', '-') + path = os.path.join(module_directory, filename + ".py") + return os.path.abspath(path) + else: + # we're probably in "paster run standalone" mode. we'll just avoid + # caching templates since we don't know where they should go. + module_directory = mako_module_path = None + + # set up the templating system + config["pylons.g"].mako_lookup = TemplateLookup( + directories=paths["templates"], + error_handler=handle_mako_error, + module_directory=module_directory, + input_encoding="utf-8", + default_filters=["mako_websafe"], + filesystem_checks=getattr(g, "reload_templates", False), + imports=[ + "from r2.lib.filters import websafe, unsafe, mako_websafe", + "from pylons import c, g, request", + "from pylons.i18n import _, ungettext", + ], + modulename_callable=mako_module_path, + ) if setup_globals: g.setup_complete() diff --git a/r2/r2/controllers/error.py b/r2/r2/controllers/error.py index f245f3be8..d7bde4707 100644 --- a/r2/r2/controllers/error.py +++ b/r2/r2/controllers/error.py @@ -142,8 +142,7 @@ class ErrorController(RedditController): else: template_name = '/ratelimit_throttled.html' - loader = pylons.buffet.engines['mako']['engine'] - template = loader.load_template(template_name) + template = g.mako_lookup.get_template(template_name) return template.render(logo_url=static(g.default_header_url)) def send503(self): diff --git a/r2/r2/lib/manager/tp_manager.py b/r2/r2/lib/manager/tp_manager.py index 408cf25a3..052a5a4fa 100644 --- a/r2/r2/lib/manager/tp_manager.py +++ b/r2/r2/lib/manager/tp_manager.py @@ -20,7 +20,7 @@ # Inc. All Rights Reserved. ############################################################################### -import pylons +from pylons import g import hashlib from mako.template import Template as mTemplate from mako.exceptions import TemplateLookupException @@ -31,9 +31,8 @@ from r2.lib.utils import Storage import inspect, re, os class tp_manager: - def __init__(self, engine = 'mako', template_cls = mTemplate): + def __init__(self, template_cls=mTemplate): self.templates = {} - self.engine = engine self.Template = template_cls def add(self, name, style, file = None): @@ -70,8 +69,7 @@ class tp_manager: break else: try: - _loader = pylons.buffet.engines[self.engine]['engine'] - template = _loader.load_template(template_or_name) + template = g.mako_lookup.get_template(template_or_name) if cache: self.templates[key] = template # also store a hash for the template