Skip to content

Instantly share code, notes, and snippets.

@mei-li
Created September 28, 2018 11:11
Show Gist options
  • Save mei-li/bb712fb8d8287bc54f4969fbd58eb658 to your computer and use it in GitHub Desktop.
Save mei-li/bb712fb8d8287bc54f4969fbd58eb658 to your computer and use it in GitHub Desktop.
Getting all the routes and methods for a Pyramid route.
from pyramid.interfaces import IRoutesMapper
from pyramid.config.predicates import RequestMethodPredicate
mapper = request.registry.queryUtility(IRoutesMapper)
json_routes = []
for route in mapper.get_routes():
if 'json' not in route.name: # custom convention: Only routes with json in the name as interesting
continue
method = [','.join(pr.val) for pr in route.predicates if isinstance(pr, RequestMethodPredicate)][0]
json_routes.append((route.name, route.pattern, method))
for route in json_routes:
print(route)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment