From f591dba67831445f47fecd6145a95d5ff60dc7e7 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Thu, 9 Apr 2015 10:15:57 +0200 Subject: [PATCH] Browser: callback in the same place, async or not --- weboob/browser/browsers.py | 6 ++---- weboob/browser/sessions.py | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/weboob/browser/browsers.py b/weboob/browser/browsers.py index fe81e3a3..36634b5d 100644 --- a/weboob/browser/browsers.py +++ b/weboob/browser/browsers.py @@ -346,10 +346,8 @@ class Browser(object): verify=verify, cert=cert, proxies=proxies, - background_callback=async and inner_callback) - if not async: - inner_callback(self, response) - + callback=inner_callback, + async=async) return response def async_open(self, url, **kwargs): diff --git a/weboob/browser/sessions.py b/weboob/browser/sessions.py index 9e72adba..f5188831 100644 --- a/weboob/browser/sessions.py +++ b/weboob/browser/sessions.py @@ -131,25 +131,26 @@ class FuturesSession(WeboobSession): Used by :meth:`request` and thus all of the higher level methods - If background_callback param is defined, request is processed in a - thread, calling background_callback and returning it's result when - request has been processed. If background_callback is not defined, - request is processed as usual, in a blocking way. + If the `async` param is True, the request is processed in a + thread. Otherwise, the request is processed as usual, in a blocking way. + + In all cases, it will call the `callback` parameter and return its + result when the request has been processed. """ sup = super(FuturesSession, self).send - background_callback = kwargs.pop('background_callback', None) - if background_callback: + callback = kwargs.pop('callback', lambda future, response: response) + async = kwargs.pop('async', False) + def func(*args, **kwargs): + resp = sup(*args, **kwargs) + return callback(self, resp) + + if async: if not self.executor: raise ImportError('Please install python-concurrent.futures') - - def func(*args, **kwargs): - resp = sup(*args, **kwargs) - return background_callback(self, resp) - return self.executor.submit(func, *args, **kwargs) - return sup(*args, **kwargs) + return func(*args, **kwargs) def close(self): super(FuturesSession, self).close()