From: Ben Darnell Date: Tue, 5 Jul 2011 05:34:13 +0000 (-0700) Subject: Non-file-based template loaders should always use posix filename semantics X-Git-Tag: v2.1.0~111 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=180b2136a1544d165a30cde4b4486b51e21bb452;p=thirdparty%2Ftornado.git Non-file-based template loaders should always use posix filename semantics --- diff --git a/tornado/template.py b/tornado/template.py index f8b33bc69..8963fd452 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -90,6 +90,7 @@ import cStringIO import datetime import logging import os.path +import posixpath import re from tornado import escape @@ -181,7 +182,7 @@ class Template(object): class BaseLoader(object): """Base class for template loaders.""" - def __init__(self, root_directory, autoescape=_DEFAULT_AUTOESCAPE): + def __init__(self, autoescape=_DEFAULT_AUTOESCAPE): """Creates a template loader. root_directory may be the empty string if this loader does not @@ -190,7 +191,6 @@ class BaseLoader(object): autoescape must be either None or a string naming a function in the template namespace, such as "xhtml_escape". """ - self.root = os.path.abspath(root_directory) self.autoescape = autoescape self.templates = {} @@ -200,15 +200,7 @@ class BaseLoader(object): def resolve_path(self, name, parent_path=None): """Converts a possibly-relative path to absolute (used internally).""" - if parent_path and not parent_path.startswith("<") and \ - not parent_path.startswith("/") and \ - not name.startswith("/"): - current_path = os.path.join(self.root, parent_path) - file_dir = os.path.dirname(os.path.abspath(current_path)) - relative_path = os.path.abspath(os.path.join(file_dir, name)) - if relative_path.startswith(self.root): - name = relative_path[len(self.root) + 1:] - return name + raise NotImplementedError() def load(self, name, parent_path=None): """Loads a template.""" @@ -228,7 +220,20 @@ class Loader(BaseLoader): they are loaded the first time. """ def __init__(self, root_directory, **kwargs): - super(Loader, self).__init__(root_directory, **kwargs) + super(Loader, self).__init__(**kwargs) + self.root = os.path.abspath(root_directory) + + + def resolve_path(self, name, parent_path=None): + if parent_path and not parent_path.startswith("<") and \ + not parent_path.startswith("/") and \ + not name.startswith("/"): + current_path = os.path.join(self.root, parent_path) + file_dir = os.path.dirname(os.path.abspath(current_path)) + relative_path = os.path.abspath(os.path.join(file_dir, name)) + if relative_path.startswith(self.root): + name = relative_path[len(self.root) + 1:] + return name def _create_template(self, name): path = os.path.join(self.root, name) @@ -241,9 +246,17 @@ class Loader(BaseLoader): class DictLoader(BaseLoader): """A template loader that loads from a dictionary.""" def __init__(self, dict, **kwargs): - super(DictLoader, self).__init__("", **kwargs) + super(DictLoader, self).__init__(**kwargs) self.dict = dict + def resolve_path(self, name, parent_path=None): + if parent_path and not parent_path.startswith("<") and \ + not parent_path.startswith("/") and \ + not name.startswith("/"): + file_dir = posixpath.dirname(parent_path) + name = posixpath.normpath(posixpath.join(file_dir, name)) + return name + def _create_template(self, name): return Template(self.dict[name], name=name, loader=self)