예제 #1
0
파일: asqc.py 프로젝트: gklyne/asqc
def outputResult(progname, options, result):
    outstr = sys.stdout
    if options.output and options.output != "-":
        print "Output to other than stdout not implemented"
    if isinstance(result, rdflib.Graph):
        rdfformatdefault = RDFTYPSERIALIZERMAP[RDFTYP[0]]
        rdfformatselect  = RDFTYPSERIALIZERMAP.get(options.format_rdf_out, rdfformatdefault)
        result.serialize(destination=outstr, format=rdfformatselect, base=None)
    elif isinstance(result, str):
        outstr.write(result)
    else:
        if options.format_var_out == "JSON" or options.format_var_out == None:
            outstr.write(json.dumps(result))
            outstr.write("\n")
        elif options.format_var_out == "XML":
            writeResultsXML(outstr, result)
        elif options.format_var_out == "CSV":
            qvars = result["head"]["vars"]
            outstr.write(", ".join(qvars))
            outstr.write("\n")
            for bindings in result["results"]["bindings"]:
                vals = [ termToCSV(bindings[str(v)]) for v in qvars ]
                outstr.write(", ".join(vals))
                outstr.write("\n")
        else:
            for bindings in result["results"]["bindings"]:
                #log.debug("options.format_var_out '%s'"%(repr(options.format_var_out)))
                formattedrow = formatBindings(options.format_var_out, bindings)
                #log.debug("formattedrow '%s'"%(repr(formattedrow)))
                outstr.write(formattedrow)
    return
예제 #2
0
파일: asqc.py 프로젝트: timrdf/asqc
def outputResult(progname, options, result):
    outstr = sys.stdout
    if options.output and options.output != "-":
        print "Output to other than stdout not implemented"
    if isinstance(result, rdflib.Graph):
        rdfformatdefault = RDFTYPSERIALIZERMAP[RDFTYP[0]]
        rdfformatselect  = RDFTYPSERIALIZERMAP.get(options.format_rdf_out, rdfformatdefault)
        result.serialize(destination=outstr, format=rdfformatselect, base=None)
    elif isinstance(result, str):
        outstr.write(result)
    else:
        if options.format_var_out == "JSON" or options.format_var_out == None:
            outstr.write(json.dumps(result))
            outstr.write("\n")
        elif options.format_var_out == "XML":
            writeResultsXML(outstr, result)
        else:
            for bindings in result["results"]["bindings"]:
                formattedrow = formatBindings(options.format_var_out, bindings)
                outstr.write(formattedrow)
    return