<s> from repoze . bfg . encode import urlencode <EOL> from repoze . bfg . threadlocal import get_current_registry <EOL> from repoze . bfg . url import _join_elements <EOL> from pylons . interfaces import IRoutesMapper <EOL> def route_url ( route_name , request , * elements , ** kw ) : <EOL> try : <EOL> reg = request . registry <EOL> except AttributeError : <EOL> reg = get_current_registry ( ) <EOL> mapper = reg . getUtility ( IRoutesMapper ) <EOL> route = mapper . routes . get ( route_name ) <EOL> if route and '<STR_LIT>' in route . __dict__ : <EOL> route_name , request , elements , kw = route . custom_url_generator ( <EOL> route_name , request , * elements , ** kw ) <EOL> anchor = '<STR_LIT>' <EOL> qs = '<STR_LIT>' <EOL> app_url = None <EOL> if '<STR_LIT>' in kw : <EOL> qs = '<STR_LIT:?>' + urlencode ( kw . pop ( '<STR_LIT>' ) , doseq = True ) <EOL> if '<STR_LIT>' in kw : <EOL> anchor = kw . pop ( '<STR_LIT>' ) <EOL> if isinstance ( anchor , unicode ) : <EOL> anchor = anchor . encode ( '<STR_LIT:utf-8>' ) <EOL> anchor = '<STR_LIT:#>' + anchor <EOL> if '<STR_LIT>' in kw : <EOL> app_url = kw . pop ( '<STR_LIT>' ) <EOL> path = mapper . generate ( route_name , kw ) <EOL> if elements : <EOL> suffix = _join_elements ( elements ) <EOL> if not path . endswith ( '<STR_LIT:/>' ) : <EOL> suffix = '<STR_LIT:/>' + suffix <EOL> else : <EOL> suffix = '<STR_LIT>' <EOL> if app_url is None : <EOL> app_url = request . application_url <EOL> return app_url + path + suffix + qs + anchor </s>
