From 4d8030b8dd5f1ef7a11515d5cd008e1056486ecf Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Thu, 21 Apr 2011 22:18:17 +0200 Subject: [PATCH] Add check_url decorator, remove check_domain check_domain was not used anywhere and was bugged (the domain argument was ignored in favor of self.DOMAIN). The check_url decorator checks if the URL can be handled, if not it will return None. The idea is to avoid making unnecessary requests. --- weboob/tools/browser/decorators.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/weboob/tools/browser/decorators.py b/weboob/tools/browser/decorators.py index bbaa731c..9461026a 100644 --- a/weboob/tools/browser/decorators.py +++ b/weboob/tools/browser/decorators.py @@ -18,18 +18,27 @@ # along with weboob. If not, see . -__all__ = ['check_domain', 'id2url'] +__all__ = ['check_url', 'id2url'] from urlparse import urlsplit +import re -def check_domain(domain): - def wrapper(func): - def inner(self, *args, **kwargs): - if self.DOMAIN not in args[0]: - return None - return func(self, *args, **kwargs) - return inner - return wrapper +class check_url(object): + """ + Checks if the first argument matches the given regular expression (given as str, + without the ^$ delimiters which are automatically added). + If not, this decorator will return None instead of calling the function. + """ + + def __init__(self, regexp): + self.regexp = re.compile('^%s$' % regexp) + + def __call__(self, func): + def wrapper(funcself, *args, **kwargs): + if self.regexp.match(args[0]): + return func(funcself, *args, **kwargs) + return None + return wrapper def id2url(id2url):