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.
This commit is contained in:
parent
1a4fd0d320
commit
4d8030b8dd
1 changed files with 18 additions and 9 deletions
|
|
@ -18,18 +18,27 @@
|
||||||
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['check_domain', 'id2url']
|
__all__ = ['check_url', 'id2url']
|
||||||
|
|
||||||
from urlparse import urlsplit
|
from urlparse import urlsplit
|
||||||
|
import re
|
||||||
|
|
||||||
def check_domain(domain):
|
class check_url(object):
|
||||||
def wrapper(func):
|
"""
|
||||||
def inner(self, *args, **kwargs):
|
Checks if the first argument matches the given regular expression (given as str,
|
||||||
if self.DOMAIN not in args[0]:
|
without the ^$ delimiters which are automatically added).
|
||||||
return None
|
If not, this decorator will return None instead of calling the function.
|
||||||
return func(self, *args, **kwargs)
|
"""
|
||||||
return inner
|
|
||||||
return wrapper
|
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):
|
def id2url(id2url):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue