#NEWLINE# def url_for( * args , **kargs ) : #NEWLINE# #INDENT# "Generates a URL\n\n    All keys given to url_for are sent to the Routes Mapper instance for\n    generation except for::\n\n        anchor          specified the anchor name to be appened to the path\n        host            overrides the default (current) host if provided\n        protocol        overrides the default (current) protocol if provided\n        qualified       creates the URL with the host/port information as\n                        needed\n\n    The URL is generated based on the rest of the keys. When generating a new\n    URL, values will be used from the current request's parameters (if\n    present). The following rules are used to determine when and how to keep\n    the current requests parameters:\n\n    * If the controller is present and begins with '/', no defaults are used\n    * If the controller is changed, action is set to 'index' unless otherwise\n      specified\n\n    For example, if the current request yielded a dict of\n    {'controller': 'blog', 'action': 'view', 'id': 2}, with the standard\n    ':controller/:action/:id' route, you'd get the following results::\n\n        url_for(id=4)                    =>  '/blog/view/4',\n        url_for(controller='/admin')     =>  '/admin',\n        url_for(controller='admin')      =>  '/admin/view/2'\n        url_for(action='edit')           =>  '/blog/edit/2',\n        url_for(action='list', id=None)  =>  '/blog/list'\n\n    **Static and Named Routes**\n\n    If there is a string present as the first argument, a lookup is done\n    against the named routes table to see if there's any matching routes. The\n    keyword defaults used with static routes will be sent in as GET query\n    arg's if a route matches.\n\n    If no route by that name is found, the string is assumed to be a raw URL.\n    Should the raw URL begin with ``/`` then appropriate SCRIPT_NAME data will\n    be added if present, otherwise the string will be used as the url with\n    keyword args becoming GET query args.\n\n    " #NEWLINE# anchor = kargs . get ( 'anchor' ) #NEWLINE# host = kargs . get ( 'host' ) #NEWLINE# protocol = kargs . pop ( 'protocol' , None ) #NEWLINE# qualified = kargs . pop ( 'qualified' , None ) #NEWLINE# for key in [ 'anchor' , 'host' ] : #NEWLINE# #INDENT# if kargs . get ( key ) : #NEWLINE# #INDENT# del kargs [ key ] #NEWLINE# #UNINDENT# if ( ( key + '_' ) in kargs ) : #NEWLINE# #INDENT# kargs [ key ] = kargs . pop ( ( key + '_' ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( 'protocol_' in kargs ) : #NEWLINE# #INDENT# kargs [ 'protocol_' ] = protocol #NEWLINE# #UNINDENT# config = request_config ( ) #NEWLINE# route = None #NEWLINE# static = False #NEWLINE# encoding = config . mapper . encoding #NEWLINE# url = '' #NEWLINE# if ( len ( args ) > 0 ) : #NEWLINE# #INDENT# route = config . mapper . _routenames . get ( args [ 0 ] ) #NEWLINE# if ( not route ) : #NEWLINE# #INDENT# static = True #NEWLINE# url = args [ 0 ] #NEWLINE# #UNINDENT# if ( url . startswith ( '/' ) and hasattr ( config , 'environ' ) and config . environ . get ( 'SCRIPT_NAME' ) ) : #NEWLINE# #INDENT# url = ( config . environ . get ( 'SCRIPT_NAME' ) + url ) #NEWLINE# #UNINDENT# if static : #NEWLINE# #INDENT# if kargs : #NEWLINE# #INDENT# url += '?' #NEWLINE# query_args = [ ] #NEWLINE# for ( key , val ) in six . iteritems ( kargs ) : #NEWLINE# #INDENT# if isinstance ( val , ( list , tuple ) ) : #NEWLINE# #INDENT# for value in val : #NEWLINE# #INDENT# query_args . append ( ( '%s=%s' % ( urllib . parse . quote ( six . text_type ( key ) . encode ( encoding ) ) , urllib . parse . quote ( six . text_type ( value ) . encode ( encoding ) ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# query_args . append ( ( '%s=%s' % ( urllib . parse . quote ( six . text_type ( key ) . encode ( encoding ) ) , urllib . parse . quote ( six . text_type ( val ) . encode ( encoding ) ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# url += '&' . join ( query_args ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# environ = getattr ( config , 'environ' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# if ( 'wsgiorg.routing_args' not in environ ) : #NEWLINE# #INDENT# environ = environ . copy ( ) #NEWLINE# mapper_dict = getattr ( config , 'mapper_dict' , None ) #NEWLINE# if ( mapper_dict is not None ) : #NEWLINE# #INDENT# match_dict = mapper_dict . copy ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# match_dict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# environ [ 'wsgiorg.routing_args' ] = ( ( ) , match_dict ) #NEWLINE# #UNINDENT# if ( not static ) : #NEWLINE# #INDENT# route_args = [ ] #NEWLINE# if route : #NEWLINE# #INDENT# if config . mapper . hardcode_names : #NEWLINE# #INDENT# route_args . append ( route ) #NEWLINE# #UNINDENT# newargs = route . defaults . copy ( ) #NEWLINE# newargs . update ( kargs ) #NEWLINE# if route . filter : #NEWLINE# #INDENT# newargs = route . filter ( newargs ) #NEWLINE# #UNINDENT# if ( not route . static ) : #NEWLINE# #INDENT# newargs = _subdomain_check ( newargs , config . mapper , environ ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# newargs = _screenargs ( kargs , config . mapper , environ ) #NEWLINE# #UNINDENT# anchor = ( newargs . pop ( '_anchor' , None ) or anchor ) #NEWLINE# host = ( newargs . pop ( '_host' , None ) or host ) #NEWLINE# protocol = newargs . pop ( '_protocol' , protocol ) #NEWLINE# url = config . mapper . generate ( * route_args , ** newargs ) #NEWLINE# #UNINDENT# if ( anchor is not None ) : #NEWLINE# #INDENT# url += ( '#' + _url_quote ( anchor , encoding ) ) #NEWLINE# #UNINDENT# if ( host or ( protocol is not None ) or qualified ) : #NEWLINE# #INDENT# if ( ( not host ) and ( not qualified ) ) : #NEWLINE# #INDENT# host = config . host . split ( ':' ) [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not host ) : #NEWLINE# #INDENT# host = config . host #NEWLINE# #UNINDENT# #UNINDENT# if ( protocol is None ) : #NEWLINE# #INDENT# protocol = config . protocol #NEWLINE# #UNINDENT# if ( protocol != '' ) : #NEWLINE# #INDENT# protocol += ':' #NEWLINE# #UNINDENT# if ( url is not None ) : #NEWLINE# #INDENT# url = ( ( ( protocol + '//' ) + host ) + url ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( not ascii_characters ( url ) ) and ( url is not None ) ) : #NEWLINE# #INDENT# raise GenerationException ( ( 'url_for can only return a string, got unicode instead: %s' % url ) ) #NEWLINE# #UNINDENT# if ( url is None ) : #NEWLINE# #INDENT# raise GenerationException ( ( 'url_for could not generate URL. Called with args: %s %s' % ( args , kargs ) ) ) #NEWLINE# #UNINDENT# return url