Created
August 8, 2013 17:44
-
-
Save valtron/6186867 to your computer and use it in GitHub Desktop.
Lists full url regexes that are matched by a django urlconf in a standardized form.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from project import urls | |
def main(): | |
for x in sorted(map(combine, list_urls(urls.urlpatterns))): | |
print x | |
def list_urls(urllist): | |
for entry in urllist: | |
if hasattr(entry, 'url_patterns'): | |
for x in list_urls(entry.url_patterns): | |
yield [entry] + x | |
else: | |
yield [entry] | |
def combine(lst): | |
full_regex = '' | |
iri = [] | |
for x in lst: | |
pat = x.regex.pattern | |
nomen = _get_name(x) | |
if nomen: | |
iri.append(nomen) | |
if full_regex and pat.startswith('^'): | |
pat = pat[1:] | |
full_regex += pat | |
full_regex = _standardize_url_regex(full_regex) | |
return full_regex, ':'.join(iri) | |
def _standardize_url_regex(regex): | |
assert regex.startswith('^') | |
assert regex.endswith('$') | |
regex = regex[1:-1] | |
assert '$' not in regex | |
assert '^' not in regex | |
if regex: | |
assert not regex.endswith('/') | |
assert not regex.startswith('/') | |
regex = '/' + regex | |
return regex | |
def _get_name(regexurl): | |
if hasattr(regexurl, 'name'): | |
return regexurl.name | |
return regexurl.namespace | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment