mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
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:
parent
547f8d2e83
commit
3f26f9af5b
1 changed files with 26 additions and 16 deletions
|
|
@ -84,6 +84,21 @@ class Generator(object):
|
||||||
% (name, self._templates_path)))
|
% (name, self._templates_path)))
|
||||||
return self._templates[name]
|
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):
|
def get_files(self, path, exclude=[], extensions=None):
|
||||||
"""Return a list of files to use, based on rules
|
"""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
|
:param extensions: the list of allowed extensions (if False, all
|
||||||
extensions are allowed)
|
extensions are allowed)
|
||||||
"""
|
"""
|
||||||
if extensions is None:
|
|
||||||
extensions = self.markup
|
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
|
|
||||||
try:
|
if os.path.isdir(path):
|
||||||
iter = os.walk(path, followlinks=True)
|
for root, dirs, temp_files in os.walk(path, followlinks=True):
|
||||||
except TypeError: # python 2.5 does not support followlinks
|
for e in exclude:
|
||||||
iter = os.walk(path)
|
if e in dirs:
|
||||||
|
dirs.remove(e)
|
||||||
for root, dirs, temp_files in iter:
|
for f in temp_files:
|
||||||
for e in exclude:
|
fp = os.path.join(root, f)
|
||||||
if e in dirs:
|
if self._include_path(fp, extensions):
|
||||||
dirs.remove(e)
|
files.append(fp)
|
||||||
for f in temp_files:
|
elif os.path.exists(path) and self._include_path(path, extensions):
|
||||||
if extensions is False or \
|
files.append(path) # can't walk non-directories
|
||||||
(True in [f.endswith(ext) for ext in extensions]):
|
|
||||||
files.append(os.sep.join((root, f)))
|
|
||||||
return files
|
return files
|
||||||
|
|
||||||
def add_source_path(self, content):
|
def add_source_path(self, content):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue