]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Non-file-based template loaders should always use posix filename semantics
authorBen Darnell <ben@bendarnell.com>
Tue, 5 Jul 2011 05:34:13 +0000 (22:34 -0700)
committerBen Darnell <ben@bendarnell.com>
Tue, 5 Jul 2011 05:34:13 +0000 (22:34 -0700)
tornado/template.py

index f8b33bc69a85e2bc1845f36519990948e237405b..8963fd4528bc09d9b12801d512e41b87f3acf2ec 100644 (file)
@@ -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)