"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/ui/directory.py $
$Id: directory.py 28617 2006-08-18 18:45:36Z dbinger $
"""
from dulcinea.common import get_request, get_response, get_path, is_qp
from qp.fill.directory import get_directory_path

assert not is_qp()

def get_crumb_tree():
    def get_path_directory_list():
        app_root = get_request().get_environ('SCRIPT_NAME', '')
        path = get_path()
        path_components = path[len(app_root):].split('/')
        namespace_stack = get_directory_path()
        if get_response().get_status_code() == 200:
            offset = -1
        else:
            offset = 0
        return [('./' + '../' * (len(namespace_stack) - k + offset),
                 namespace_stack[k])
                for k in range(min(len(path_components), len(namespace_stack)))]

    def gen_crumb_tree_menu_items(path, directory):
        for component, name, crumb, title in directory.get_exports():
            if crumb: # skip crumbs and menus if no crumb
                css_class = 'menu_' + component.replace('.', '_')
                yield (path + component, crumb, title, css_class)


    """() - > [[(path:str, crumb:str, title:str, css_class:str)]]
    Each list in the result is a menu of exports for a directory on the
    current path with a derived css class name for each menu entry.
    """
    crumb_tree = []
    for path, directory in get_path_directory_list():
        menu = list(gen_crumb_tree_menu_items(path, directory))
        if menu:
            crumb_tree.append(menu)
    return crumb_tree
