Permalink
Browse files

Non-file-based template loaders should always use posix filename sema…

…ntics
  • Loading branch information...
1 parent d732f9d commit 180b2136a1544d165a30cde4b4486b51e21bb452 @bdarnell committed Jul 5, 2011
Showing with 26 additions and 13 deletions.
  1. +26 −13 tornado/template.py
View
@@ -90,6 +90,7 @@ def add(x, y):
import datetime
import logging
import os.path
+import posixpath
import re
from tornado import escape
@@ -181,7 +182,7 @@ def _get_ancestors(self, loader):
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 @@ def __init__(self, root_directory, autoescape=_DEFAULT_AUTOESCAPE):
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 @@ def reset(self):
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 @@ def _create_template(self, name):
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)

0 comments on commit 180b213

Please sign in to comment.