Skip to content

Commit b9afd9b

Browse files
committed
Allow passing positional argument to ShowUrl command to find matching Rule
1 parent e1367df commit b9afd9b

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

examples/manage.py

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from flask import Flask, current_app
44
from flask.ext.script import Manager, prompt_choices, Server
5+
from flask.ext.script.commands import ShowUrls, Clean
56

67

78
def create_app(config=None):
@@ -74,6 +75,8 @@ def optional(name, url):
7475
required=False)
7576

7677
manager.add_command("runservernoreload", Server(use_reloader=False))
78+
manager.add_command("urls", ShowUrls())
79+
manager.add_command("clean", Clean())
7780

7881
if __name__ == "__main__":
7982
manager.run()

flask_script/commands.py

+54-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import code
77
import warnings
8+
import string
89

910
import argparse
1011

@@ -376,13 +377,17 @@ def run(self):
376377

377378
class ShowUrls(Command):
378379
"""
379-
Displays all of the url matching routes for the project.
380+
Displays all of the url matching routes for the project
380381
"""
381382
def __init__(self, order='rule'):
382383
self.order = order
383384

384385
def get_options(self):
385386
options = super(ShowUrls, self).get_options()
387+
options += Option('url',
388+
nargs='?',
389+
help='Url to test (ex. /static/image.png)',
390+
),
386391
options += Option('--order',
387392
dest='order',
388393
default=self.order,
@@ -391,12 +396,54 @@ def get_options(self):
391396

392397
return options
393398

394-
def run(self, order):
399+
def run(self, url, order):
395400
from flask import current_app
401+
from werkzeug.exceptions import NotFound, MethodNotAllowed
396402

397-
print "%-30s" % 'Rule', 'Endpoint'
398-
print '-' * 80
403+
rows = []
404+
column_length = 0
405+
column_headers = ('Rule', 'Endpoint', 'Arguments')
399406

400-
rules = sorted(current_app.url_map.iter_rules(), key=lambda rule: getattr(rule, order))
401-
for rule in rules:
402-
print "%-30s" % rule, rule.endpoint
407+
if url:
408+
try:
409+
rule, arguments = current_app.url_map \
410+
.bind('localhost') \
411+
.match(url, return_rule=True)
412+
rows.append((rule.rule, rule.endpoint, arguments))
413+
column_length = 3
414+
except (NotFound, MethodNotAllowed), e:
415+
rows.append(("<%s>" % e, None, None))
416+
column_length = 1
417+
else:
418+
rules = sorted(current_app.url_map.iter_rules(), key=lambda rule: getattr(rule, order))
419+
for rule in rules:
420+
rows.append((rule.rule, rule.endpoint, None))
421+
column_length = 2
422+
423+
str_template = ''
424+
table_width = 0
425+
426+
if column_length >= 1:
427+
max_rule_length = max(len(r[0]) for r in rows)
428+
max_rule_length = max_rule_length if max_rule_length > 4 else 4
429+
str_template += '%-' + str(max_rule_length) + 's'
430+
table_width += max_rule_length
431+
432+
if column_length >= 2:
433+
max_endpoint_length = max(len(str(r[1])) for r in rows)
434+
# max_endpoint_length = max(rows, key=len)
435+
max_endpoint_length = max_endpoint_length if max_endpoint_length > 8 else 8
436+
str_template += ' %-' + str(max_endpoint_length) + 's'
437+
table_width += 2 + max_endpoint_length
438+
439+
if column_length >= 3:
440+
max_arguments_length = max(len(str(r[2])) for r in rows)
441+
max_arguments_length = max_arguments_length if max_arguments_length > 9 else 9
442+
str_template += ' %-' + str(max_arguments_length) + 's'
443+
table_width += 2 + max_arguments_length
444+
445+
print str_template % (column_headers[:column_length])
446+
print '-' * table_width
447+
448+
for row in rows:
449+
print str_template % row[:column_length]

0 commit comments

Comments
 (0)