django-imagekit/imagekit/cachefiles/strategies.py

57 lines
1.4 KiB
Python
Raw Normal View History

2013-01-29 06:48:06 +00:00
from django.utils.functional import LazyObject
from ..utils import get_singleton
2012-10-17 01:46:23 +00:00
class JustInTime(object):
"""
A strategy that ensures the file exists right before it's needed.
"""
2012-10-17 02:30:36 +00:00
def before_access(self, file):
file.generate()
class Optimistic(object):
"""
A strategy that acts immediately when the source file changes and assumes
that the cache files will not be removed (i.e. it doesn't ensure the
cache file exists when it's accessed).
"""
2013-01-29 02:07:35 +00:00
def on_source_created(self, file):
file.generate()
2013-01-29 02:07:35 +00:00
def on_source_changed(self, file):
file.generate()
class DictStrategy(object):
def __init__(self, callbacks):
for k, v in callbacks.items():
setattr(self, k, v)
2013-01-29 06:48:06 +00:00
class StrategyWrapper(LazyObject):
def __init__(self, strategy):
if isinstance(strategy, basestring):
strategy = get_singleton(strategy, 'cache file strategy')
elif isinstance(strategy, dict):
strategy = DictStrategy(strategy)
elif callable(strategy):
strategy = strategy()
self._wrapped = strategy
2013-01-29 07:27:03 +00:00
def __getstate__(self):
return {'_wrapped': self._wrapped}
def __setstate__(self, state):
self._wrapped = state['_wrapped']
2012-10-17 02:30:36 +00:00
def __unicode__(self):
return unicode(self._wrapped)
def __str__(self):
return str(self._wrapped)