Skip to content

Commit

Permalink
Issue #71: Add followlinks parameter to FileSystemLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
berkerpeksag committed Sep 13, 2013
1 parent 63227f0 commit 1eeaccc
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions jinja2/loaders.py
Expand Up @@ -141,20 +141,28 @@ class FileSystemLoader(BaseLoader):
The loader takes the path to the templates as string, or if multiple The loader takes the path to the templates as string, or if multiple
locations are wanted a list of them which is then looked up in the locations are wanted a list of them which is then looked up in the
given order: given order::
>>> loader = FileSystemLoader('/path/to/templates') >>> loader = FileSystemLoader('/path/to/templates')
>>> loader = FileSystemLoader(['/path/to/templates', '/other/path']) >>> loader = FileSystemLoader(['/path/to/templates', '/other/path'])
Per default the template encoding is ``'utf-8'`` which can be changed Per default the template encoding is ``'utf-8'`` which can be changed
by setting the `encoding` parameter to something else. by setting the `encoding` parameter to something else.
To follow symbolic links, set the *followlinks* parameter to ``True``::
>>> loader = FileSystemLoader('/path/to/templates', followlinks=True)
.. versionchanged:: 2.8+
The *followlinks* parameter was added.
""" """


def __init__(self, searchpath, encoding='utf-8'): def __init__(self, searchpath, encoding='utf-8', followlinks=False):
if isinstance(searchpath, string_types): if isinstance(searchpath, string_types):
searchpath = [searchpath] searchpath = [searchpath]
self.searchpath = list(searchpath) self.searchpath = list(searchpath)
self.encoding = encoding self.encoding = encoding
self.followlinks = followlinks


def get_source(self, environment, template): def get_source(self, environment, template):
pieces = split_template_path(template) pieces = split_template_path(template)
Expand All @@ -169,6 +177,7 @@ def get_source(self, environment, template):
f.close() f.close()


mtime = path.getmtime(filename) mtime = path.getmtime(filename)

def uptodate(): def uptodate():
try: try:
return path.getmtime(filename) == mtime return path.getmtime(filename) == mtime
Expand All @@ -180,7 +189,8 @@ def uptodate():
def list_templates(self): def list_templates(self):
found = set() found = set()
for searchpath in self.searchpath: for searchpath in self.searchpath:
for dirpath, dirnames, filenames in os.walk(searchpath): walk_dir = os.walk(searchpath, followlinks=self.followlinks)
for dirpath, dirnames, filenames in walk_dir:
for filename in filenames: for filename in filenames:
template = os.path.join(dirpath, filename) \ template = os.path.join(dirpath, filename) \
[len(searchpath):].strip(os.path.sep) \ [len(searchpath):].strip(os.path.sep) \
Expand Down

0 comments on commit 1eeaccc

Please sign in to comment.