<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> __author__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' % int ( __revision__ [ <NUM_LIT> : - <NUM_LIT:2> ] ) <EOL> __license__ = '<STR_LIT>' <EOL> import os <EOL> import re <EOL> import subprocess <EOL> import tempfile <EOL> import copy <EOL> try : <EOL> import dot_parser <EOL> except Exception , e : <EOL> print "<STR_LIT>" <EOL> GRAPH_ATTRIBUTES = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT:label>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:root>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:size>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:start>' , <EOL> '<STR_LIT>' , '<STR_LIT:target>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> EDGE_ATTRIBUTES = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT:label>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:target>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> NODE_ATTRIBUTES = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT:image>' , '<STR_LIT>' , '<STR_LIT:label>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:root>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:target>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:width>' , '<STR_LIT:z>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> CLUSTER_ATTRIBUTES = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:target>' , '<STR_LIT>' ] ) <EOL> class frozendict ( dict ) : <EOL> def _blocked_attribute ( obj ) : <EOL> raise AttributeError , "<STR_LIT>" <EOL> _blocked_attribute = property ( _blocked_attribute ) <EOL> __delitem__ = __setitem__ = clear = _blocked_attribute <EOL> pop = popitem = setdefault = update = _blocked_attribute <EOL> def __new__ ( cls , * args , ** kw ) : <EOL> new = dict . __new__ ( cls ) <EOL> args_ = [ ] <EOL> for arg in args : <EOL> if isinstance ( arg , dict ) : <EOL> arg = copy . copy ( arg ) <EOL> for k , v in arg . iteritems ( ) : <EOL> if isinstance ( v , frozendict ) : <EOL> arg [ k ] = v <EOL> elif isinstance ( v , dict ) : <EOL> arg [ k ] = frozendict ( v ) <EOL> elif isinstance ( v , list ) : <EOL> v_ = list ( ) <EOL> for elm in v : <EOL> if isinstance ( elm , dict ) : <EOL> v_ . append ( frozendict ( elm ) ) <EOL> else : <EOL> v_ . append ( elm ) <EOL> arg [ k ] = tuple ( v_ ) <EOL> args_ . append ( arg ) <EOL> else : <EOL> args_ . append ( arg ) <EOL> dict . __init__ ( new , * args_ , ** kw ) <EOL> return new <EOL> def __init__ ( self , * args , ** kw ) : <EOL> pass <EOL> def __hash__ ( self ) : <EOL> try : <EOL> return self . _cached_hash <EOL> except AttributeError : <EOL> h = self . _cached_hash = hash ( tuple ( sorted ( self . iteritems ( ) ) ) ) <EOL> return h <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % dict . __repr__ ( self ) <EOL> dot_keywords = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:strict>' ] <EOL> id_re_alpha_nums = re . compile ( '<STR_LIT>' , re . UNICODE ) <EOL> id_re_alpha_nums_with_ports = re . compile ( '<STR_LIT>' , re . UNICODE ) <EOL> id_re_num = re . compile ( '<STR_LIT>' , re . UNICODE ) <EOL> id_re_with_port = re . compile ( '<STR_LIT>' , re . UNICODE ) <EOL> id_re_dbl_quoted = re . compile ( '<STR_LIT>' , re . S | re . UNICODE ) <EOL> id_re_html = re . compile ( '<STR_LIT>' , re . S | re . UNICODE ) <EOL> def needs_quotes ( s ) : <EOL> """<STR_LIT>""" <EOL> if s in dot_keywords : <EOL> return False <EOL> chars = [ ord ( c ) for c in s if ord ( c ) > <NUM_LIT> or ord ( c ) == <NUM_LIT:0> ] <EOL> if chars and not id_re_dbl_quoted . match ( s ) and not id_re_html . match ( s ) : <EOL> return True <EOL> for test_re in [ id_re_alpha_nums , id_re_num , id_re_dbl_quoted , id_re_html , id_re_alpha_nums_with_ports ] : <EOL> if test_re . match ( s ) : <EOL> return False <EOL> m = id_re_with_port . match ( s ) <EOL> if m : <EOL> return needs_quotes ( m . group ( <NUM_LIT:1> ) ) or needs_quotes ( m . group ( <NUM_LIT:2> ) ) <EOL> return True <EOL> def quote_if_necessary ( s ) : <EOL> if isinstance ( s , bool ) : <EOL> if s is True : <EOL> return '<STR_LIT:True>' <EOL> return '<STR_LIT:False>' <EOL> if not isinstance ( s , basestring ) : <EOL> return s <EOL> if not s : <EOL> return s <EOL> if needs_quotes ( s ) : <EOL> replace = { '<STR_LIT:">' : r'<STR_LIT>' , <EOL> "<STR_LIT:\n>" : r'<STR_LIT:\n>' , <EOL> "<STR_LIT:\r>" : r'<STR_LIT:\r>' } <EOL> for ( a , b ) in replace . items ( ) : <EOL> s = s . replace ( a , b ) <EOL> return '<STR_LIT:">' + s + '<STR_LIT:">' <EOL> return s <EOL> def graph_from_dot_data ( data ) : <EOL> """<STR_LIT>""" <EOL> return dot_parser . parse_dot_data ( data ) <EOL> def graph_from_dot_file ( path ) : <EOL> """<STR_LIT>""" <EOL> fd = file ( path , '<STR_LIT:rb>' ) <EOL> data = fd . read ( ) <EOL> fd . close ( ) <EOL> return graph_from_dot_data ( data ) <EOL> def graph_from_edges ( edge_list , node_prefix = '<STR_LIT>' , directed = False ) : <EOL> """<STR_LIT>""" <EOL> if directed : <EOL> graph = Dot ( graph_type = '<STR_LIT>' ) <EOL> else : <EOL> graph = Dot ( graph_type = '<STR_LIT>' ) <EOL> for edge in edge_list : <EOL> if isinstance ( edge [ <NUM_LIT:0> ] , str ) : <EOL> src = node_prefix + edge [ <NUM_LIT:0> ] <EOL> else : <EOL> src = node_prefix + str ( edge [ <NUM_LIT:0> ] ) <EOL> if isinstance ( edge [ <NUM_LIT:1> ] , str ) : <EOL> dst = node_prefix + edge [ <NUM_LIT:1> ] <EOL> else : <EOL> dst = node_prefix + str ( edge [ <NUM_LIT:1> ] ) <EOL> e = Edge ( src , dst ) <EOL> graph . add_edge ( e ) <EOL> return graph <EOL> def graph_from_adjacency_matrix ( matrix , node_prefix = u'<STR_LIT>' , directed = False ) : <EOL> """<STR_LIT>""" <EOL> node_orig = <NUM_LIT:1> <EOL> if directed : <EOL> graph = Dot ( graph_type = '<STR_LIT>' ) <EOL> else : <EOL> graph = Dot ( graph_type = '<STR_LIT>' ) <EOL> for row in matrix : <EOL> if not directed : <EOL> skip = matrix . index ( row ) <EOL> r = row [ skip : ] <EOL> else : <EOL> skip = <NUM_LIT:0> <EOL> r = row <EOL> node_dest = skip + <NUM_LIT:1> <EOL> for e in r : <EOL> if e : <EOL> graph . add_edge ( <EOL> Edge ( node_prefix + node_orig , <EOL> node_prefix + node_dest ) ) <EOL> node_dest += <NUM_LIT:1> <EOL> node_orig += <NUM_LIT:1> <EOL> return graph <EOL> def graph_from_incidence_matrix ( matrix , node_prefix = '<STR_LIT>' , directed = False ) : <EOL> """<STR_LIT>""" <EOL> node_orig = <NUM_LIT:1> <EOL> if directed : <EOL> graph = Dot ( graph_type = '<STR_LIT>' ) <EOL> else : <EOL> graph = Dot ( graph_type = '<STR_LIT>' ) <EOL> for row in matrix : <EOL> nodes = [ ] <EOL> c = <NUM_LIT:1> <EOL> for node in row : <EOL> if node : <EOL> nodes . append ( c * node ) <EOL> c += <NUM_LIT:1> <EOL> nodes . sort ( ) <EOL> if len ( nodes ) == <NUM_LIT:2> : <EOL> graph . add_edge ( <EOL> Edge ( node_prefix + abs ( nodes [ <NUM_LIT:0> ] ) , <EOL> node_prefix + nodes [ <NUM_LIT:1> ] ) ) <EOL> if not directed : <EOL> graph . set_simplify ( True ) <EOL> return graph <EOL> def __find_executables ( path ) : <EOL> """<STR_LIT>""" <EOL> success = False <EOL> progs = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> was_quoted = False <EOL> path = path . strip ( ) <EOL> if path . startswith ( '<STR_LIT:">' ) and path . endswith ( '<STR_LIT:">' ) : <EOL> path = path [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> was_quoted = True <EOL> if os . path . isdir ( path ) : <EOL> for prg in progs . iterkeys ( ) : <EOL> if progs [ prg ] : <EOL> continue <EOL> if os . path . exists ( os . path . join ( path , prg ) ) : <EOL> if was_quoted : <EOL> progs [ prg ] = '<STR_LIT:">' + os . path . join ( path , prg ) + '<STR_LIT:">' <EOL> else : <EOL> progs [ prg ] = os . path . join ( path , prg ) <EOL> success = True <EOL> elif os . path . exists ( os . path . join ( path , prg + '<STR_LIT>' ) ) : <EOL> if was_quoted : <EOL> progs [ prg ] = '<STR_LIT:">' + os . path . join ( path , prg + '<STR_LIT>' ) + '<STR_LIT:">' <EOL> else : <EOL> progs [ prg ] = os . path . join ( path , prg + '<STR_LIT>' ) <EOL> success = True <EOL> if success : <EOL> return progs <EOL> else : <EOL> return None <EOL> def find_graphviz ( ) : <EOL> """<STR_LIT>""" <EOL> if os . sys . platform == '<STR_LIT:win32>' : <EOL> HKEY_LOCAL_MACHINE = <NUM_LIT> <EOL> KEY_QUERY_VALUE = <NUM_LIT> <EOL> RegOpenKeyEx = None <EOL> RegQueryValueEx = None <EOL> RegCloseKey = None <EOL> try : <EOL> import win32api , win32con <EOL> RegOpenKeyEx = win32api . RegOpenKeyEx <EOL> RegQueryValueEx = win32api . RegQueryValueEx <EOL> RegCloseKey = win32api . RegCloseKey <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> import ctypes <EOL> def RegOpenKeyEx ( key , subkey , opt , sam ) : <EOL> result = ctypes . c_uint ( <NUM_LIT:0> ) <EOL> ctypes . windll . advapi32 . RegOpenKeyExA ( key , subkey , opt , sam , ctypes . byref ( result ) ) <EOL> return result . value <EOL> def RegQueryValueEx ( hkey , valuename ) : <EOL> data_type = ctypes . c_uint ( <NUM_LIT:0> ) <EOL> data_len = ctypes . c_uint ( <NUM_LIT> ) <EOL> data = ctypes . create_string_buffer ( <NUM_LIT> ) <EOL> res = ctypes . windll . advapi32 . RegQueryValueExA ( hkey , valuename , <NUM_LIT:0> , <EOL> ctypes . byref ( data_type ) , data , ctypes . byref ( data_len ) ) <EOL> return data . value <EOL> RegCloseKey = ctypes . windll . advapi32 . RegCloseKey <EOL> except ImportError : <EOL> pass <EOL> if RegOpenKeyEx is not None : <EOL> hkey = None <EOL> potentialKeys = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> for potentialKey in potentialKeys : <EOL> try : <EOL> hkey = RegOpenKeyEx ( HKEY_LOCAL_MACHINE , <EOL> potentialKey , <NUM_LIT:0> , KEY_QUERY_VALUE ) <EOL> if hkey is not None : <EOL> path = RegQueryValueEx ( hkey , "<STR_LIT>" ) <EOL> RegCloseKey ( hkey ) <EOL> if not path : <EOL> continue <EOL> path = os . path . join ( path , "<STR_LIT>" ) <EOL> progs = __find_executables ( path ) <EOL> if progs is not None : <EOL> return progs <EOL> except Exception , excp : <EOL> pass <EOL> else : <EOL> break <EOL> if os . environ . has_key ( '<STR_LIT>' ) : <EOL> for path in os . environ [ '<STR_LIT>' ] . split ( os . pathsep ) : <EOL> progs = __find_executables ( path ) <EOL> if progs is not None : <EOL> return progs <EOL> if os . sys . platform == '<STR_LIT:win32>' : <EOL> if os . environ . has_key ( '<STR_LIT>' ) : <EOL> path = os . path . join ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> path = r"<STR_LIT>" <EOL> progs = __find_executables ( path ) <EOL> if progs is not None : <EOL> return progs <EOL> for path in ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) : <EOL> progs = __find_executables ( path ) <EOL> if progs is not None : <EOL> return progs <EOL> return None <EOL> class Common : <EOL> """<STR_LIT>""" <EOL> def __getstate__ ( self ) : <EOL> dict = copy . copy ( self . obj_dict ) <EOL> return dict <EOL> def __setstate__ ( self , state ) : <EOL> self . obj_dict = state <EOL> def __get_attribute__ ( self , attr ) : <EOL> """<STR_LIT>""" <EOL> attr_val = self . obj_dict [ '<STR_LIT>' ] . get ( attr , None ) <EOL> if attr_val is None : <EOL> default_node_name = self . obj_dict [ '<STR_LIT:type>' ] <EOL> if default_node_name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> default_node_name = '<STR_LIT>' <EOL> g = self . get_parent_graph ( ) <EOL> if g is not None : <EOL> defaults = g . get_node ( default_node_name ) <EOL> else : <EOL> return None <EOL> if not isinstance ( defaults , ( list , tuple ) ) : <EOL> defaults = [ defaults ] <EOL> for default in defaults : <EOL> attr_val = default . obj_dict [ '<STR_LIT>' ] . get ( attr , None ) <EOL> if attr_val : <EOL> return attr_val <EOL> else : <EOL> return attr_val <EOL> return None <EOL> def set_parent_graph ( self , parent_graph ) : <EOL> self . obj_dict [ '<STR_LIT>' ] = parent_graph <EOL> def get_parent_graph ( self ) : <EOL> return self . obj_dict . get ( '<STR_LIT>' , None ) <EOL> def set ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> self . obj_dict [ '<STR_LIT>' ] [ name ] = value <EOL> def get ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT>' ] . get ( name , None ) <EOL> def get_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT>' ] <EOL> def set_sequence ( self , seq ) : <EOL> self . obj_dict [ '<STR_LIT>' ] = seq <EOL> def get_sequence ( self ) : <EOL> return self . obj_dict [ '<STR_LIT>' ] <EOL> def create_attribute_methods ( self , obj_attributes ) : <EOL> for attr in obj_attributes : <EOL> self . __setattr__ ( '<STR_LIT>' + attr , lambda x , a = attr : self . obj_dict [ '<STR_LIT>' ] . __setitem__ ( a , x ) ) <EOL> self . __setattr__ ( '<STR_LIT>' + attr , lambda a = attr : self . __get_attribute__ ( a ) ) <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return self . value <EOL> class InvocationException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return self . value <EOL> class Node ( object , Common ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = '<STR_LIT>' , obj_dict = None , ** attrs ) : <EOL> if obj_dict is not None : <EOL> self . obj_dict = obj_dict <EOL> else : <EOL> self . obj_dict = dict ( ) <EOL> self . obj_dict [ '<STR_LIT>' ] = dict ( attrs ) <EOL> self . obj_dict [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> self . obj_dict [ '<STR_LIT>' ] = None <EOL> self . obj_dict [ '<STR_LIT>' ] = None <EOL> self . obj_dict [ '<STR_LIT>' ] = None <EOL> port = None <EOL> if isinstance ( name , basestring ) and not name . startswith ( '<STR_LIT:">' ) : <EOL> idx = name . find ( '<STR_LIT::>' ) <EOL> if idx > <NUM_LIT:0> and idx + <NUM_LIT:1> < len ( name ) : <EOL> name , port = name [ : idx ] , name [ idx : ] <EOL> if isinstance ( name , ( long , int ) ) : <EOL> name = str ( name ) <EOL> self . obj_dict [ '<STR_LIT:name>' ] = quote_if_necessary ( name ) <EOL> self . obj_dict [ '<STR_LIT:port>' ] = port <EOL> self . create_attribute_methods ( NODE_ATTRIBUTES ) <EOL> def set_name ( self , node_name ) : <EOL> """<STR_LIT>""" <EOL> self . obj_dict [ '<STR_LIT:name>' ] = node_name <EOL> def get_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT:name>' ] <EOL> def get_port ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT:port>' ] <EOL> def add_style ( self , style ) : <EOL> styles = self . obj_dict [ '<STR_LIT>' ] . get ( '<STR_LIT>' , None ) <EOL> if not styles and style : <EOL> styles = [ style ] <EOL> else : <EOL> styles = styles . split ( '<STR_LIT:U+002C>' ) <EOL> styles . append ( style ) <EOL> self . obj_dict [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT:U+002C>' . join ( styles ) <EOL> def to_string ( self ) : <EOL> """<STR_LIT>""" <EOL> node = quote_if_necessary ( self . obj_dict [ '<STR_LIT:name>' ] ) <EOL> node_attr = list ( ) <EOL> for attr , value in self . obj_dict [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if value is not None : <EOL> node_attr . append ( '<STR_LIT>' % ( attr , quote_if_necessary ( value ) ) ) <EOL> else : <EOL> node_attr . append ( attr ) <EOL> if node in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) and len ( node_attr ) == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> node_attr = '<STR_LIT:U+002CU+0020>' . join ( node_attr ) <EOL> if node_attr : <EOL> node += '<STR_LIT>' + node_attr + '<STR_LIT:]>' <EOL> return node + '<STR_LIT:;>' <EOL> class Edge ( object , Common ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , src = '<STR_LIT>' , dst = '<STR_LIT>' , obj_dict = None , ** attrs ) : <EOL> if isinstance ( src , ( list , tuple ) ) and dst == '<STR_LIT>' : <EOL> src , dst = src <EOL> if obj_dict is not None : <EOL> self . obj_dict = obj_dict <EOL> else : <EOL> self . obj_dict = dict ( ) <EOL> self . obj_dict [ '<STR_LIT>' ] = dict ( attrs ) <EOL> self . obj_dict [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> self . obj_dict [ '<STR_LIT>' ] = None <EOL> self . obj_dict [ '<STR_LIT>' ] = None <EOL> self . obj_dict [ '<STR_LIT>' ] = None <EOL> if isinstance ( src , Node ) : <EOL> src = src . get_name ( ) <EOL> if isinstance ( dst , Node ) : <EOL> dst = dst . get_name ( ) <EOL> points = ( quote_if_necessary ( src ) , quote_if_necessary ( dst ) ) <EOL> self . obj_dict [ '<STR_LIT>' ] = points <EOL> self . create_attribute_methods ( EDGE_ATTRIBUTES ) <EOL> def get_source ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> def get_destination ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> def __hash__ ( self ) : <EOL> return hash ( hash ( self . get_source ( ) ) + hash ( self . get_destination ( ) ) ) <EOL> def __eq__ ( self , edge ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( edge , Edge ) : <EOL> raise Error , "<STR_LIT>" <EOL> if self . get_parent_graph ( ) . get_top_graph_type ( ) == '<STR_LIT>' : <EOL> if ( ( self . get_source ( ) == edge . get_source ( ) and self . get_destination ( ) == edge . get_destination ( ) ) or <EOL> ( edge . get_source ( ) == self . get_destination ( ) and edge . get_destination ( ) == self . get_source ( ) ) ) : <EOL> return True <EOL> else : <EOL> if self . get_source ( ) == edge . get_source ( ) and self . get_destination ( ) == edge . get_destination ( ) : <EOL> return True <EOL> return False <EOL> def parse_node_ref ( self , node_str ) : <EOL> if not isinstance ( node_str , str ) : <EOL> return node_str <EOL> if node_str . startswith ( '<STR_LIT:">' ) and node_str . endswith ( '<STR_LIT:">' ) : <EOL> return node_str <EOL> node_port_idx = node_str . rfind ( '<STR_LIT::>' ) <EOL> if node_port_idx > <NUM_LIT:0> and node_str [ <NUM_LIT:0> ] == '<STR_LIT:">' and node_str [ node_port_idx - <NUM_LIT:1> ] == '<STR_LIT:">' : <EOL> return node_str <EOL> if node_port_idx > <NUM_LIT:0> : <EOL> a = node_str [ : node_port_idx ] <EOL> b = node_str [ node_port_idx + <NUM_LIT:1> : ] <EOL> node = quote_if_necessary ( a ) <EOL> node += '<STR_LIT::>' + quote_if_necessary ( b ) <EOL> return node <EOL> return node_str <EOL> def to_string ( self ) : <EOL> """<STR_LIT>""" <EOL> src = self . parse_node_ref ( self . get_source ( ) ) <EOL> dst = self . parse_node_ref ( self . get_destination ( ) ) <EOL> if isinstance ( src , frozendict ) : <EOL> edge = [ Subgraph ( obj_dict = src ) . to_string ( ) ] <EOL> elif isinstance ( src , ( int , long ) ) : <EOL> edge = [ str ( src ) ] <EOL> else : <EOL> edge = [ src ] <EOL> if ( self . get_parent_graph ( ) and <EOL> self . get_parent_graph ( ) . get_top_graph_type ( ) and <EOL> self . get_parent_graph ( ) . get_top_graph_type ( ) == '<STR_LIT>' ) : <EOL> edge . append ( '<STR_LIT>' ) <EOL> else : <EOL> edge . append ( '<STR_LIT>' ) <EOL> if isinstance ( dst , frozendict ) : <EOL> edge . append ( Subgraph ( obj_dict = dst ) . to_string ( ) ) <EOL> elif isinstance ( dst , ( int , long ) ) : <EOL> edge . append ( str ( dst ) ) <EOL> else : <EOL> edge . append ( dst ) <EOL> edge_attr = list ( ) <EOL> for attr , value in self . obj_dict [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if value is not None : <EOL> edge_attr . append ( '<STR_LIT>' % ( attr , quote_if_necessary ( value ) ) ) <EOL> else : <EOL> edge_attr . append ( attr ) <EOL> edge_attr = '<STR_LIT:U+002CU+0020>' . join ( edge_attr ) <EOL> if edge_attr : <EOL> edge . append ( '<STR_LIT>' + edge_attr + '<STR_LIT:]>' ) <EOL> return '<STR_LIT:U+0020>' . join ( edge ) + '<STR_LIT:;>' <EOL> class Graph ( object , Common ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , graph_name = '<STR_LIT>' , obj_dict = None , graph_type = '<STR_LIT>' , strict = False , <EOL> suppress_disconnected = False , simplify = False , ** attrs ) : <EOL> if obj_dict is not None : <EOL> self . obj_dict = obj_dict <EOL> else : <EOL> self . obj_dict = dict ( ) <EOL> self . obj_dict [ '<STR_LIT>' ] = dict ( attrs ) <EOL> if graph_type not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise Error , '<STR_LIT>' % graph_type <EOL> self . obj_dict [ '<STR_LIT:name>' ] = quote_if_necessary ( graph_name ) <EOL> self . obj_dict [ '<STR_LIT:type>' ] = graph_type <EOL> self . obj_dict [ '<STR_LIT:strict>' ] = strict <EOL> self . obj_dict [ '<STR_LIT>' ] = suppress_disconnected <EOL> self . obj_dict [ '<STR_LIT>' ] = simplify <EOL> self . obj_dict [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> self . obj_dict [ '<STR_LIT>' ] = dict ( ) <EOL> self . obj_dict [ '<STR_LIT>' ] = dict ( ) <EOL> self . obj_dict [ '<STR_LIT>' ] = dict ( ) <EOL> self . set_parent_graph ( self ) <EOL> self . create_attribute_methods ( GRAPH_ATTRIBUTES ) <EOL> def get_graph_type ( self ) : <EOL> return self . obj_dict [ '<STR_LIT:type>' ] <EOL> def get_top_graph_type ( self ) : <EOL> parent = self <EOL> while True : <EOL> parent_ = parent . get_parent_graph ( ) <EOL> if parent_ == parent : <EOL> break <EOL> parent = parent_ <EOL> return parent . obj_dict [ '<STR_LIT:type>' ] <EOL> def set_graph_defaults ( self , ** attrs ) : <EOL> self . add_node ( Node ( '<STR_LIT>' , ** attrs ) ) <EOL> def get_graph_defaults ( self , ** attrs ) : <EOL> graph_nodes = self . get_node ( '<STR_LIT>' ) <EOL> if isinstance ( graph_nodes , ( list , tuple ) ) : <EOL> return [ node . get_attributes ( ) for node in graph_nodes ] <EOL> return graph_nodes . get_attributes ( ) <EOL> def set_node_defaults ( self , ** attrs ) : <EOL> self . add_node ( Node ( '<STR_LIT>' , ** attrs ) ) <EOL> def get_node_defaults ( self , ** attrs ) : <EOL> graph_nodes = self . get_node ( '<STR_LIT>' ) <EOL> if isinstance ( graph_nodes , ( list , tuple ) ) : <EOL> return [ node . get_attributes ( ) for node in graph_nodes ] <EOL> return graph_nodes . get_attributes ( ) <EOL> def set_edge_defaults ( self , ** attrs ) : <EOL> self . add_node ( Node ( '<STR_LIT>' , ** attrs ) ) <EOL> def get_edge_defaults ( self , ** attrs ) : <EOL> graph_nodes = self . get_node ( '<STR_LIT>' ) <EOL> if isinstance ( graph_nodes , ( list , tuple ) ) : <EOL> return [ node . get_attributes ( ) for node in graph_nodes ] <EOL> return graph_nodes . get_attributes ( ) <EOL> def set_simplify ( self , simplify ) : <EOL> """<STR_LIT>""" <EOL> self . obj_dict [ '<STR_LIT>' ] = simplify <EOL> def get_simplify ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT>' ] <EOL> def set_type ( self , graph_type ) : <EOL> """<STR_LIT>""" <EOL> self . obj_dict [ '<STR_LIT:type>' ] = graph_type <EOL> def get_type ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT:type>' ] <EOL> def set_name ( self , graph_name ) : <EOL> """<STR_LIT>""" <EOL> self . obj_dict [ '<STR_LIT:name>' ] = graph_name <EOL> def get_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT:name>' ] <EOL> def set_strict ( self , val ) : <EOL> """<STR_LIT>""" <EOL> self . obj_dict [ '<STR_LIT:strict>' ] = val <EOL> def get_strict ( self , val ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT:strict>' ] <EOL> def set_suppress_disconnected ( self , val ) : <EOL> """<STR_LIT>""" <EOL> self . obj_dict [ '<STR_LIT>' ] = val <EOL> def get_suppress_disconnected ( self , val ) : <EOL> """<STR_LIT>""" <EOL> return self . obj_dict [ '<STR_LIT>' ] <EOL> def get_next_sequence_number ( self ) : <EOL> seq = self . obj_dict [ '<STR_LIT>' ] <EOL> self . obj_dict [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> return seq <EOL> def add_node ( self , graph_node ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( graph_node , Node ) : <EOL> raise TypeError ( '<STR_LIT>' + str ( graph_node ) ) <EOL> node = self . get_node ( graph_node . get_name ( ) ) <EOL> if not node : <EOL> self . obj_dict [ '<STR_LIT>' ] [ graph_node . get_name ( ) ] = [ graph_node . obj_dict ] <EOL> graph_node . set_parent_graph ( self . get_parent_graph ( ) ) <EOL> else : <EOL> self . obj_dict [ '<STR_LIT>' ] [ graph_node . get_name ( ) ] . append ( graph_node . obj_dict ) <EOL> graph_node . set_sequence ( self . get_next_sequence_number ( ) ) <EOL> def del_node ( self , name , index = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( name , Node ) : <EOL> name = name . get_name ( ) <EOL> if self . obj_dict [ '<STR_LIT>' ] . has_key ( name ) : <EOL> if index is not None and index < len ( self . obj_dict [ '<STR_LIT>' ] [ name ] ) : <EOL> del self . obj_dict [ '<STR_LIT>' ] [ name ] [ index ] <EOL> return True <EOL> else : <EOL> del self . obj_dict [ '<STR_LIT>' ] [ name ] <EOL> return True <EOL> return False <EOL> def get_node ( self , name ) : <EOL> """<STR_LIT>""" <EOL> match = list ( ) <EOL> if self . obj_dict [ '<STR_LIT>' ] . has_key ( name ) : <EOL> match . extend ( [ Node ( obj_dict = obj_dict ) for obj_dict in self . obj_dict [ '<STR_LIT>' ] [ name ] ] ) <EOL> return match <EOL> def get_nodes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_node_list ( ) <EOL> def get_node_list ( self ) : <EOL> """<STR_LIT>""" <EOL> node_objs = list ( ) <EOL> for node , obj_dict_list in self . obj_dict [ '<STR_LIT>' ] . iteritems ( ) : <EOL> node_objs . extend ( [ Node ( obj_dict = obj_d ) for obj_d in obj_dict_list ] ) <EOL> return node_objs <EOL> def add_edge ( self , graph_edge ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( graph_edge , Edge ) : <EOL> raise TypeError ( '<STR_LIT>' + str ( graph_edge ) ) <EOL> edge_points = ( graph_edge . get_source ( ) , graph_edge . get_destination ( ) ) <EOL> if self . obj_dict [ '<STR_LIT>' ] . has_key ( edge_points ) : <EOL> edge_list = self . obj_dict [ '<STR_LIT>' ] [ edge_points ] <EOL> edge_list . append ( graph_edge . obj_dict ) <EOL> else : <EOL> self . obj_dict [ '<STR_LIT>' ] [ edge_points ] = [ graph_edge . obj_dict ] <EOL> graph_edge . set_sequence ( self . get_next_sequence_number ( ) ) <EOL> graph_edge . set_parent_graph ( self . get_parent_graph ( ) ) <EOL> def del_edge ( self , src_or_list , dst = None , index = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( src_or_list , ( list , tuple ) ) : <EOL> if dst is not None and isinstance ( dst , ( int , long ) ) : <EOL> index = dst <EOL> src , dst = src_or_list <EOL> else : <EOL> src , dst = src_or_list , dst <EOL> if isinstance ( src , Node ) : <EOL> src = src . get_name ( ) <EOL> if isinstance ( dst , Node ) : <EOL> dst = dst . get_name ( ) <EOL> if self . obj_dict [ '<STR_LIT>' ] . has_key ( ( src , dst ) ) : <EOL> if index is not None and index < len ( self . obj_dict [ '<STR_LIT>' ] [ ( src , dst ) ] ) : <EOL> del self . obj_dict [ '<STR_LIT>' ] [ ( src , dst ) ] [ index ] <EOL> return True <EOL> else : <EOL> del self . obj_dict [ '<STR_LIT>' ] [ ( src , dst ) ] <EOL> return True <EOL> return False <EOL> def get_edge ( self , src_or_list , dst = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( src_or_list , ( list , tuple ) ) and dst is None : <EOL> edge_points = tuple ( src_or_list ) <EOL> edge_points_reverse = ( edge_points [ <NUM_LIT:1> ] , edge_points [ <NUM_LIT:0> ] ) <EOL> else : <EOL> edge_points = ( src_or_list , dst ) <EOL> edge_points_reverse = ( dst , src_or_list ) <EOL> match = list ( ) <EOL> if self . obj_dict [ '<STR_LIT>' ] . has_key ( edge_points ) or ( <EOL> self . get_top_graph_type ( ) == '<STR_LIT>' and self . obj_dict [ '<STR_LIT>' ] . has_key ( edge_points_reverse ) ) : <EOL> edges_obj_dict = self . obj_dict [ '<STR_LIT>' ] . get ( <EOL> edge_points , <EOL> self . obj_dict [ '<STR_LIT>' ] . get ( edge_points_reverse , None ) ) <EOL> for edge_obj_dict in edges_obj_dict : <EOL> match . append ( Edge ( edge_points [ <NUM_LIT:0> ] , edge_points [ <NUM_LIT:1> ] , obj_dict = edge_obj_dict ) ) <EOL> return match <EOL> def get_edges ( self ) : <EOL> return self . get_edge_list ( ) <EOL> def get_edge_list ( self ) : <EOL> """<STR_LIT>""" <EOL> edge_objs = list ( ) <EOL> for edge , obj_dict_list in self . obj_dict [ '<STR_LIT>' ] . iteritems ( ) : <EOL> edge_objs . extend ( [ Edge ( obj_dict = obj_d ) for obj_d in obj_dict_list ] ) <EOL> return edge_objs <EOL> def add_subgraph ( self , sgraph ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( sgraph , Subgraph ) and not isinstance ( sgraph , Cluster ) : <EOL> raise TypeError ( '<STR_LIT>' + str ( sgraph ) ) <EOL> if self . obj_dict [ '<STR_LIT>' ] . has_key ( sgraph . get_name ( ) ) : <EOL> sgraph_list = self . obj_dict [ '<STR_LIT>' ] [ sgraph . get_name ( ) ] <EOL> sgraph_list . append ( sgraph . obj_dict ) <EOL> else : <EOL> self . obj_dict [ '<STR_LIT>' ] [ sgraph . get_name ( ) ] = [ sgraph . obj_dict ] <EOL> sgraph . set_sequence ( self . get_next_sequence_number ( ) ) <EOL> sgraph . set_parent_graph ( self . get_parent_graph ( ) ) <EOL> def get_subgraph ( self , name ) : <EOL> """<STR_LIT>""" <EOL> match = list ( ) <EOL> if self . obj_dict [ '<STR_LIT>' ] . has_key ( name ) : <EOL> sgraphs_obj_dict = self . obj_dict [ '<STR_LIT>' ] . get ( name ) <EOL> for obj_dict_list in sgraphs_obj_dict : <EOL> match . append ( Subgraph ( obj_dict = obj_dict_list ) ) <EOL> return match <EOL> def get_subgraphs ( self ) : <EOL> return self . get_subgraph_list ( ) <EOL> def get_subgraph_list ( self ) : <EOL> """<STR_LIT>""" <EOL> sgraph_objs = list ( ) <EOL> for sgraph , obj_dict_list in self . obj_dict [ '<STR_LIT>' ] . iteritems ( ) : <EOL> sgraph_objs . extend ( [ Subgraph ( obj_dict = obj_d ) for obj_d in obj_dict_list ] ) <EOL> return sgraph_objs <EOL> def set_parent_graph ( self , parent_graph ) : <EOL> self . obj_dict [ '<STR_LIT>' ] = parent_graph <EOL> for obj_list in self . obj_dict [ '<STR_LIT>' ] . itervalues ( ) : <EOL> for obj in obj_list : <EOL> obj [ '<STR_LIT>' ] = parent_graph <EOL> for obj_list in self . obj_dict [ '<STR_LIT>' ] . itervalues ( ) : <EOL> for obj in obj_list : <EOL> obj [ '<STR_LIT>' ] = parent_graph <EOL> for obj_list in self . obj_dict [ '<STR_LIT>' ] . itervalues ( ) : <EOL> for obj in obj_list : <EOL> Graph ( obj_dict = obj ) . set_parent_graph ( parent_graph ) <EOL> def to_string ( self ) : <EOL> """<STR_LIT>""" <EOL> graph = list ( ) <EOL> if self . obj_dict . get ( '<STR_LIT:strict>' , None ) is not None : <EOL> if self == self . get_parent_graph ( ) and self . obj_dict [ '<STR_LIT:strict>' ] : <EOL> graph . append ( '<STR_LIT>' ) <EOL> if self . obj_dict [ '<STR_LIT:name>' ] == '<STR_LIT>' : <EOL> if '<STR_LIT>' in self . obj_dict and self . obj_dict [ '<STR_LIT>' ] : <EOL> graph . append ( '<STR_LIT>' ) <EOL> else : <EOL> graph . append ( '<STR_LIT>' ) <EOL> else : <EOL> graph . append ( '<STR_LIT>' % ( self . obj_dict [ '<STR_LIT:type>' ] , self . obj_dict [ '<STR_LIT:name>' ] ) ) <EOL> for attr in self . obj_dict [ '<STR_LIT>' ] . iterkeys ( ) : <EOL> if self . obj_dict [ '<STR_LIT>' ] . get ( attr , None ) is not None : <EOL> val = self . obj_dict [ '<STR_LIT>' ] . get ( attr ) <EOL> if val is not None : <EOL> graph . append ( '<STR_LIT>' % ( attr , quote_if_necessary ( val ) ) ) <EOL> else : <EOL> graph . append ( attr ) <EOL> graph . append ( '<STR_LIT>' ) <EOL> edges_done = set ( ) <EOL> edge_obj_dicts = list ( ) <EOL> for e in self . obj_dict [ '<STR_LIT>' ] . itervalues ( ) : <EOL> edge_obj_dicts . extend ( e ) <EOL> if edge_obj_dicts : <EOL> edge_src_set , edge_dst_set = zip ( * [ obj [ '<STR_LIT>' ] for obj in edge_obj_dicts ] ) <EOL> edge_src_set , edge_dst_set = set ( edge_src_set ) , set ( edge_dst_set ) <EOL> else : <EOL> edge_src_set , edge_dst_set = set ( ) , set ( ) <EOL> node_obj_dicts = list ( ) <EOL> for e in self . obj_dict [ '<STR_LIT>' ] . itervalues ( ) : <EOL> node_obj_dicts . extend ( e ) <EOL> sgraph_obj_dicts = list ( ) <EOL> for sg in self . obj_dict [ '<STR_LIT>' ] . itervalues ( ) : <EOL> sgraph_obj_dicts . extend ( sg ) <EOL> obj_list = [ ( obj [ '<STR_LIT>' ] , obj ) for obj in ( edge_obj_dicts + node_obj_dicts + sgraph_obj_dicts ) ] <EOL> obj_list . sort ( ) <EOL> for idx , obj in obj_list : <EOL> if obj [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> node = Node ( obj_dict = obj ) <EOL> if self . obj_dict . get ( '<STR_LIT>' , False ) : <EOL> if ( node . get_name ( ) not in edge_src_set and <EOL> node . get_name ( ) not in edge_dst_set ) : <EOL> continue <EOL> graph . append ( node . to_string ( ) + '<STR_LIT:\n>' ) <EOL> elif obj [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> edge = Edge ( obj_dict = obj ) <EOL> if self . obj_dict . get ( '<STR_LIT>' , False ) and edge in edges_done : <EOL> continue <EOL> graph . append ( edge . to_string ( ) + '<STR_LIT:\n>' ) <EOL> edges_done . add ( edge ) <EOL> else : <EOL> sgraph = Subgraph ( obj_dict = obj ) <EOL> graph . append ( sgraph . to_string ( ) + '<STR_LIT:\n>' ) <EOL> graph . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' . join ( graph ) <EOL> class Subgraph ( Graph ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , graph_name = '<STR_LIT>' , obj_dict = None , suppress_disconnected = False , <EOL> simplify = False , ** attrs ) : <EOL> Graph . __init__ ( self , graph_name = graph_name , obj_dict = obj_dict , <EOL> suppress_disconnected = suppress_disconnected , simplify = simplify , ** attrs ) <EOL> if obj_dict is None : <EOL> self . obj_dict [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> class Cluster ( Graph ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , graph_name = '<STR_LIT>' , obj_dict = None , suppress_disconnected = False , <EOL> simplify = False , ** attrs ) : <EOL> Graph . __init__ ( self , graph_name = graph_name , obj_dict = obj_dict , <EOL> suppress_disconnected = suppress_disconnected , simplify = simplify , ** attrs ) <EOL> if obj_dict is None : <EOL> self . obj_dict [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> self . obj_dict [ '<STR_LIT:name>' ] = '<STR_LIT>' + graph_name <EOL> self . create_attribute_methods ( CLUSTER_ATTRIBUTES ) <EOL> class Dot ( Graph ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * argsl , ** argsd ) : <EOL> Graph . __init__ ( self , * argsl , ** argsd ) <EOL> self . shape_files = list ( ) <EOL> self . progs = None <EOL> self . formats = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . prog = '<STR_LIT>' <EOL> for frmt in self . formats : <EOL> self . __setattr__ ( <EOL> '<STR_LIT>' + frmt , <EOL> lambda f = frmt , prog = self . prog : self . create ( format = f , prog = prog ) ) <EOL> f = self . __dict__ [ '<STR_LIT>' + frmt ] <EOL> f . __doc__ = '''<STR_LIT>''' <EOL> for frmt in self . formats + [ '<STR_LIT>' ] : <EOL> self . __setattr__ ( <EOL> '<STR_LIT>' + frmt , <EOL> lambda path , f = frmt , prog = self . prog : self . write ( path , format = f , prog = prog ) ) <EOL> f = self . __dict__ [ '<STR_LIT>' + frmt ] <EOL> f . __doc__ = '''<STR_LIT>''' <EOL> def __getstate__ ( self ) : <EOL> dict = copy . copy ( self . obj_dict ) <EOL> return dict <EOL> def __setstate__ ( self , state ) : <EOL> self . obj_dict = state <EOL> def set_shape_files ( self , file_paths ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( file_paths , basestring ) : <EOL> self . shape_files . append ( file_paths ) <EOL> if isinstance ( file_paths , ( list , tuple ) ) : <EOL> self . shape_files . extend ( file_paths ) <EOL> def set_prog ( self , prog ) : <EOL> """<STR_LIT>""" <EOL> self . prog = prog <EOL> def set_graphviz_executables ( self , paths ) : <EOL> """<STR_LIT>""" <EOL> self . progs = paths <EOL> def write ( self , path , prog = None , format = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if prog is None : <EOL> prog = self . prog <EOL> dot_fd = file ( path , "<STR_LIT>" ) <EOL> if format == '<STR_LIT>' : <EOL> data = self . to_string ( ) <EOL> if isinstance ( data , basestring ) : <EOL> if not isinstance ( data , unicode ) : <EOL> try : <EOL> data = unicode ( data , '<STR_LIT:utf-8>' ) <EOL> except : <EOL> pass <EOL> try : <EOL> data = data . encode ( '<STR_LIT:utf-8>' ) <EOL> except : <EOL> pass <EOL> dot_fd . write ( data ) <EOL> else : <EOL> dot_fd . write ( self . create ( prog , format ) ) <EOL> dot_fd . close ( ) <EOL> return True <EOL> def create ( self , prog = None , format = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if prog is None : <EOL> prog = self . prog <EOL> if isinstance ( prog , ( list , tuple ) ) : <EOL> prog , args = prog [ <NUM_LIT:0> ] , prog [ <NUM_LIT:1> : ] <EOL> else : <EOL> args = [ ] <EOL> if self . progs is None : <EOL> self . progs = find_graphviz ( ) <EOL> if self . progs is None : <EOL> raise InvocationException ( <EOL> '<STR_LIT>' ) <EOL> if not self . progs . has_key ( prog ) : <EOL> raise InvocationException ( <EOL> '<STR_LIT>' % prog ) <EOL> if not os . path . exists ( self . progs [ prog ] ) or not os . path . isfile ( self . progs [ prog ] ) : <EOL> raise InvocationException ( <EOL> '<STR_LIT>' % self . progs [ prog ] ) <EOL> tmp_fd , tmp_name = tempfile . mkstemp ( ) <EOL> os . close ( tmp_fd ) <EOL> self . write ( tmp_name ) <EOL> tmp_dir = os . path . dirname ( tmp_name ) <EOL> for img in self . shape_files : <EOL> f = file ( img , '<STR_LIT:rb>' ) <EOL> f_data = f . read ( ) <EOL> f . close ( ) <EOL> f = file ( os . path . join ( tmp_dir , os . path . basename ( img ) ) , '<STR_LIT:wb>' ) <EOL> f . write ( f_data ) <EOL> f . close ( ) <EOL> cmdline = [ self . progs [ prog ] , '<STR_LIT>' + format , tmp_name ] + args <EOL> p = subprocess . Popen ( <EOL> cmdline , <EOL> cwd = tmp_dir , <EOL> stderr = subprocess . PIPE , stdout = subprocess . PIPE ) <EOL> stderr = p . stderr <EOL> stdout = p . stdout <EOL> stdout_output = list ( ) <EOL> while True : <EOL> data = stdout . read ( ) <EOL> if not data : <EOL> break <EOL> stdout_output . append ( data ) <EOL> stdout . close ( ) <EOL> stdout_output = '<STR_LIT>' . join ( stdout_output ) <EOL> if not stderr . closed : <EOL> stderr_output = list ( ) <EOL> while True : <EOL> data = stderr . read ( ) <EOL> if not data : <EOL> break <EOL> stderr_output . append ( data ) <EOL> stderr . close ( ) <EOL> if stderr_output : <EOL> stderr_output = '<STR_LIT>' . join ( stderr_output ) <EOL> status = p . wait ( ) <EOL> if status != <NUM_LIT:0> : <EOL> raise InvocationException ( <EOL> '<STR_LIT>' % ( <EOL> status , stderr_output ) ) <EOL> elif stderr_output : <EOL> print stderr_output <EOL> for img in self . shape_files : <EOL> os . unlink ( os . path . join ( tmp_dir , os . path . basename ( img ) ) ) <EOL> os . unlink ( tmp_name ) <EOL> return stdout_output </s>
