add URL.id2url decorator
This commit is contained in:
parent
bf8675c88b
commit
9724dcf461
1 changed files with 27 additions and 7 deletions
|
|
@ -85,24 +85,44 @@ class URL(object):
|
||||||
>>> url = URL('http://exawple.org/(?P<pagename>).html')
|
>>> url = URL('http://exawple.org/(?P<pagename>).html')
|
||||||
>>> url.stay_or_go(pagename='index')
|
>>> url.stay_or_go(pagename='index')
|
||||||
"""
|
"""
|
||||||
|
return self.browser.location(self.build(**kwargs))
|
||||||
|
|
||||||
|
def build(self, **kwargs):
|
||||||
patterns = []
|
patterns = []
|
||||||
for url in self.urls:
|
for url in self.urls:
|
||||||
patterns += normalize(url)
|
patterns += normalize(url)
|
||||||
|
|
||||||
for pattern, args in patterns:
|
for pattern, args in patterns:
|
||||||
url = pattern % kwargs
|
url = pattern % kwargs
|
||||||
return self.browser.location(url)
|
return url
|
||||||
|
|
||||||
|
def match(self, url):
|
||||||
|
for regex in self.urls:
|
||||||
|
if regex.startswith('/'):
|
||||||
|
regex = self.browser.BASEURL + regex
|
||||||
|
m = re.match(regex, url)
|
||||||
|
if m:
|
||||||
|
return m
|
||||||
|
|
||||||
def handle(self, response):
|
def handle(self, response):
|
||||||
"""
|
"""
|
||||||
Handle a HTTP response to get an instance of the klass if it matches.
|
Handle a HTTP response to get an instance of the klass if it matches.
|
||||||
"""
|
"""
|
||||||
for regex in self.urls:
|
m = self.match(response.url)
|
||||||
if regex.startswith('/'):
|
if m:
|
||||||
regex = self.browser.BASEURL + regex
|
return self.klass(self.browser, response, m.groupdict())
|
||||||
m = re.match(regex, response.url)
|
|
||||||
if m:
|
def id2url(self, func):
|
||||||
return self.klass(self.browser, response, m.groupdict())
|
def inner(browser, _id, *args, **kwargs):
|
||||||
|
if re.match('^https?://.*', _id):
|
||||||
|
_id = self.match(_id)
|
||||||
|
if _id is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
url = self.build(id=_id)
|
||||||
|
|
||||||
|
return func(browser, url, *args, **kwargs)
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
class _PagesBrowserMeta(type):
|
class _PagesBrowserMeta(type):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue