def to_tree(graph, context_data=None, base=None, generate_compact=True):

    tree = {}

    # TODO: Check which prefixes are actually used when creating curies and add
    # incrementally. (Use rdflib features for this, e.g. RecursiveSerializer?)
    if not context_data:
        if generate_compact:
            context_data = dict((pfx, unicode(ns))
                    for (pfx, ns) in graph.namespaces() if pfx and
                    unicode(ns) != u"http://www.w3.org/XML/1998/namespace")

    if isinstance(context_data, Context):
            context = context_data
            context_data = context.to_dict()
    else:
        context = Context(context_data)

    if context_data:
        tree[context.context_key] = context_data

    nodes = []

    state = (graph, context, base)

    # TODO: framing/CBD-with-startnode...
    for s in set(graph.subjects()):
        # only unreferenced.. TODO: not if more than one ref!
        if isinstance(s, URIRef) or not any(graph.subjects(None, s)):
            current = _subject_to_node(state, s)
            nodes.append(current)

    if len(nodes) == 1:
        tree.update(nodes[0])
    else:
        tree[context.id_key] = nodes

    return tree
@@ TODO: add docstring describing usage
dataset/ETHPy150Open smart-classic/smart_server/smart/lib/rdflib-jsonld/rdflib_jsonld/jsonld_serializer.py/to_tree