Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow passing positional argument to ShowUrl command to find matching…

… Rule
  • Loading branch information...
commit b9afd9bb55b2b18ff2c25d079b25b7306e66f9ac 1 parent e1367df
@techniq techniq authored
Showing with 57 additions and 7 deletions.
  1. +3 −0  examples/manage.py
  2. +54 −7 flask_script/commands.py
View
3  examples/manage.py
@@ -2,6 +2,7 @@
from flask import Flask, current_app
from flask.ext.script import Manager, prompt_choices, Server
+from flask.ext.script.commands import ShowUrls, Clean
def create_app(config=None):
@@ -74,6 +75,8 @@ def optional(name, url):
required=False)
manager.add_command("runservernoreload", Server(use_reloader=False))
+manager.add_command("urls", ShowUrls())
+manager.add_command("clean", Clean())
if __name__ == "__main__":
manager.run()
View
61 flask_script/commands.py
@@ -5,6 +5,7 @@
import os
import code
import warnings
+import string
import argparse
@@ -376,13 +377,17 @@ def run(self):
class ShowUrls(Command):
"""
- Displays all of the url matching routes for the project.
+ Displays all of the url matching routes for the project
"""
def __init__(self, order='rule'):
self.order = order
def get_options(self):
options = super(ShowUrls, self).get_options()
+ options += Option('url',
+ nargs='?',
+ help='Url to test (ex. /static/image.png)',
+ ),
options += Option('--order',
dest='order',
default=self.order,
@@ -391,12 +396,54 @@ def get_options(self):
return options
- def run(self, order):
+ def run(self, url, order):
from flask import current_app
+ from werkzeug.exceptions import NotFound, MethodNotAllowed
- print "%-30s" % 'Rule', 'Endpoint'
- print '-' * 80
+ rows = []
+ column_length = 0
+ column_headers = ('Rule', 'Endpoint', 'Arguments')
- rules = sorted(current_app.url_map.iter_rules(), key=lambda rule: getattr(rule, order))
- for rule in rules:
- print "%-30s" % rule, rule.endpoint
+ if url:
+ try:
+ rule, arguments = current_app.url_map \
+ .bind('localhost') \
+ .match(url, return_rule=True)
+ rows.append((rule.rule, rule.endpoint, arguments))
+ column_length = 3
+ except (NotFound, MethodNotAllowed), e:
+ rows.append(("<%s>" % e, None, None))
+ column_length = 1
+ else:
+ rules = sorted(current_app.url_map.iter_rules(), key=lambda rule: getattr(rule, order))
+ for rule in rules:
+ rows.append((rule.rule, rule.endpoint, None))
+ column_length = 2
+
+ str_template = ''
+ table_width = 0
+
+ if column_length >= 1:
+ max_rule_length = max(len(r[0]) for r in rows)
+ max_rule_length = max_rule_length if max_rule_length > 4 else 4
+ str_template += '%-' + str(max_rule_length) + 's'
+ table_width += max_rule_length
+
+ if column_length >= 2:
+ max_endpoint_length = max(len(str(r[1])) for r in rows)
+ # max_endpoint_length = max(rows, key=len)
+ max_endpoint_length = max_endpoint_length if max_endpoint_length > 8 else 8
+ str_template += ' %-' + str(max_endpoint_length) + 's'
+ table_width += 2 + max_endpoint_length
+
+ if column_length >= 3:
+ max_arguments_length = max(len(str(r[2])) for r in rows)
+ max_arguments_length = max_arguments_length if max_arguments_length > 9 else 9
+ str_template += ' %-' + str(max_arguments_length) + 's'
+ table_width += 2 + max_arguments_length
+
+ print str_template % (column_headers[:column_length])
+ print '-' * table_width
+
+ for row in rows:
+ print str_template % row[:column_length]
Please sign in to comment.
Something went wrong with that request. Please try again.