def _addSimpleAnnotation(ro_config, ro_dir, rofile, attrname, attrvalue): """ Add a simple annotation to a file in 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 file or resource to be annotated, possibly relative to the RO. attrname names the attribute in a form recognized by getAnnotationByName attrvalue is a value to be associated with the attribute """ annfile = createAnnotationBody(ro_config, ro_dir, rofile, { attrname: attrvalue} ) ro_graph = ro_manifest.readManifestGraph(ro_dir) _addAnnotationBodyToRoGraph(ro_graph, ro_dir, rofile, annfile) ro_manifest.writeManifestGraph(ro_dir, ro_graph) return
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
def _replaceSimpleAnnotation(ro_config, ro_dir, rofile, attrname, attrvalue): """ Replace a simple annotation in 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 file or resource to be annotated, possibly relative to the RO. attrname names the attribute in a form recognized by getAnnotationByName attrvalue is a new value to be associated with the attribute """ ro_graph = ro_manifest.readManifestGraph(ro_dir) subject = ro_manifest.getComponentUri(ro_dir, rofile) (predicate,valtype) = getAnnotationByName(ro_config, attrname) log.debug("Replace annotation: subject %s, predicate %s, value %s"%(repr(subject), repr(predicate), repr(attrvalue))) ro_graph.remove((subject, predicate, None)) ro_graph.add((subject, predicate, makeAnnotationValue(ro_config, attrvalue, valtype))) ro_manifest.writeManifestGraph(ro_dir, ro_graph) return
def annotate(progname, configbase, options, args): """ Annotate a specified research object component ro annotate file attribute-name [ attribute-value ] """ # Check command arguments if len(args) not in [4,5]: print ("%s annotate: wrong number of arguments provided"% (progname)) print ("Usage: %s annotate file attribute-name [ attribute-value ]"% (progname)) return 1 ro_config = ro_utils.readconfig(configbase) ro_options = { "rofile": args[2], "rodir": os.path.dirname(args[2]), "roattribute": args[3], "rovalue": args[4] or None } log.debug("ro_options: "+repr(ro_options)) # Find RO root directory ro_dir = ro_root_directory(progname+" attribute", ro_config, ro_options['rodir']) if not ro_dir: return 1 # Read and update manifest if options.verbose: print "ro annotate %(rofile)s %(roattribute)s \"%(rovalue)s\""%ro_options ro_graph = ro_manifest.readManifestGraph(ro_dir) (predicate,valtype) = getAnnotationByName(ro_config, ro_options['roattribute']) log.debug("Adding annotation predicate: %s, value %s"%(repr(predicate),repr(ro_options['rovalue']))) ro_graph.add( ( ro_manifest.getComponentUri(ro_dir, os.path.abspath(ro_options['rofile'])), predicate, rdflib.Literal(ro_options['rovalue']) ) ) ro_manifest.writeManifestGraph(ro_dir, ro_graph) return 0