Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a management command option to exclude files ... #1024

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions compressor/management/commands/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def add_arguments(self, parser):
"supported. It may be a specified more than once for "
"multiple engines. If not specified, django engine is used.",
dest="engines")
parser.add_argument('--exclude', '-x', default=[], action="append", dest='exclusions',
help="Add excluded files using a glob patter. It can be used "
"multiple times to add more exclusions." )

def get_loaders(self):
template_source_loaders = []
Expand Down Expand Up @@ -86,7 +89,7 @@ def __get_parser(self, engine):

return parser

def compress(self, engine, extensions, verbosity, follow_links, log):
def compress(self, engine, exclusions, extensions, verbosity, follow_links, log):
"""
Searches templates containing 'compress' nodes and compresses them
"offline" -- outside of the request/response cycle.
Expand Down Expand Up @@ -137,6 +140,12 @@ def compress(self, engine, extensions, verbosity, follow_links, log):
env.list_templates(filter_func=lambda _path:
os.path.splitext(_path)[-1] in extensions)])

excluded_templates = [t for t in templates if any([fnmatch(t,excl) for excl in exclusions])]
if excluded_templates:
if verbosity >=2:
log.write("Excluded templates:\n\t" + "\n\t".join(excluded_templates) + "\n")
templates = [t for t in templates if t not in excluded_templates]

if not templates:
raise OfflineGenerationError("No templates found. Make sure your "
"TEMPLATE_LOADERS and TEMPLATE_DIRS "
Expand Down Expand Up @@ -290,12 +299,13 @@ def handle_inner(self, **options):
follow_links = options.get("follow_links", False)
extensions = self.handle_extensions(options.get("extensions") or ["html"])
engines = [e.strip() for e in options.get("engines", [])] or ["django"]
exclusions = [e.strip() for e in options.get("exclusions", [])] or []

final_offline_manifest = {}
final_block_count = 0
final_results = []
for engine in engines:
offline_manifest, block_count, results = self.compress(engine, extensions, verbosity, follow_links, log)
offline_manifest, block_count, results = self.compress(engine, exclusions, extensions, verbosity, follow_links, log)
final_results.extend(results)
final_block_count += block_count
final_offline_manifest.update(offline_manifest)
Expand Down