Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convert output of django-extensions' `show_urls` to a python list of "simple" URLs
import re
from import call_command
from test_plus.test import TestCase
class SmokeTestAllViews(BaseUserTestCase):
def test_all_view_respones(self):
Extract all URLs and do a quick sanity check for accessibility.
Are all responding with 200 or 302 or 404 where expected? Are expected authorizations enforced?
Clean up the output of django-extensions' `show_urls` command. A bit tricky because it's ANSI-encoded
for terminal display, so clean that up and filter out the /admin URLs.
On each remaining line, strip out everything after first tab char.
# Write output to a file, then read it back in
tempfile = '/tmp/show_urls.txt'
with open(tempfile, 'w+') as f:
call_command('show_urls', stdout=f)
with open(tempfile) as f:
content = f.readlines()
# Strip out the ansi chars
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
url_list = []
[url_list.append(ansi_escape.sub('', line)) for line in content] # Strip ANSI chars
url_list = [line.split('\t')[0] for line in url_list] # Output is tab-sep; keep data before first tab per line
url_list = list(filter(lambda x: not x.startswith("/admin"), url_list)) # Filter out all the /admin URLs
for el in url_list:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment