1
0
Fork 0
forked from github/pelican

generators: Teach Generator.get_files to handle non-directory input

This makes it easier for StaticGenerator to walk FILES_TO_COPY, where
the input may be a directory or a bare filename.

Non-traversable file types (e.g. everything but directories and
symlinks to directories) are not checked against the exclude list.
The user-level effect of this is that explicit entries in STATIC_PATHS
or FILES_TO_COPY will override a hypothetical STATIC_EXCLUDES setting,
which seems like a reasonable approach.

I also removed the Python 2.5 compatibility check for `followlinks` in
os.walk, since Pelican is now Python >=2.7.
This commit is contained in:
W. Trevor King 2013-01-04 09:20:40 -05:00
commit 3f26f9af5b

View file

@ -84,6 +84,21 @@ class Generator(object):
% (name, self._templates_path)))
return self._templates[name]
def _include_path(self, path, extensions=None):
"""Inclusion logic for .get_files(), returns True/False
:param path: the path which might be including
:param extensions: the list of allowed extensions (if False, all
extensions are allowed)
"""
if extensions is None:
extensions = self.markup
basename = os.path.basename(path)
if extensions is False or \
(True in [basename.endswith(ext) for ext in extensions]):
return True
return False
def get_files(self, path, exclude=[], extensions=None):
"""Return a list of files to use, based on rules
@ -92,24 +107,19 @@ class Generator(object):
:param extensions: the list of allowed extensions (if False, all
extensions are allowed)
"""
if extensions is None:
extensions = self.markup
files = []
try:
iter = os.walk(path, followlinks=True)
except TypeError: # python 2.5 does not support followlinks
iter = os.walk(path)
for root, dirs, temp_files in iter:
if os.path.isdir(path):
for root, dirs, temp_files in os.walk(path, followlinks=True):
for e in exclude:
if e in dirs:
dirs.remove(e)
for f in temp_files:
if extensions is False or \
(True in [f.endswith(ext) for ext in extensions]):
files.append(os.sep.join((root, f)))
fp = os.path.join(root, f)
if self._include_path(fp, extensions):
files.append(fp)
elif os.path.exists(path) and self._include_path(path, extensions):
files.append(path) # can't walk non-directories
return files
def add_source_path(self, content):