def prepare_tree(results):
    '''
    Filter results for matching types and render tree
    '''
    ## List view
    if not CONFIGURATION['tree']:
        # Fast track: if there is no filter, do not filter
        if not CONFIGURATION['type']:
            return results

        # Slow track: do filter
        return [l for l in results if l['type'] in CONFIGURATION['type']]

    ## Tree view
    tree = {}
    rendered = []

    # Build tree
    for line in results:
        cgroup = line['cgroup']
        parent = os.path.dirname(cgroup)

        # Root cgroup ?
        if parent == cgroup:
            rendered.append(line)
            continue

        # Insert in hierarchie as needed
        if parent not in tree:
            tree[parent] = []
        tree[parent].append(line)

    # If there are filters, filter
    if CONFIGURATION['type']:
         filter_tree(tree, CONFIGURATION['type'])

    # Render tree, starting from root
    render_tree(rendered, tree)
    return rendered
dataset/ETHPy150Open yadutaf/ctop/cgroup_top.py/prepare_tree/original