def url_for(name, *values, **kw_values):
    """
    Generates a URL for the route with the given name. You may give either an
    absolute name for the route or use a period to match names relative to the
    current route. Multiple periods may be used to traverse up the name tree.

    Passed arguments will be used to construct the URL. Any unknown keyword
    arguments will be appended to the URL as query arguments. Additionally,
    there are several special keyword arguments to customize
    ``url_for``'s behavior.

    ==========  ========  ============
    Argument    Default   Description
    ==========  ========  ============
    _anchor     None      *Optional.* An anchor string to be appended to the URL.
    _doseq      True      *Optional.* The value to pass to :func:`urllib.urlencode`'s ``doseq`` parameter for building the query string.
    _external   False     *Optional.* Whether or not a URL is meant for external use. External URLs never have their host portion removed.
    _scheme     None      *Optional.* The scheme of the link to generate. By default, this is set to the scheme of the current request.
    ==========  ========  ============
    """

    if '_request' in kw_values:
        request = kw_values.pop('_request')
    else:
        app = Application.current_app
        if not app or not app.request:
            raise RuntimeError("Called url_for outside of a request.")
        request = app.request

    # Handle periods, which are for moving up the module table.
    if name[0] == '.':
        # Count and remove the periods.
        count = len(name)
        name = name.lstrip('.')
        count = count - len(name)

        # Now, build a list of route names, and pop one item off for every
        # period we've counted.
        mod_name = request.route_name.split('.')
        if count >= len(mod_name):
            del mod_name[:]
        else:
            del mod_name[len(mod_name) - count:]

        mod_name.append(name)
        name = '.'.join(mod_name)

    if not name in app._name_table:
        raise KeyError("Cannot find request handler with name %r." % name)

    rule_table = app._name_table[name]
    names, namegen, converters = rule_table[-3:]

    data = []
    values = list(values)

    for i in xrange(len(names)):
        name = names[i]

        if name in kw_values:
            val = kw_values.pop(name)
        elif values:
            val = values.pop(0)
        elif converters[i].default is not None:
            val = NoValue
        else:
            raise ValueError("Missing required value %r." % name)

        # Process the data.
        if val is NoValue:
            data.append(converters[i].default)
        else:
            data.append(converters[i].encode(request, val))

    # If we still have values, we were given too many.
    if values:
        raise ValueError("Too many values to unpack.")

    # Generate the string.
    out = namegen % tuple(data)

    dmn, sep, pth = out.partition("/")
    out = dmn + sep + urllib.quote(pth)

    if '_external' in kw_values:
        if kw_values['_external'] and out[0] == '/':
            out = request.host + out
        elif not kw_values['_external'] and out[0] != '/':
            _, _, out = out.partition('/')
            out = '/' + out
        del kw_values['_external']
    else:
        if not ":" in out and out.lower().startswith(request.hostname.lower()):
            out = out[len(request.hostname):]
        elif out.lower().startswith(request.host.lower()):
            out = out[len(request.host):]

    if '_scheme' in kw_values:
        if not out[0] == "/":
            out = "%s://%s" % (kw_values['_scheme'], out)
        elif request.scheme.lower() != kw_values['_scheme'].lower():
            out = "%s://%s%s" % (kw_values['_scheme'], request.host, out)
        del kw_values['_scheme']
    else:
        if not out[0] == "/":
            out = '%s://%s' % (request.scheme, out)

    # Remove the anchor before adding query string variables.
    anchor = kw_values.pop('_anchor', None)

    # Build the query
    if kw_values:
        out += '?%s' % urllib.urlencode(kw_values, doseq=kw_values.pop('_doseq', True))

    if anchor:
        out += '#' + anchor

    return out
dataset/ETHPy150Open ecdavis/pants/pants/web/application.py/url_for/original