From 1b0ed3bd4db80e4c7b2cf0da11fa582adc3ea978 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Thu, 7 Mar 2019 17:37:04 +0100 Subject: [PATCH] config: Move time buffer logic out of the class So it can be used easily by other subclasses --- weboob/tools/config/extra.py | 52 +++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/weboob/tools/config/extra.py b/weboob/tools/config/extra.py index 82978a9926..72a1597d20 100644 --- a/weboob/tools/config/extra.py +++ b/weboob/tools/config/extra.py @@ -3,7 +3,7 @@ from datetime import datetime -__all__ = ['AutoCleanConfig', 'ForkingConfig', 'TimeBufferConfig'] +__all__ = ['AutoCleanConfig', 'ForkingConfig', 'TimeBufferConfig', 'time_buffer'] """ @@ -62,33 +62,49 @@ def __exit__(self, t, v, tb): super(ForkingConfig, self).__exit__(t, v, tb) +def time_buffer(since_seconds=None, last_run=True, logger=False): + def decorator_time_buffer(func): + def wrapper_time_buffer(*args, **kwargs): + since_seconds = kwargs.pop('since_seconds', None) + if since_seconds is None: + since_seconds = decorator_time_buffer.since_seconds + if logger: + logger.debug('Time buffer for %s of %s. Last run %s.' + % (repr(func), since_seconds, decorator_time_buffer.last_run)) + if since_seconds and decorator_time_buffer.last_run: + if (datetime.now() - decorator_time_buffer.last_run).seconds < since_seconds: + if logger: + logger.debug('Too soon to run %s, ignore.' % repr(func)) + return + if logger: + logger.debug('Run %s and record' % repr(func)) + res = func(*args, **kwargs) + decorator_time_buffer.last_run = datetime.now() + return res + + decorator_time_buffer.since_seconds = since_seconds + decorator_time_buffer.last_run = datetime.now() if last_run is True else last_run + + return wrapper_time_buffer + return decorator_time_buffer + + class TimeBufferConfig(object): """ Really saves only every saved_since_seconds seconds. It is possible to force save (e.g. at exit) with force_save(). """ - last_save = None saved_since_seconds = None - def __init__(self, path, saved_since_seconds=None): - super(TimeBufferConfig, self).__init__(path) - self.saved_since_seconds = saved_since_seconds - - def load(self, default={}): - super(TimeBufferConfig, self).load(default) - self.last_save = datetime.now() + def __init__(self, path, saved_since_seconds=None, *args, **kwargs): + super(TimeBufferConfig, self).__init__(path, *args, **kwargs) + if saved_since_seconds: + self.saved_since_seconds = saved_since_seconds - def save(self, saved_since_seconds=None): - if saved_since_seconds is None: - saved_since_seconds = self.saved_since_seconds - if saved_since_seconds and self.last_save: - if (datetime.now() - self.last_save).seconds < saved_since_seconds: - return - super(TimeBufferConfig, self).save() - self.last_save = datetime.now() + self.save = time_buffer(since_seconds=self.saved_since_seconds)(self.save) def force_save(self): - self.save(False) + self.save(since_seconds=False) def __exit__(self, t, v, tb): self.force_save() -- GitLab