django-imagekit/imagekit/imagecache/strategies.py
Matthew Tretter 7447d147d4 Wire up source events to specs (and image cache strategies)
Also change signal names to past tense to match convention.
2012-10-13 01:16:05 -04:00

57 lines
1.4 KiB
Python

from .actions import validate_now, clear_now
from ..utils import get_singleton
class Pessimistic(object):
"""
A caching strategy that validates the file every time it's accessed.
"""
def on_accessed(self, file):
validate_now(file)
def on_source_deleted(self, file):
clear_now(file)
def on_source_changed(self, file):
validate_now(file)
class Optimistic(object):
"""
A caching strategy that validates when the source file changes and assumes
that the cached file will persist.
"""
def on_source_created(self, file):
validate_now(file)
def on_source_deleted(self, file):
clear_now(file)
def on_source_changed(self, file):
validate_now(file)
class DictStrategy(object):
def __init__(self, callbacks):
for k, v in callbacks.items():
setattr(self, k, v)
class StrategyWrapper(object):
def __init__(self, strategy):
if isinstance(strategy, basestring):
strategy = get_singleton(strategy, 'image cache strategy')
elif isinstance(strategy, dict):
strategy = DictStrategy(strategy)
elif callable(strategy):
strategy = strategy()
self._wrapped = strategy
def invoke_callback(self, name, *args, **kwargs):
func = getattr(self._wrapped, name, None)
if func:
func(*args, **kwargs)