Created
May 15, 2015 14:16
-
-
Save mhemesath/9ae577b963662805e454 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 django.core.management import base | |
from django.core.cache import get_cache | |
from django.conf import settings | |
from django.core.management import call_command | |
import StringIO | |
import re | |
import urls | |
output = StringIO.StringIO() | |
URL_PATTERNS = [] | |
class Command(base.NoArgsCommand): | |
help = """Clears iqh's caches. | |
Namespaces are listed in settings.CACHES. | |
""" | |
def show_url(self, entry, prefix=''): | |
#print " " * depth, entry.regex.pattern | |
regex = prefix + entry.regex.pattern | |
if hasattr(entry, 'url_patterns'): | |
for entry in entry.url_patterns: | |
self.show_url(entry, regex) | |
else: | |
URL_PATTERNS.append(regex) | |
def handle_noargs(self, **options): | |
#[self.show_url(entry) for entry in urls.urlpatterns] | |
#for pattern in URL_PATTERNS: | |
# print pattern | |
#return | |
cases = [] | |
call_command('show_urls', 'unsorted', stdout=output) | |
for line in output.getvalue().splitlines(): | |
line = re.split(r'\s+', line)[0] | |
line = line.replace('\x1b[0m', '') | |
line = line.replace('\x1b[32;1m', '') | |
if re.search(r'[<>]', line) and 'api' not in line: | |
find = line.replace('/', '\/') | |
find = re.sub(r'<[\w\-\.~]+>', '([^/]+)', find).strip() | |
find = re.sub(r'/$', '/?', find) | |
replace = re.sub(r'[<>]', '', line).strip() | |
cases.append('\nmatch(uri_path, "^{0}$"), "{1}"'.format(find, replace)) | |
print ("case({0})".format(",".join(cases))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment