I am using routes in turbogears. I started with the recipe at
http://docs.turbogears.org/1.0/RoutesIntegration
That works for dispatch. Now I want to use "url_for". I tried using
a "tgdict" wrapper around the returned dictionary, which works like
this
def tgdict(**kwargs):
d = {"url_for": routes.url_for}
d.update(kwargs)
return d
for example,
@expose(template="proxy.templates.status")
def index(self):
server = self._current_server()
if server is None:
server_url = "http://www.dasregistry.org/das1/sources/"
sources = []
else:
server_url = server.primary_url
sources = server.sources
return tgdict(server_url=server_url,
server=server,
sources=sources)
That does not work. I get the following error (trimmed to the tail)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/kid-0.9.5-py2.5.egg/kid/parser.py",
line 219, in _coalesce
for ev, item in stream:
File "/Users/dalke/src/proxy/proxy/templates/status.py", line 103, in
_pull
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/Routes-1.6.2-py2.5.egg/routes/util.py",
line 122, in url_for
route = config.mapper._routenames.get(args[0])
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/Routes-1.6.2-py2.5.egg/routes/__init__.py",
line 15, in __getattr__
return getattr(self.__shared_state, name)
AttributeError: 'thread._local' object has no attribute 'mapper'
Error in code generated from template file
'/Users/dalke/src/proxy/proxy/templates/status.kid'
This is in the template code calling "url_for".
My guess is the template is being generated in a different thread than
where the routes dispatching was being done. The routes code looks
for the current mapper and there isn't one in this thread. I don't
know if that's the case or how to investigate this further.
If it helps, my routes code looks like this
das2_mapper = routes.Mapper()
connect = das2_mapper.connect
connect("sources", "das2", controller="sources")
connect("source", "das2/:source", controller="source")
...
def _make_routes_request():
base = request.base
d = _base_re.match(base).groupdict()
host = d["host"]
proto = d["protocol"]
path = request.path
con = routes.request_config()
con.mapper = das2_mapper
con.host = host
con.protocol = proto
con.redirect = redirect
con.mapper_dict = das2_mapper.match(path)
#con.create_regs([]) # XXX What does this do?
return con
...
@expose()
def default(self, *args, **kwargs):
con = _make_routes_request()
if con.mapper_dict:
method_name = con.mapper_dict.pop("controller")
method = getattr(self, method_name, None)
if method is None:
raise NotImplementedError("not supposed to fail")
con.mapper_dict.pop("action") # ignored
return method(kwargs = kwargs, **con.mapper_dict)
raise cherrypy.NotFound(request.path)
Andrew
[EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---