2013-01-29 06:48:06 +00:00
|
|
|
from django.utils.functional import LazyObject
|
2012-10-04 02:23:11 +00:00
|
|
|
from ..utils import get_singleton
|
|
|
|
|
|
|
|
|
|
|
2012-10-17 01:46:23 +00:00
|
|
|
class JustInTime(object):
|
2012-10-04 02:23:11 +00:00
|
|
|
"""
|
2013-01-31 08:51:29 +00:00
|
|
|
A strategy that ensures the file exists right before it's needed.
|
2012-10-04 02:23:11 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2013-05-10 08:39:46 +00:00
|
|
|
def on_existence_required(self, file):
|
|
|
|
|
file.generate()
|
|
|
|
|
|
|
|
|
|
def on_content_required(self, file):
|
2013-01-31 09:20:21 +00:00
|
|
|
file.generate()
|
2012-10-04 02:23:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Optimistic(object):
|
|
|
|
|
"""
|
2013-01-31 08:51:29 +00:00
|
|
|
A strategy that acts immediately when the source file changes and assumes
|
2013-02-05 00:39:25 +00:00
|
|
|
that the cache files will not be removed (i.e. it doesn't ensure the
|
|
|
|
|
cache file exists when it's accessed).
|
2012-10-04 02:23:11 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2013-05-25 03:21:30 +00:00
|
|
|
def on_source_saved(self, file):
|
2013-01-31 09:20:21 +00:00
|
|
|
file.generate()
|
2012-10-04 02:23:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
2012-10-04 02:23:11 +00:00
|
|
|
def __init__(self, strategy):
|
|
|
|
|
if isinstance(strategy, basestring):
|
2013-02-05 00:39:25 +00:00
|
|
|
strategy = get_singleton(strategy, 'cache file strategy')
|
2012-10-04 02:23:11 +00:00
|
|
|
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)
|