<s> """<STR_LIT>""" <EOL> import rdflib <EOL> import rdflib . extras . cmdlineutils <EOL> import sys <EOL> import cgi <EOL> import collections <EOL> from rdflib import XSD <EOL> LABEL_PROPERTIES = [ rdflib . RDFS . label , <EOL> rdflib . URIRef ( "<STR_LIT>" ) , <EOL> rdflib . URIRef ( "<STR_LIT>" ) , <EOL> rdflib . URIRef ( "<STR_LIT>" ) , <EOL> rdflib . URIRef ( "<STR_LIT>" ) <EOL> ] <EOL> XSDTERMS = [ <EOL> XSD [ x ] for x in ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:date>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:float>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:int>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:Name>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:string>" , "<STR_LIT:time>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> EDGECOLOR = "<STR_LIT>" <EOL> NODECOLOR = "<STR_LIT>" <EOL> ISACOLOR = "<STR_LIT>" <EOL> def rdf2dot ( g , stream , opts = { } ) : <EOL> """<STR_LIT>""" <EOL> fields = collections . defaultdict ( set ) <EOL> nodes = { } <EOL> def node ( x ) : <EOL> if x not in nodes : <EOL> nodes [ x ] = "<STR_LIT>" % len ( nodes ) <EOL> return nodes [ x ] <EOL> def label ( x , g ) : <EOL> for labelProp in LABEL_PROPERTIES : <EOL> l = g . value ( x , labelProp ) <EOL> if l : <EOL> return l <EOL> try : <EOL> return g . namespace_manager . compute_qname ( x ) [ <NUM_LIT:2> ] <EOL> except : <EOL> return x <EOL> def formatliteral ( l , g ) : <EOL> v = cgi . escape ( l ) <EOL> if l . datatype : <EOL> return u'<STR_LIT>' % ( v , qname ( l . datatype , g ) ) <EOL> elif l . language : <EOL> return u'<STR_LIT>' % ( v , l . language ) <EOL> return u'<STR_LIT>' % v <EOL> def qname ( x , g ) : <EOL> try : <EOL> q = g . compute_qname ( x ) <EOL> return q [ <NUM_LIT:0> ] + "<STR_LIT::>" + q [ <NUM_LIT:2> ] <EOL> except : <EOL> return x <EOL> def color ( p ) : <EOL> return "<STR_LIT>" <EOL> stream . write ( u"<STR_LIT>" ) <EOL> for s , p , o in g : <EOL> sn = node ( s ) <EOL> if p == rdflib . RDFS . label : <EOL> continue <EOL> if isinstance ( o , ( rdflib . URIRef , rdflib . BNode ) ) : <EOL> on = node ( o ) <EOL> opstr = u"<STR_LIT>" + u"<STR_LIT>" <EOL> stream . write ( opstr % ( sn , on , color ( p ) , qname ( p , g ) ) ) <EOL> else : <EOL> fields [ sn ] . add ( ( qname ( p , g ) , formatliteral ( o , g ) ) ) <EOL> for u , n in nodes . items ( ) : <EOL> stream . write ( u"<STR_LIT>" % ( u , n ) ) <EOL> f = [ u"<STR_LIT>" % <EOL> x for x in sorted ( fields [ n ] ) ] <EOL> opstr = u"<STR_LIT>" + u"<STR_LIT>" + u"<STR_LIT>" + u"<STR_LIT>" + u"<STR_LIT>" + u"<STR_LIT>" <EOL> stream . write ( opstr % ( n , NODECOLOR , label ( u , g ) , u , u , u"<STR_LIT>" . join ( f ) ) ) <EOL> stream . write ( "<STR_LIT>" ) <EOL> def _help ( ) : <EOL> sys . stderr . write ( """<STR_LIT>""" ) <EOL> def main ( ) : <EOL> rdflib . extras . cmdlineutils . main ( rdf2dot , _help ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
