diff options
Diffstat (limited to 'src/jinja2/loaders.py')
-rw-r--r-- | src/jinja2/loaders.py | 70 |
1 files changed, 38 insertions, 32 deletions
diff --git a/src/jinja2/loaders.py b/src/jinja2/loaders.py index 86049f7..fa91a73 100644 --- a/src/jinja2/loaders.py +++ b/src/jinja2/loaders.py @@ -31,12 +31,14 @@ def split_template_path(template): '..' in the path it will raise a `TemplateNotFound` error. """ pieces = [] - for piece in template.split('/'): - if path.sep in piece \ - or (path.altsep and path.altsep in piece) or \ - piece == path.pardir: + for piece in template.split("/"): + if ( + path.sep in piece + or (path.altsep and path.altsep in piece) + or piece == path.pardir + ): raise TemplateNotFound(template) - elif piece and piece != '.': + elif piece and piece != ".": pieces.append(piece) return pieces @@ -93,15 +95,16 @@ class BaseLoader(object): the template will be reloaded. """ if not self.has_source_access: - raise RuntimeError('%s cannot provide access to the source' % - self.__class__.__name__) + raise RuntimeError( + "%s cannot provide access to the source" % self.__class__.__name__ + ) raise TemplateNotFound(template) def list_templates(self): """Iterates over all templates. If the loader does not support that it should raise a :exc:`TypeError` which is the default behavior. """ - raise TypeError('this loader cannot iterate over all templates') + raise TypeError("this loader cannot iterate over all templates") @internalcode def load(self, environment, name, globals=None): @@ -138,8 +141,9 @@ class BaseLoader(object): bucket.code = code bcc.set_bucket(bucket) - return environment.template_class.from_code(environment, code, - globals, uptodate) + return environment.template_class.from_code( + environment, code, globals, uptodate + ) class FileSystemLoader(BaseLoader): @@ -164,10 +168,9 @@ class FileSystemLoader(BaseLoader): The ``followlinks`` parameter was added. """ - def __init__(self, searchpath, encoding='utf-8', followlinks=False): - if ( - not isinstance(searchpath, abc.Iterable) - or isinstance(searchpath, string_types) + def __init__(self, searchpath, encoding="utf-8", followlinks=False): + if not isinstance(searchpath, abc.Iterable) or isinstance( + searchpath, string_types ): searchpath = [searchpath] @@ -197,6 +200,7 @@ class FileSystemLoader(BaseLoader): return path.getmtime(filename) == mtime except OSError: return False + return contents, filename, uptodate raise TemplateNotFound(template) @@ -206,10 +210,12 @@ class FileSystemLoader(BaseLoader): walk_dir = os.walk(searchpath, followlinks=self.followlinks) for dirpath, dirnames, filenames in walk_dir: for filename in filenames: - template = os.path.join(dirpath, filename) \ - [len(searchpath):].strip(os.path.sep) \ - .replace(os.path.sep, '/') - if template[:2] == './': + template = ( + os.path.join(dirpath, filename)[len(searchpath) :] + .strip(os.path.sep) + .replace(os.path.sep, "/") + ) + if template[:2] == "./": template = template[2:] if template not in found: found.add(template) @@ -305,6 +311,7 @@ class PackageLoader(BaseLoader): def up_to_date(): return os.path.isfile(p) and os.path.getmtime(p) == mtime + else: # Package is a zip file. try: @@ -341,7 +348,7 @@ class PackageLoader(BaseLoader): # Package is a zip file. prefix = ( - self._template_root[len(self._archive):].lstrip(os.path.sep) + self._template_root[len(self._archive) :].lstrip(os.path.sep) + os.path.sep ) offset = len(prefix) @@ -422,7 +429,7 @@ class PrefixLoader(BaseLoader): by loading ``'app2/index.html'`` the file from the second. """ - def __init__(self, mapping, delimiter='/'): + def __init__(self, mapping, delimiter="/"): self.mapping = mapping self.delimiter = delimiter @@ -522,22 +529,20 @@ class ModuleLoader(BaseLoader): has_source_access = False def __init__(self, path): - package_name = '_jinja2_module_templates_%x' % id(self) + package_name = "_jinja2_module_templates_%x" % id(self) # create a fake module that looks for the templates in the # path given. mod = _TemplateModule(package_name) - if ( - not isinstance(path, abc.Iterable) - or isinstance(path, string_types) - ): + if not isinstance(path, abc.Iterable) or isinstance(path, string_types): path = [path] mod.__path__ = [fspath(p) for p in path] - sys.modules[package_name] = weakref.proxy(mod, - lambda x: sys.modules.pop(package_name, None)) + sys.modules[package_name] = weakref.proxy( + mod, lambda x: sys.modules.pop(package_name, None) + ) # the only strong reference, the sys.modules entry is weak # so that the garbage collector can remove it once the @@ -547,20 +552,20 @@ class ModuleLoader(BaseLoader): @staticmethod def get_template_key(name): - return 'tmpl_' + sha1(name.encode('utf-8')).hexdigest() + return "tmpl_" + sha1(name.encode("utf-8")).hexdigest() @staticmethod def get_module_filename(name): - return ModuleLoader.get_template_key(name) + '.py' + return ModuleLoader.get_template_key(name) + ".py" @internalcode def load(self, environment, name, globals=None): key = self.get_template_key(name) - module = '%s.%s' % (self.package_name, key) + module = "%s.%s" % (self.package_name, key) mod = getattr(self.module, module, None) if mod is None: try: - mod = __import__(module, None, None, ['root']) + mod = __import__(module, None, None, ["root"]) except ImportError: raise TemplateNotFound(name) @@ -569,4 +574,5 @@ class ModuleLoader(BaseLoader): sys.modules.pop(module, None) return environment.template_class.from_module_dict( - environment, mod.__dict__, globals) + environment, mod.__dict__, globals + ) |