def match_url_regex(rules, url, callback):
"""Given rules and a callback, find the rule that matches the url.
Rules look like::
(
{
'schemes': ['https', 'ssh'],
'netlocs': ['hg.mozilla.org'],
'path_regexes': [
"^(?P<path>/mozilla-(central|unified))(/|$)",
]
},
...
)
Args:
rules (list): a list of dictionaries specifying lists of ``schemes``,
``netlocs``, and ``path_regexes``.
url (str): the url to test
callback (function): a callback that takes an ``re.MatchObject``.
If it returns None, continue searching. Otherwise, return the
value from the callback.
Returns:
value: the value from the callback, or None if no match.
"""
parts = urlparse(url)
path = unquote(parts.path)
for rule in rules:
if parts.scheme not in rule['schemes']:
continue
if parts.netloc not in rule['netlocs']:
continue
for regex in rule['path_regexes']:
m = re.search(regex, path)
if m is None:
continue
result = callback(m)
if result is not None:
return result
评论列表
文章目录