Exemplo n.º 1
0
def showAnnotations(ro_config, ro_dir, annotations, outstr):
    sname_prev = None
    for (asubj,apred,aval) in annotations:
        # log.debug("Annotations: asubj %s, apred %s, aval %s"%
        #           (repr(asubj), repr(apred), repr(aval)))
        if apred != ORE.aggregates:
            (aname, atype) = getAnnotationByUri(ro_config, apred)
            sname = ro_manifest.getComponentUriRel(ro_dir, str(asubj))
            log.debug("Annotations: sname %s, aname %s"%(sname, aname))
            if sname == "":
                sname = ro_manifest.getRoUri(ro_dir)
            if sname != sname_prev:
                print "\n<"+str(sname)+">"
                sname_prev = sname
            outstr.write("  %s %s\n"%(aname, formatAnnotationValue(aval, atype)))
    return
Exemplo n.º 2
0
def _getAnnotationValues(ro_config, ro_dir, rofile, attrname):
    """
    Returns iterator over annotation values for given subject and attribute
    """
    log.debug("getAnnotationValues: ro_dir %s, rofile %s, attrname %s"%(ro_dir, rofile, attrname))
    ro_graph    = ro_manifest.readManifestGraph(ro_dir)
    subject     = ro_manifest.getComponentUri(ro_dir, rofile)
    (predicate,valtype) = getAnnotationByName(ro_config, attrname)
    #@@TODO refactor common code with getRoAnnotations, etc.
    for ann_node in ro_graph.subjects(predicate=RO.annotatesAggregatedResource, object=subject):
        ann_uri   = ro_graph.value(subject=ann_node, predicate=AO.body)
        ann_graph = readAnnotationBody(ro_dir, ro_manifest.getComponentUriRel(ro_dir, ann_uri))
        for v in ann_graph.objects(subject=subject, predicate=predicate):
            #log.debug("Triple: %s %s %s"%(subject,p,v))
            yield v
    return
Exemplo n.º 3
0
def _removeSimpleAnnotation(ro_config, ro_dir, rofile, attrname, attrvalue):
    """
    Remove a simple annotation or multiple matching annotations a research object.

    ro_config   is the research object manager configuration, supplied as a dictionary
    ro_dir      is the research object root directory
    rofile      names the annotated file or resource, possibly relative to the RO.
    attrname    names the attribute in a form recognized by getAnnotationByName
    attrvalue   is the attribute value to be deleted, or Nomne to delete all vaues
    """
    log.debug("removeSimpleAnnotation: ro_dir %s, rofile %s, attrname %s, attrvalue %s"%
              (ro_dir, rofile, attrname, attrvalue))
    # Enumerate annotations
    # For each:
    #     if annotation is only one in graph then:
    #         remove aggregated annotation
    #     else:
    #         create new annotation graph witj annotation removed
    #         update aggregated annotation
    ro_graph    = ro_manifest.readManifestGraph(ro_dir)
    subject     = ro_manifest.getComponentUri(ro_dir, rofile)
    (predicate,valtype) = getAnnotationByName(ro_config, attrname)
    val         = attrvalue and makeAnnotationValue(ro_config, attrvalue, valtype)
    #@@TODO refactor common code with getRoAnnotations, etc.
    add_annotations = []
    remove_annotations = []
    for ann_node in ro_graph.subjects(predicate=RO.annotatesAggregatedResource, object=subject):
        ann_uri   = ro_graph.value(subject=ann_node, predicate=AO.body)
        ann_graph = readAnnotationBody(ro_dir, ro_manifest.getComponentUriRel(ro_dir, ann_uri))
        if (subject, predicate, val) in ann_graph:
            ann_graph.remove((subject, predicate, val))
            if (subject, None, None) in ann_graph:
                # Triples remain in annotation body: write new body and update RO graph
                ann_name = createAnnotationGraphBody(ro_config, ro_dir, rofile, ann_graph)
                remove_annotations.append(ann_node)
                add_annotations.append(ann_name)
            else:
                # Remove annotation from RO graph
                remove_annotations.append(ann_node)
    # Update RO graph if needed
    if add_annotations or remove_annotations:
        for a in remove_annotations:
            _removeAnnotationBodyFromRoGraph(ro_graph, a)
        for a in add_annotations:
            _addAnnotationBodyToRoGraph(ro_graph, ro_dir, rofile, a)
        ro_manifest.writeManifestGraph(ro_dir, ro_graph)
    return
Exemplo n.º 4
0
def _getRoAnnotations(ro_dir):
    """
    Returns iterator over annotations applied to the RO as an entity.

    Each value returned by the iterator is a (subject,predicate,object) triple.
    """
    ro_graph = ro_manifest.readManifestGraph(ro_dir)
    subject  = ro_manifest.getRoUri(ro_dir)
    log.debug("getRoAnnotations %s"%str(subject))
    for ann_node in ro_graph.subjects(predicate=RO.annotatesAggregatedResource, object=subject):
        ann_uri   = ro_graph.value(subject=ann_node, predicate=AO.body)
        ann_graph = readAnnotationBody(ro_dir, ro_manifest.getComponentUriRel(ro_dir, ann_uri))
        if ann_graph:
            for (p, v) in ann_graph.predicate_objects(subject=subject):
                #log.debug("Triple: %s %s %s"%(subject,p,v))
                yield (subject, p, v)
    return
Exemplo n.º 5
0
def _getFileAnnotations(ro_dir, rofile):
    """
    Returns iterator over annotations applied to a specified component in the RO

    Each value returned by the iterator is a (subject,predicate,object) triple.
    """
    log.debug("getFileAnnotations: ro_dir %s, rofile %s"%(ro_dir, rofile))
    ro_graph    = ro_manifest.readManifestGraph(ro_dir)
    subject     = ro_manifest.getComponentUri(ro_dir, rofile)
    log.debug("getFileAnnotations: %s"%str(subject))
    #@@TODO refactor common code with getRoAnnotations, etc.
    for ann_node in ro_graph.subjects(predicate=RO.annotatesAggregatedResource, object=subject):
        ann_uri   = ro_graph.value(subject=ann_node, predicate=AO.body)
        ann_graph = readAnnotationBody(ro_dir, ro_manifest.getComponentUriRel(ro_dir, ann_uri))
        if ann_graph:
            for (p, v) in ann_graph.predicate_objects(subject=subject):
                #log.debug("Triple: %s %s %s"%(subject,p,v))
                yield (subject, p, v)
    return
Exemplo n.º 6
0
def getAllAnnotations(ro_dir):
    """
    Returns iterator over all annotations associated with the RO

    Each value returned by the iterator is a (subject,predicate,object) triple.
    """
    log.debug("getAllAnnotations %s"%str(ro_dir))
    ro_graph    = ro_manifest.readManifestGraph(ro_dir)
    #@@TODO refactor common code with getRoAnnotations, etc.
    for (ann_node, subject) in ro_graph.subject_objects(predicate=RO.annotatesAggregatedResource):
        ann_uri   = ro_graph.value(subject=ann_node, predicate=AO.body)
        log.debug("- ann_uri %s"%(str(ann_uri)))
        ann_graph = readAnnotationBody(ro_dir, ro_manifest.getComponentUriRel(ro_dir, ann_uri))
        if ann_graph == None:
            log.debug("No annotation graph: ann_uri: "+str(ann_uri))
        else:
            for (p, v) in ann_graph.predicate_objects(subject=subject):
                #log.debug("Triple: %s %s %s"%(subject,p,v))
                yield (subject, p, v)
    return