Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format proutes output and include module instead of repr of view #1453

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions pyramid/scripts/proutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

from pyramid.paster import bootstrap
from pyramid.scripts.common import parse_vars
from pyramid.config.views import MultiView


PAD = 3


def main(argv=sys.argv, quiet=False):
command = PRoutesCommand(argv, quiet)
return command.run()


class PRoutesCommand(object):
description = """\
Print all URL dispatch routes used by a Pyramid application in the
Expand Down Expand Up @@ -43,7 +49,7 @@ def _get_mapper(self, registry):
def out(self, msg): # pragma: no cover
if not self.quiet:
print(msg)

def run(self, quiet=False):
if not self.args:
self.out('requires a config file argument')
Expand All @@ -59,27 +65,73 @@ def run(self, quiet=False):
registry = env['registry']
mapper = self._get_mapper(registry)
if mapper is not None:
mapped_routes = [('Name', 'Pattern', 'View')]

max_name = len('Name')
max_pattern = len('Pattern')
max_view = len('View')

routes = mapper.get_routes()
fmt = '%-15s %-30s %-25s'
if not routes:
return 0
self.out(fmt % ('Name', 'Pattern', 'View'))
self.out(
fmt % ('-'*len('Name'), '-'*len('Pattern'), '-'*len('View')))

mapped_routes.append((
'-' * max_name,
'-' * max_pattern,
'-' * max_view,
))

for route in routes:
pattern = route.pattern
if not pattern.startswith('/'):
pattern = '/' + pattern
request_iface = registry.queryUtility(IRouteRequest,
name=route.name)
view_callable = None

if (request_iface is None) or (route.factory is not None):
self.out(fmt % (route.name, pattern, '<unknown>'))
view_callable = '<unknown>'
else:
view_callable = registry.adapters.lookup(
(IViewClassifier, request_iface, Interface),
IView, name='', default=None)
self.out(fmt % (route.name, pattern, view_callable))

if view_callable is not None:
if isinstance(view_callable, MultiView):
view_callables = [
x[1] for x in view_callable.views
]
else:
view_callables = [view_callable]

for view_func in view_callables:
view_callable = '%s.%s' % (
view_func.__module__,
view_func.__name__,
)
else:
view_callable = str(None)

if len(route.name) > max_name:
max_name = len(route.name)

if len(pattern) > max_pattern:
max_pattern = len(pattern)

if len(view_callable) > max_view:
max_view = len(view_callable)

mapped_routes.append((route.name, pattern, view_callable))

fmt = '%-{0}s %-{1}s %-{2}s'.format(
max_name + PAD,
max_pattern + PAD,
max_view + PAD,
)

for route_data in mapped_routes:
self.out(fmt % route_data)

return 0

if __name__ == '__main__': # pragma: no cover
Expand Down
10 changes: 6 additions & 4 deletions pyramid/tests/test_scripts/test_proutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ class IMyRoute(Interface):
self.assertEqual(result, 0)
self.assertEqual(len(L), 3)
compare_to = L[-1].split()[:3]
self.assertEqual(compare_to, ['a', '/a', '<function'])

self.assertEqual(
compare_to,
['a', '/a', 'pyramid.tests.test_scripts.test_proutes.view']
)

def test_single_route_one_view_registered_with_factory(self):
from zope.interface import Interface
from pyramid.registry import Registry
Expand Down Expand Up @@ -161,7 +164,7 @@ def test__get_mapper(self):
registry = Registry()
result = command._get_mapper(registry)
self.assertEqual(result.__class__, RoutesMapper)

class Test_main(unittest.TestCase):
def _callFUT(self, argv):
from pyramid.scripts.proutes import main
Expand All @@ -170,4 +173,3 @@ def _callFUT(self, argv):
def test_it(self):
result = self._callFUT(['proutes'])
self.assertEqual(result, 2)