django-imagekit/imagekit/management/commands/warmimagecache.py
Matthew Tretter 7bc82d3624 Remove arguments from generate() method
Previously, we had two places where we were passing kwargs that affected
the image generation: the ImageSpec constructor and the generate method.
These were essentially partial applications. With this commit, there's
only one partial application (when the spec is instantiated), and the
generate method is called without arguments. Therefore, specs can now
be treated as generic generators whose constructors just happen to
accept a source_file.
2012-12-01 21:20:33 -05:00

35 lines
1.7 KiB
Python

from django.core.management.base import BaseCommand
import re
from ...files import GeneratedImageCacheFile
from ...registry import generator_registry, source_group_registry
class Command(BaseCommand):
help = ('Warm the image cache for the specified specs (or all specs if none'
' was provided). Simple wildcard matching (using asterisks) is'
' supported.')
args = '[spec_ids]'
def handle(self, *args, **options):
specs = generator_registry.get_ids()
if args:
patterns = self.compile_patterns(args)
specs = (id for id in specs if any(p.match(id) for p in patterns))
for spec_id in specs:
self.stdout.write('Validating spec: %s\n' % spec_id)
for source in source_group_registry.get(spec_id):
for source_file in source.files():
if source_file:
spec = generator_registry.get(spec_id, source_file=source_file) # TODO: HINTS! (Probably based on source, so this will need to be moved into loop below.)
self.stdout.write(' %s\n' % source_file)
try:
# TODO: Allow other validation actions through command option
GeneratedImageCacheFile(spec).validate()
except Exception, err:
# TODO: How should we handle failures? Don't want to error, but should call it out more than this.
self.stdout.write(' FAILED: %s\n' % err)
def compile_patterns(self, spec_ids):
return [re.compile('%s$' % '.*'.join(re.escape(part) for part in id.split('*'))) for id in spec_ids]