<s> """<STR_LIT>""" <EOL> import random <EOL> from quepy . expression import isnode <EOL> from quepy . dsl import IsRelatedTo , HasKeyword <EOL> from quepy . encodingpolicy import assert_valid_encoding <EOL> def escape ( x , add_quotes = True ) : <EOL> x = unicode ( x ) <EOL> x = x . replace ( u"<STR_LIT:U+0020>" , u"<STR_LIT:_>" ) <EOL> x = x . replace ( u"<STR_LIT:\n>" , u"<STR_LIT>" ) <EOL> x = x . replace ( u"<STR_LIT>" , u"<STR_LIT>" ) <EOL> x = x . replace ( u"<STR_LIT:[>" , u"<STR_LIT>" ) <EOL> x = x . replace ( u"<STR_LIT:]>" , u"<STR_LIT>" ) <EOL> x = x . replace ( u"<STR_LIT:\\>" , u"<STR_LIT>" ) <EOL> if x . count ( "<STR_LIT>" ) : <EOL> x = x . replace ( u"<STR_LIT>" , u"<STR_LIT>" ) <EOL> if add_quotes : <EOL> x = u'<STR_LIT:">' + x + u'<STR_LIT:">' <EOL> return x <EOL> def adapt ( x ) : <EOL> if isnode ( x ) : <EOL> x = u"<STR_LIT>" . format ( x ) <EOL> return x <EOL> if isinstance ( x , basestring ) : <EOL> assert_valid_encoding ( x ) <EOL> x = escape ( x ) <EOL> if x . startswith ( u"<STR_LIT>" ) : <EOL> return x <EOL> return u'<STR_LIT>' . format ( x ) <EOL> return unicode ( x ) <EOL> def expression_to_dot ( e ) : <EOL> d = { u"<STR_LIT>" : dot_type , <EOL> HasKeyword . relation : dot_keyword , <EOL> IsRelatedTo : lambda x , y : dot_arc ( x , u"<STR_LIT>" , y ) } <EOL> s = u"<STR_LIT>" <EOL> xs = [ ] <EOL> for node in e . iter_nodes ( ) : <EOL> for relation , other in e . iter_edges ( node ) : <EOL> node1 = adapt ( node ) <EOL> node2 = adapt ( other ) <EOL> relation = escape ( relation , add_quotes = False ) <EOL> if relation in d : <EOL> x = d [ relation ] ( node1 , node2 ) <EOL> else : <EOL> x = dot_arc ( node1 , relation , node2 ) <EOL> xs . append ( x ) <EOL> return None , s . format ( adapt ( e . head ) , u"<STR_LIT>" . join ( xs ) ) <EOL> def dot_arc ( a , label , b ) : <EOL> assert u"<STR_LIT:U+0020>" not in a and u"<STR_LIT:U+0020>" not in b <EOL> assert u"<STR_LIT:\n>" not in a + label + b <EOL> return u"<STR_LIT>" . format ( a , b , label ) <EOL> def dot_type ( a , t ) : <EOL> s = u"<STR_LIT>" . format ( t ) <EOL> return s + u"<STR_LIT>" . format ( a , t ) <EOL> def dot_attribute ( a , key ) : <EOL> blank = id ( a ) <EOL> s = u"<STR_LIT>" . format ( blank , key ) <EOL> return s + u"<STR_LIT>" . format ( a , blank ) <EOL> def dot_keyword ( a , key ) : <EOL> blank = u"<STR_LIT>" . format ( random . random ( ) ) <EOL> blank = u"<STR_LIT:blank>" + blank . replace ( u"<STR_LIT:.>" , u"<STR_LIT>" ) <EOL> s = u"<STR_LIT>" . format ( blank , key ) <EOL> return s + u"<STR_LIT>" . format ( a , blank ) <EOL> def dot_fixed_type ( a , fixedtype ) : <EOL> blank = u"<STR_LIT>" . format ( random . random ( ) ) <EOL> blank = u"<STR_LIT:blank>" + blank . replace ( u"<STR_LIT:.>" , u"<STR_LIT>" ) <EOL> s = u"<STR_LIT>" . format ( blank , fixedtype ) <EOL> return s + u"<STR_LIT>" . format ( a , blank ) </s>
