I use yapps to generate a parser for a LaTex-ish language (for example to translate stuff like \begin{itemize}
to the corresponding <ul>
-Tags) within pyramid. One command (i.e. \ref{SOMEID}
) should construct a route via a call of route_url
(or route_path
) and pass the id to it. Since this call happens deep in the code that was generated by yapps and the grammar that I defined, I don't see any possibility to pass a request object to it.
Is there some sort of global request object? Or, since I foresee that I shouldn't use it, is there a possibility to construct a route (that depends on a parameter) without a request object?
route_url
requires both a request and a registry (request.registry
). It generates urls relative to the request, and it accesses the list of all routes and other settings from the registry. Thus, you must generate a dummy request with parameters you care about. For example:
from pyramid.request import Request
request = Request.blank('/', base_url='https://example.com/prefix')
request.registry = config.registry
Now you can store this request anywhere, it's good to go representing everything about your site: the hostname/port (example.com:443
), the prefix your app is mounted at (/prefix
), the uri scheme (https
).
If you need to get this deep down into your code you may have to make it a global or attach it to some context/registry that you have available, but what I've shown is how to make the request that you require.