Register cacheables as generators instead of items

This commit is contained in:
Eric Eldredge 2013-01-24 00:04:43 -05:00
parent a8855d4c27
commit eb9089e0c8
3 changed files with 23 additions and 31 deletions

View file

@ -19,17 +19,16 @@ class Command(BaseCommand):
for generator_id in generators:
self.stdout.write('Validating generator: %s\n' % generator_id)
for cacheables in cacheable_registry.get(generator_id):
for cacheable in cacheables.files():
if cacheable:
generator = generator_registry.get(generator_id, cacheable=cacheable) # TODO: HINTS! (Probably based on cacheable, so this will need to be moved into loop below.)
self.stdout.write(' %s\n' % cacheable)
try:
# TODO: Allow other validation actions through command option
GeneratedImageCacheFile(generator).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)
for kwargs in cacheable_registry.get(generator_id):
if kwargs:
generator = generator_registry.get(generator_id, **kwargs) # TODO: HINTS! (Probably based on cacheable, so this will need to be moved into loop below.)
self.stdout.write(' %s\n' % generator)
try:
# TODO: Allow other validation actions through command option
GeneratedImageCacheFile(generator).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, generator_ids):
return [re.compile('%s$' % '.*'.join(re.escape(part) for part in id.split('*'))) for id in generator_ids]

View file

@ -70,25 +70,25 @@ class CacheableRegistry(object):
Associates cacheables with a generator id
"""
for cacheable in cacheables:
if cacheable not in self._cacheables:
self._cacheables[cacheable] = set()
self._cacheables[cacheable].add(generator_id)
if cacheables not in self._cacheables:
self._cacheables[cacheables] = set()
self._cacheables[cacheables].add(generator_id)
def unregister(self, generator_id, cacheables):
"""
Disassociates cacheables with a generator id
"""
for cacheable in cacheables:
try:
self._cacheables[cacheable].remove(generator_id)
except KeyError:
continue
try:
self._cacheables[cacheables].remove(generator_id)
except KeyError:
pass
def get(self, generator_id):
return [cacheable for cacheable in self._cacheables
if generator_id in self._cacheables[cacheable]]
for k, v in self._cacheables.items():
if generator_id in v:
for cacheable in k():
yield cacheable
def before_access_receiver(self, sender, generator, cacheable, **kwargs):
generator.image_cache_strategy.invoke_callback('before_access', cacheable)
@ -130,8 +130,6 @@ class Register(object):
# iterable that returns kwargs or callable that returns iterable of kwargs
def cacheables(self, generator_id, cacheables):
if callable(cacheables):
cacheables = cacheables()
cacheable_registry.register(generator_id, cacheables)
@ -144,8 +142,6 @@ class Unregister(object):
generator_registry.unregister(id, generator)
def cacheables(self, generator_id, cacheables):
if callable(cacheables):
cacheables = cacheables()
cacheable_registry.unregister(generator_id, cacheables)

View file

@ -117,11 +117,8 @@ class ImageFieldSourceGroup(object):
self.image_field = image_field
signal_router.add(self)
def files(self):
for instance in self.model_class.objects.all():
yield getattr(instance, self.image_field)
def __call__(self):
return self.files()
for instance in self.model_class.objects.all():
yield {'source': getattr(instance, self.image_field)}
signal_router = ModelSignalRouter()