示例#1
0
文件: utils.py 项目: ktbs/ktbs
def mint_uri_from_label(label, target, uri=None, suffix=""):
    """
    Mint a URI for a resource posted to `target` based on `label`.

    :param label:  the label for the resource to create
    :param target: the resource "containing" the resource to create
    :param uri:    if provided, will be used instead (must be fresh)
    :param suffix: if provided, will be added to the end of the URI

    :return: a URI not present in `target.state`
    :rtype: rdflib.URIRef
    :raise: InvalidDataError if `uri` is provided and not acceptable
    """
    if uri is not None:
        uri = coerce_to_uri(uri, target.uri)
        if not check_new(target.state, uri):
            raise InvalidDataError("URI already in use <%s>" % uri)
        if not uri.startswith(target.uri):
            raise InvalidDataError(
                "URI is wrong <%s> (did you forget a leading '#'?)" % uri)
    else:
        label = label.lower()
        prefix = target.uri
        if prefix[-1] != "/":
            prefix = "%s#" % prefix
        prefix = "%s%s" % (prefix, _NON_ALPHA.sub("-", label))
        uri = URIRef("%s%s" % (prefix, suffix))
        if not check_new(target.state, uri):
            prefix = "%s-" % prefix
            uri = make_fresh_uri(target.state, prefix, suffix)
    return uri
示例#2
0
def mint_uri_from_label(label, target, uri=None, suffix=""):
    """
    Mint a URI for a resource posted to `target` based on `label`.

    :param label:  the label for the resource to create
    :param target: the resource "containing" the resource to create
    :param uri:    if provided, will be used instead (must be fresh)
    :param suffix: if provided, will be added to the end of the URI

    :return: a URI not present in `target.state`
    :rtype: rdflib.URIRef
    :raise: InvalidDataError if `uri` is provided and not acceptable
    """
    if uri is not None:
        uri = coerce_to_uri(uri, target.uri)
        if not check_new(target.state, uri):
            raise InvalidDataError("URI already in use <%s>" % uri)
        if not uri.startswith(target.uri):
            raise InvalidDataError(
                "URI is wrong <%s> (did you forget a leading '#'?)" % uri)
    else:
        label = label.lower()
        prefix = target.uri
        if prefix[-1] != "/":
            prefix = "%s#" % prefix
        prefix = "%s%s" % (prefix, _NON_ALPHA.sub("-", label))
        uri = URIRef("%s%s" % (prefix, suffix))
        if not check_new(target.state, uri):
            prefix = "%s-" % prefix
            uri = make_fresh_uri(target.state, prefix, suffix)
    return uri
示例#3
0
文件: obsel.py 项目: HuaiQiu/ktbs
    def mint_uri(cls, target, new_graph, created, basename="o", suffix=""):
        """I implement :meth:`rdfrest.cores.local.ILocalCore.mint_uri`.

        I use the skos:prefLabel of the resource to mint a URI, else the
        basename.
        """
        # Do NOT call super method, as this is the base implementation.
        label = (new_graph.value(created, SKOS.prefLabel)
                 or basename).lower()
        prefix = "%s%s-" % (target.uri, _NON_ALPHA.sub("-", label))
        return make_fresh_uri(target.obsel_collection.state, prefix, suffix)
示例#4
0
文件: utils.py 项目: HuaiQiu/ktbs
def replace_obsels(computed_trace, raw_graph, inherit=False):
    """
    Replace the @obsels graph of computed_trace with raw_graph.

    If raw_graph contains blank obsels, a URI will be generated for them.
    Except for that, no processing or verification is done on raw_graph,
    so it must be valid.
    """
    obsels = computed_trace.obsel_collection
    rg_add = raw_graph.add
    ct_uri = computed_trace.uri

    if inherit:
        rg_val = raw_graph.value
        getobs = computed_trace.service.get
        triples = raw_graph.triples((None, KTBS.hasSourceObsel, None))
        for newnode, _, olduri in triples:
            # if ktbs:hasTrace is not specified,
            # it must be smartly set and *not* inherited:
            rg_add((newnode, KTBS.hasTrace, ct_uri))
            oldobs = getobs(olduri)
            for _, prop, val in oldobs.state.triples((olduri, None, None)):
                if rg_val(newnode, prop) is None:
                    rg_add((newnode, prop, val))

    bnodes = [
        i for i in raw_graph.subjects(KTBS.hasBegin, None)
        if isinstance(i, BNode)
    ]
    bnode_map = None
    if bnodes:
        bnode_map = {}
        for bnode in bnodes:
            new_uri = make_fresh_uri(raw_graph, ct_uri + "o-")
            bnode_map[bnode] = new_uri
            rg_add((new_uri, KTBS.hasTrace, ct_uri))

    with obsels.edit(_trust=True) as editable:
        obsels._empty()  # friend #pylint: disable=W0212
        if bnodes:
            bm_get = bnode_map.get
            triples = ([bm_get(x, x) for x in triple] for triple in raw_graph)
        else:
            triples = iter(raw_graph)
        editable.addN((s, p, o, editable) for s, p, o in triples)
示例#5
0
文件: utils.py 项目: ktbs/ktbs
def replace_obsels(computed_trace, raw_graph, inherit=False):
    """
    Replace the @obsels graph of computed_trace with raw_graph.

    If raw_graph contains blank obsels, a URI will be generated for them.
    Except for that, no processing or verification is done on raw_graph,
    so it must be valid.
    """
    obsels = computed_trace.obsel_collection
    rg_add = raw_graph.add
    ct_uri = computed_trace.uri

    if inherit:
        rg_val = raw_graph.value
        getobs = computed_trace.service.get
        triples = raw_graph.triples((None, KTBS.hasSourceObsel, None))
        for newnode, _, olduri in triples:
            # if ktbs:hasTrace is not specified,
            # it must be smartly set and *not* inherited:
            rg_add((newnode, KTBS.hasTrace, ct_uri))
            oldobs = getobs(olduri)
            for _, prop, val in oldobs.state.triples((olduri, None, None)):
                if rg_val(newnode, prop) is None:
                    rg_add((newnode, prop, val))

    bnodes = [ i for i in raw_graph.subjects(KTBS.hasBegin, None)
               if isinstance(i, BNode) ]
    bnode_map = None
    if bnodes:
        bnode_map = {}
        for bnode in bnodes:
            new_uri = make_fresh_uri(raw_graph, ct_uri + "o-")
            bnode_map[bnode] = new_uri
            rg_add((new_uri, KTBS.hasTrace, ct_uri))

    with obsels.edit(_trust=True) as editable:
        obsels._empty() # friend #pylint: disable=W0212
        if bnodes:
            bm_get = bnode_map.get
            triples = ( [ bm_get(x, x) for x in triple] for triple in raw_graph )
        else:
            triples = iter(raw_graph)
        editable.addN( (s, p, o, editable) for s, p, o in triples )