Example #1
0
def to_rdf(data, graph, base=None, context_data=None):
    """
    @@ TODO: add docstring describing args and returned value type
    """
    context = Context()

    if isinstance(data, list):
        resources = data
    elif isinstance(data, dict):
        resources = data.get(context.graph_key, data)
        if not isinstance(resources, list):
            resources = [resources]
        context_data = data.get(CONTEXT_KEY) or context_data

    if context_data:
        context.load(context_data)

    if context.vocab:
        graph.bind(None, context.vocab)
    for term in context.terms:
        if term.iri and term.iri.endswith(('/', '#', ':')):
            graph.bind(term.key, term.iri)

    state = graph, context, base
    for node in resources:
        _add_to_graph(state, node)

    return graph
Example #2
0
def to_rdf(tree, graph, base=None, context_data=None):
    """
    @@ TODO: add docstring describing args and returned value type
    """
    context = Context()
    context.load(context_data or tree.get(CONTEXT_KEY) or {}, base)

    id_obj = tree.get(context.id_key)
    resources = id_obj
    if not isinstance(id_obj, list):
        resources = [tree]

    for term in context.terms:
        if term.iri and term.iri.endswith(('/', '#', ':')):
            graph.bind(term.key, term.iri)

    state = graph, context, base
    for node in resources:
        _add_to_graph(state, node)

    return graph
Example #3
0
def to_rdf(tree, graph, base=None, context_data=None):
    """
    @@ TODO: add docstring describing args and returned value type
    """
    context = Context()
    context.load(context_data or tree.get(CONTEXT_KEY) or {}, base)

    id_obj = tree.get(context.id_key)
    resources = id_obj
    if not isinstance(id_obj, list):
        resources = [tree]

    for term in context.terms:
        if term.iri and term.iri.endswith(('/', '#', ':')):
            graph.bind(term.key, term.iri)

    state = graph, context, base
    for node in resources:
        _add_to_graph(state, node)

    return graph
Example #4
0
def to_tree(graph,
            context_data=None,
            base=None,
            generate_compact=True,
            context_uri=None):
    """
    @@ TODO: add docstring describing args and returned value type
    """

    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_uri:
        tree[context.context_key] = context_uri
    elif 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.graph_key] = nodes

    return tree
Example #5
0
def _add_to_graph(state, node):
    graph, context, base = state
    if not isinstance(node, dict) or context.value_key in node:
        return

    l_ctx = node.get(CONTEXT_KEY)
    if l_ctx:
        context = Context(context.to_dict())
        context.load(l_ctx)

    id_val = node.get(context.id_key)
    if isinstance(id_val, unicode) and (not bNodeIdRegexp.match(id_val)):
        subj = URIRef(context.expand(id_val), base)
    else:
        subj = BNode()

    for pred_key, obj in node.items():
        term = None

        if not isinstance(obj, list):
            obj_nodes = [obj]
        else:
            obj_nodes = obj

        if pred_key in (CONTEXT_KEY, context.id_key):
            continue
        if pred_key == context.type_key:
            pred = RDF.type
            term = Term(None, None, context.id_key)
        elif pred_key == context.graph_key:
            for onode in obj_nodes:
                _add_to_graph(state, onode)
            continue
        else:
            pred_uri = context.expand(pred_key)
            if not pred_uri:
                continue
            pred = URIRef(pred_uri)
            # TODO: refactor context to get_term by key or iri
            term = context._term_map.get(pred_key) or context.get_term(pred_uri)

        if term:
            if term.container == LIST_KEY:
                obj_nodes = [{context.list_key: obj_nodes}]
            elif isinstance(obj, dict):
                if term.container == INDEX_KEY:
                    obj_nodes = []
                    for values in obj.values():
                        if not isinstance(values, list):
                            obj_nodes.append(values)
                        else:
                            obj_nodes += values
                elif term.container == LANG_KEY:
                    obj_nodes = []
                    for lang, values in obj.items():
                        if not isinstance(values, list):
                            values = [values]
                        for v in values:
                            obj_nodes.append((v, lang))

        for obj_node in obj_nodes:
            obj = _to_object(state, term, obj_node)
            if obj is None:
                continue
            if term and term.coercion == REV_KEY:
                graph.add((obj, pred, subj))
            else:
                graph.add((subj, pred, obj))

    return subj