def rdf2simple(rdf): class Graph: def __init__(self): self.statements = [] def triple(self, s, p, o): self.statements.append((s, p, o)) g = Graph() parseRDF(rdf, None, g) output = StringIO() for s, p, o in g.statements: if p == "<http://s.opencalais.com/1/pred/language>": print >> output, "Language: %s" % (o) for s, p, o in g.statements: if p == "<http://s.opencalais.com/1/pred/categoryName>": print >> output, "Category: %s" % (o) print >> output, "Tags: ", for s, p, o in g.statements: if p == "<http://s.opencalais.com/1/pred/name>": print >> output, ", %s" % (o), print >> output for s, p, o in g.statements: if p == "<http://s.opencalais.com/1/pred/exact>": print >> output, "%s" % (o) return output.getvalue()
def parse(self, filename): """ Parses an RDF/XML file and returns a model containing CIM elements. """ # Split the extension from the pathname root, extension = os.path.splitext(filename) if isinstance(filename, file): s = filename.read() elif extension in [".xml"]: fd = None try: fd = open(filename, "rb") s = fd.read() except: logger.error("Error reading XML data file.") finally: if fd is not None: fd.close() else: s = filename # Instantiate CIM objects and set their attributes. attr_sink = AttributeSink(self.profile) # logger.debug("Parsing objects and attributes in: %s" % filename) # If 'base' is None then no RDF namespace in triples. rdfxml.parseRDF(s, base=filename, sink=attr_sink) # Second pass to set references. ref_sink = ReferenceSink(attr_sink) # logger.debug("Starting second pass to set references.") rdfxml.parseRDF(s, base=filename, sink=ref_sink) logger.info("%d model elements created." % len(attr_sink.uri_object_map)) # Return a map of unique resource identifiers to CIM objects. return attr_sink.uri_object_map
def parse_oops_issues(oops_rdf): p = rdfxml.parseRDF(oops_rdf) raw_oops_list = p.result oops_issues = {} # Filter #1 # Construct combine all data of a single elements into one json like format for r in raw_oops_list: if r['domain'] not in oops_issues: oops_issues[r['domain']] = {} oops_issues[r['domain']][r['relation']] = r['range'] # Filter #2 # get rid of elements without issue id oops_issues_filter2 = {} for i in oops_issues: if '#' not in i: oops_issues_filter2[i] = oops_issues[i] # Filter #3 # Only include actual issues (some data are useless to us) detailed_desc = [] oops_issues_filter3 = {} for i in oops_issues_filter2: if '<http://www.oeg-upm.net/oops#hasNumberAffectedElements>' in oops_issues_filter2[ i]: oops_issues_filter3[i] = oops_issues_filter2[i] # Filter #4 # Only include data of interest about the issue oops_issues_filter4 = {} issue_interesting_data = [ '<http://www.oeg-upm.net/oops#hasName>', '<http://www.oeg-upm.net/oops#hasCode>', '<http://www.oeg-upm.net/oops#hasDescription>', '<http://www.oeg-upm.net/oops#hasNumberAffectedElements>', '<http://www.oeg-upm.net/oops#hasImportanceLevel>', #'<http://www.oeg-upm.net/oops#hasAffectedElement>', '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>', ] for i in oops_issues_filter3: oops_issues_filter4[i] = {} for intda in issue_interesting_data: if intda in oops_issues_filter3[i]: oops_issues_filter4[i][intda] = oops_issues_filter3[i][intda] return oops_issues_filter4, issue_interesting_data
def parse_oops_issues(oops_rdf): p = rdfxml.parseRDF(oops_rdf) raw_oops_list = p.result oops_issues={} #Filter #1 #Construct combine all data of a single elements into one json like format for r in raw_oops_list: if r['domain'] not in oops_issues: oops_issues[r['domain']] = {} oops_issues[r['domain']][r['relation']] = r['range'] #Filter #2 #get rid of elements without issue id oops_issues_filter2={} for i in oops_issues: if '#' not in i: oops_issues_filter2[i] = oops_issues[i] #Filter #3 #Only include actual issues (some data are useless to us) detailed_desc = [] oops_issues_filter3={} for i in oops_issues_filter2: if '<http://www.oeg-upm.net/oops#hasNumberAffectedElements>' in oops_issues_filter2[i]: oops_issues_filter3[i] = oops_issues_filter2[i] #Filter #4 #Only include data of interest about the issue oops_issues_filter4={} issue_interesting_data = [ '<http://www.oeg-upm.net/oops#hasName>', '<http://www.oeg-upm.net/oops#hasCode>', '<http://www.oeg-upm.net/oops#hasDescription>', '<http://www.oeg-upm.net/oops#hasNumberAffectedElements>', '<http://www.oeg-upm.net/oops#hasImportanceLevel>', #'<http://www.oeg-upm.net/oops#hasAffectedElement>', '<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>', ] for i in oops_issues_filter3: oops_issues_filter4[i]={} for intda in issue_interesting_data: if intda in oops_issues_filter3[i]: oops_issues_filter4[i][intda] = oops_issues_filter3[i][intda] return oops_issues_filter4, issue_interesting_data
print("Ready to download info for pid " + pid) print(progurl) # Grab BBC data try: rdfconn = urllib2.urlopen(progurl) print ("Connected to requested programme. Awaiting data...") except URLError, e: print ("BBC connection failed - aborting") print (e.reason) sys.exit(0) # Print data to the screen if rdfconn: content = rdfconn.read() rdfconn.close() data = rdfxml.parseRDF(content).result print(data) print("Unfortunately I haven't a clue what to do with this, so let's send twitter the show title instead") if __name__=="__main__": # Load Config try: homedir = os.path.expanduser("~") file = open(homedir + "/twitter-login.conf") except IOError, e: print ("Failed to load login data - exiting") sys.exit(0) raw_config = file.read() file.close()
def rdfToPython(s, base=None): sink = Sink() return rdfxml.parseRDF(s, base=None, sink=sink).result
print("Ready to download info for pid " + pid) print(progurl) # Grab BBC data try: rdfconn = urllib2.urlopen(progurl) print("Connected to requested programme. Awaiting data...") except URLError, e: print("BBC connection failed - aborting") print(e.reason) sys.exit(0) # Print data to the screen if rdfconn: content = rdfconn.read() rdfconn.close() data = rdfxml.parseRDF(content).result print(data) print( "Unfortunately I haven't a clue what to do with this, so let's send twitter the show title instead" ) if __name__ == "__main__": # Load Config try: homedir = os.path.expanduser("~") file = open(homedir + "/twitter-login.conf") except IOError, e: print("Failed to load login data - exiting") sys.exit(0)