From 13f5684ed91844da3065e249ce8921a9ea2ef782 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Thu, 28 Mar 2019 12:19:45 +0100 Subject: [PATCH] config: Move time_buffer to util --- weboob/tools/config/extra.py | 32 +++-------------------------- weboob/tools/config/sqliteconfig.py | 3 +-- weboob/tools/config/util.py | 30 ++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/weboob/tools/config/extra.py b/weboob/tools/config/extra.py index 62960d3cb0..a647cf3dac 100644 --- a/weboob/tools/config/extra.py +++ b/weboob/tools/config/extra.py @@ -20,10 +20,11 @@ import multiprocessing import os -from datetime import datetime +from .util import time_buffer -__all__ = ['AutoCleanConfig', 'ForkingConfig', 'TimeBufferConfig', 'time_buffer'] + +__all__ = ['AutoCleanConfig', 'ForkingConfig', 'TimeBufferConfig'] """ @@ -95,33 +96,6 @@ def __setstate__(self, d): setattr(self, k, v) -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. diff --git a/weboob/tools/config/sqliteconfig.py b/weboob/tools/config/sqliteconfig.py index 1fe21f4578..fa35e45011 100644 --- a/weboob/tools/config/sqliteconfig.py +++ b/weboob/tools/config/sqliteconfig.py @@ -28,9 +28,8 @@ from weboob.tools.compat import unicode -from .extra import time_buffer from .iconfig import ConfigError, IConfig -from .util import replace +from .util import replace, time_buffer from .yamlconfig import WeboobDumper try: diff --git a/weboob/tools/config/util.py b/weboob/tools/config/util.py index 0ee7acfbf7..b020ae8b76 100644 --- a/weboob/tools/config/util.py +++ b/weboob/tools/config/util.py @@ -19,8 +19,9 @@ import os +from datetime import datetime -__all__ = ['replace'] +__all__ = ['replace', 'time_buffer'] try: @@ -33,3 +34,30 @@ def replace(src, dst, *args, **kwargs): except OSError: pass return os.rename(src, dst, *args, **kwargs) + + +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 -- GitLab