def flatten_path(path, flatten_slashes=False):
    '''Flatten an absolute URL path by removing the dot segments.

    :func:`urllib.parse.urljoin` has some support for removing dot segments,
    but it is conservative and only removes them as needed.

    Arguments:
        path (str): The URL path.
        flatten_slashes (bool): If True, consecutive slashes are removed.

    The path returned will always have a leading slash.
    '''
    # Based on posixpath.normpath

    # Fast path
    if not path or path == '/':
        return '/'

    # Take off leading slash
    if path[0] == '/':
        path = path[1:]

    parts = path.split('/')
    new_parts = collections.deque()

    for part in parts:
        if part == '.' or (flatten_slashes and not part):
            continue
        elif part != '..':
            new_parts.append(part)
        elif new_parts:
            new_parts.pop()

    # If the filename is empty string
    if flatten_slashes and path.endswith('/') or not len(new_parts):
        new_parts.append('')

    # Put back leading slash
    new_parts.appendleft('')

    return '/'.join(new_parts)
Flatten an absolute URL path by removing the dot segments.

    :func:`urllib.parse.urljoin` has some support for removing dot segments,
    but it is conservative and only removes them as needed.

    Arguments:
        path (str): The URL path.
        flatten_slashes (bool): If True, consecutive slashes are removed.

    The path returned will always have a leading slash.