2013-08-19 18:27:50 +00:00
|
|
|
import six
|
|
|
|
|
|
2013-01-29 06:48:06 +00:00
|
|
|
from django.utils.functional import LazyObject
|
2013-08-19 18:27:50 +00:00
|
|
|
from ..lib import force_text
|
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
|
|
|
|
2014-09-23 22:35:38 +00:00
|
|
|
def should_verify_existence(self, file):
|
|
|
|
|
return False
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
2014-01-21 16:46:19 +00:00
|
|
|
def load_strategy(strategy):
|
|
|
|
|
if isinstance(strategy, six.string_types):
|
|
|
|
|
strategy = get_singleton(strategy, 'cache file strategy')
|
|
|
|
|
elif isinstance(strategy, dict):
|
|
|
|
|
strategy = DictStrategy(strategy)
|
|
|
|
|
elif callable(strategy):
|
|
|
|
|
strategy = strategy()
|
|
|
|
|
return strategy
|