import sys import sha import RDF def getnick(subjectnode,m): """Get a nickname from a subject RDF.Node()""" foaf = RDF.NS("http://xmlns.com/foaf/0.1/") statements = m.find_statements(RDF.Statement(subjectnode, foaf.nick, None)) nickstatement = statements.current() return nickstatement.object.literal_value['string'] return "null" def getstorage(): #return RDF.MemoryStorage() return RDF.Storage(storage_name="mysql", name="foafnaut", options_string="merge='yes',host='127.0.0.1',user='julie',password='julie',database='redland'") def main(args): if len(args) != 3: print """Usage: %s type value type - sha or ID -- defines whether to search on sha1sum or jim:foafnautID value - value being searched for. possibly a bnode identifier, URI, or sha1sum."""%args[0] return # Who to query for type, sha1 = args[1:] print type, sha1 # Init s = getstorage() m = RDF.Model(s) p = RDF.Parser() foaf = RDF.NS("http://xmlns.com/foaf/0.1/") # Load data -- if you are loading from an existing Store, this is unneeded. #p.parse_into_model(m, "http://crschmidt.net/foaf.rdf") # Find the RDF.Node that the person is if type == "sha": statements = m.find_statements(RDF.Statement(None, foaf.mbox_sha1sum, RDF.Node(sha))) i = statements.current() person = i.subject elif type == "ID": if "://" in sha1: # This is a really crappy way of checking it. Should I even check it? Who knows. person = RDF.Node(RDF.Uri(sha1)) elif sha1.startswith("r1"): person = RDF.Node(blank=sha1) else: raise "WrongTypeException", "ID doesn't seem to be a URI or a bnode." else: raise "InvalidArgumentsException" if person.is_resource(): id = sha.new(str(person.uri)).hexdigest() elif person.is_blank(): id = sha.new(str(person.blank_identifier)).hexdigest() f = open("cache/%s" % id, "w") # Start printing information. f.write("""\n""") print """""" print """""" f.write("""\n""") f.write("%s"%id) print "%s"%id for i in m.find_statements(RDF.Statement(person, foaf.name, None)): f.write("%s\n"%i.object.literal_value['string']) print "%s"%i.object.literal_value['string'] for i in m.find_statements(RDF.Statement(person, foaf.nick, None)): f.write("%s\n"%i.object.literal_value['string']) print "%s"%i.object.literal_value['string'] for i in m.find_statements(RDF.Statement(person, foaf.homepage, None)): if (i.object.is_resource()): print "\n" % i.object.uri for i in m.find_statements(RDF.Statement(person, foaf.depiction, None)): if (i.object.is_resource()): f.write("""\n""" % i.object.uri) print """""" % i.object.uri for i in m.find_statements(RDF.Statement(person, foaf.knows, None)): nick = getnick(i.object,m) if i.object.is_blank(): f.write("\n" % (i.object.blank_identifier, sha.new(str(i.object.blank_identifier)).hexdigest(),nick)) print "" % (i.object.blank_identifier, sha.new(str(i.object.blank_identifier)).hexdigest(),nick) elif i.object.is_resource(): f.write("\n" % (i.object.uri,sha.new(str(i.object.uri)).hexdigest(),nick)) print "" % (i.object.uri,sha.new(str(i.object.uri)).hexdigest(),nick) for i in m.find_statements(RDF.Statement(None, foaf.knows, person)): nick = getnick(i.subject,m) if i.subject.is_blank(): f.write("""\n""" % (sha.new(str(i.subject.blank_identifier)).hexdigest(), nick)) print """""" % (sha.new(str(i.subject.blank_identifier)).hexdigest(), nick) elif i.subject.is_resource(): f.write("""\n""" % (sha.new(str(i.subject.uri)).hexdigest(), nick)) print """""" % (sha.new(str(i.subject.uri)).hexdigest(), nick) f.write("") print "" if __name__ == "__main__": main(sys.argv)