diff options
| author | John Anderson <sontek@gmail.com> | 2014-11-11 08:17:02 -0800 |
|---|---|---|
| committer | John Anderson <sontek@gmail.com> | 2014-11-11 08:17:02 -0800 |
| commit | e51dd09eb1ba4c873f7dec763a1e51c5779801b7 (patch) | |
| tree | 6cb9b044c650f60e6cdd87abe9831033070cbf17 | |
| parent | 4d19a6f383c802f9620b7d2fc239e5c6ad6c52f9 (diff) | |
| download | pyramid-e51dd09eb1ba4c873f7dec763a1e51c5779801b7.tar.gz pyramid-e51dd09eb1ba4c873f7dec763a1e51c5779801b7.tar.bz2 pyramid-e51dd09eb1ba4c873f7dec763a1e51c5779801b7.zip | |
Format proutes output and include module instead of repr of view
| -rw-r--r-- | pyramid/scripts/proutes.py | 66 | ||||
| -rw-r--r-- | pyramid/tests/test_scripts/test_proutes.py | 10 |
2 files changed, 65 insertions, 11 deletions
diff --git a/pyramid/scripts/proutes.py b/pyramid/scripts/proutes.py index 5784026bb..792030a74 100644 --- a/pyramid/scripts/proutes.py +++ b/pyramid/scripts/proutes.py @@ -4,11 +4,17 @@ import textwrap 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 @@ -43,7 +49,7 @@ class PRoutesCommand(object): 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') @@ -59,13 +65,22 @@ class PRoutesCommand(object): 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('/'): @@ -73,13 +88,50 @@ class PRoutesCommand(object): 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 diff --git a/pyramid/tests/test_scripts/test_proutes.py b/pyramid/tests/test_scripts/test_proutes.py index 25a3cd2e3..45ab57d3a 100644 --- a/pyramid/tests/test_scripts/test_proutes.py +++ b/pyramid/tests/test_scripts/test_proutes.py @@ -123,8 +123,11 @@ class TestPRoutesCommand(unittest.TestCase): 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 @@ -161,7 +164,7 @@ class TestPRoutesCommand(unittest.TestCase): 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 @@ -170,4 +173,3 @@ class Test_main(unittest.TestCase): def test_it(self): result = self._callFUT(['proutes']) self.assertEqual(result, 2) - |
