Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid: How to get all app's routes within a view?

I want to see all the routes which my application has. Return them as a response like key=>value pair:

'route1' => '{foo:\w+}'
'route2' => '{baz:\w+\d+}'
... and so on

But I don't know how to get them within my view. For example, this is my view. I want it to return a map of routes. I do this:

@view_config(route_name='route1')
def someView(request):
    routes = request.registry.settings.getRoutes()  ## what should I print here to get a map of routes?
    r = ''
    for k,v in sorted(routes.items()):
        r += str(k) + "=>" + str(v) + "<br/>";
    return Response(r)

There is a RoutesConfiguratorMixin class with get_routes_mapper method. I tried to import the class and called its method but got an error that no registry was in the instance of it:

from pyramid.config.routes import RoutesConfiguratorMixin as Router

r = Router();
routes = r.get_routes_mapper();
## ... and the same code as above

Doesn't work.

like image 636
Green Avatar asked Dec 09 '22 17:12

Green


1 Answers

There are 2 ways, one is supported (public) and one is unsupported (private).

Option #1 is to use the introspector and is explained here.

Option #2 is to use the route mapper (which is not a public api), in the way that the pyramid debugtoolbar does in its routes panel.

like image 60
Michael Merickel Avatar answered Dec 11 '22 07:12

Michael Merickel