<s> """<STR_LIT>""" <EOL> from zen . exceptions import * <EOL> from zen . graph import Graph <EOL> from zen . digraph import DiGraph <EOL> from zen . bipartite import BipartiteGraph <EOL> from gml_codec import BasicGMLCodec , ZenGMLCodec <EOL> from gml_interpreter import GMLInterpreter <EOL> from gml_tokenizer import GMLTokenizer <EOL> from collections import Iterable , Hashable <EOL> import os <EOL> import cgi <EOL> import re <EOL> import codecs <EOL> import pdb <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> DIGITS = tuple ( [ '<STR_LIT>' % x for x in range ( <NUM_LIT:10> ) ] ) + ( '<STR_LIT:+>' , '<STR_LIT:->' ) <EOL> DIGITS_AND_QUOTES = DIGITS + ( '<STR_LIT:">' , ) <EOL> def write ( G , filename , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> write_node_data , write_edge_data , use_node_zen_data , use_edge_zen_data = resolve_write_mode ( kwargs ) <EOL> enc = resolve_codec ( kwargs ) <EOL> fh = open ( filename , '<STR_LIT:w>' ) <EOL> fh . write ( '<STR_LIT>' ) <EOL> fh . write ( '<STR_LIT>' ) <EOL> fh . write ( '<STR_LIT>' % enc . __name__ ) <EOL> fh . write ( '<STR_LIT>' % enc . encode_str . __name__ ) <EOL> fh . write ( '<STR_LIT>' ) <EOL> if G . is_directed ( ) : <EOL> fh . write ( '<STR_LIT>' ) <EOL> else : <EOL> fh . write ( '<STR_LIT>' ) <EOL> if isinstance ( G , BipartiteGraph ) : <EOL> is_bipartite = True <EOL> fh . write ( '<STR_LIT>' ) <EOL> else : <EOL> is_bipartite = False <EOL> fh . write ( '<STR_LIT>' ) <EOL> for nidx , nobj , ndata in G . nodes_iter_ ( obj = True , data = True ) : <EOL> fh . write ( '<STR_LIT>' ) <EOL> fh . write ( '<STR_LIT>' + str ( nidx ) + '<STR_LIT:\n>' ) <EOL> if is_bipartite : <EOL> fh . write ( format_zen_data ( '<STR_LIT>' , G . is_in_U_ ( nidx ) , <NUM_LIT:2> , enc ) ) <EOL> if nobj != None : <EOL> fh . write ( format_zen_data ( '<STR_LIT:name>' , nobj , <NUM_LIT:2> , enc ) ) <EOL> if ndata != None and write_node_data : <EOL> if use_node_zen_data : <EOL> fh . write ( format_zen_data ( '<STR_LIT>' , ndata , <NUM_LIT:2> , enc ) ) <EOL> else : <EOL> for key , val in ndata . items ( ) : <EOL> fh . write ( format_zen_data ( key , val , <NUM_LIT:2> , enc ) ) <EOL> fh . write ( '<STR_LIT>' ) <EOL> for eidx , edata , weight in G . edges_iter_ ( data = True , weight = True ) : <EOL> fh . write ( '<STR_LIT>' ) <EOL> fh . write ( '<STR_LIT>' + str ( G . endpoints_ ( eidx ) [ <NUM_LIT:0> ] ) + '<STR_LIT:\n>' ) <EOL> fh . write ( '<STR_LIT>' + str ( G . endpoints_ ( eidx ) [ <NUM_LIT:1> ] ) + '<STR_LIT:\n>' ) <EOL> fh . write ( '<STR_LIT>' + str ( weight ) + '<STR_LIT:\n>' ) <EOL> if edata != None and write_edge_data : <EOL> if use_edge_zen_data : <EOL> fh . write ( format_zen_data ( '<STR_LIT>' , edata , <NUM_LIT:2> , enc ) ) <EOL> else : <EOL> for key , val in edata . items ( ) : <EOL> fh . write ( format_zen_data ( key , val , <NUM_LIT:2> , enc ) ) <EOL> fh . write ( '<STR_LIT>' ) <EOL> fh . write ( '<STR_LIT>' ) <EOL> fh . close ( ) <EOL> def format_zen_data ( keyname , data , tab_depth , encoder , strict = True ) : <EOL> """<STR_LIT>""" <EOL> if re . search ( '<STR_LIT>' , keyname ) and strict : <EOL> raise ZenException ( <EOL> '<STR_LIT>' ) <EOL> formatted_data = '<STR_LIT>' <EOL> tabs = '<STR_LIT:\t>' * tab_depth <EOL> if not isinstance ( data , ( dict , list , tuple ) ) : <EOL> encoded_data = encoder . encode ( data ) <EOL> if strict : <EOL> try : <EOL> assert ( isinstance ( encoded_data , basestring ) ) <EOL> assert ( encoded_data . startswith ( DIGITS_AND_QUOTES ) ) <EOL> encoded_data . encode ( '<STR_LIT:ascii>' ) <EOL> if encoded_data . startswith ( DIGITS ) : <EOL> num = float ( encoded_data ) <EOL> assert ( num < <NUM_LIT> or num > - <NUM_LIT> ) <EOL> except AssertionError , UnicodeEncodeError : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' ) <EOL> formatted_data = tabs + keyname + '<STR_LIT:U+0020>' + encoded_data + '<STR_LIT:\n>' <EOL> elif isinstance ( data , dict ) : <EOL> formatted_data += tabs + keyname + '<STR_LIT>' <EOL> for key , val in data . items ( ) : <EOL> formatted_data += format_zen_data ( key , val , tab_depth + <NUM_LIT:1> , encoder ) <EOL> formatted_data += tabs + '<STR_LIT>' <EOL> else : <EOL> assert ( isinstance ( data , ( list , tuple ) ) ) <EOL> for val in data : <EOL> formatted_data += format_zen_data ( keyname , val , tab_depth , encoder ) <EOL> return formatted_data <EOL> def make_tree ( fname , ** kwargs ) : <EOL> codec = resolve_codec ( kwargs ) <EOL> fh = open ( fname , '<STR_LIT:r>' ) <EOL> gml_str = fh . read ( ) <EOL> fh . close ( ) <EOL> tok = GMLTokenizer ( ) <EOL> tokens = tok . tokenize ( gml_str ) <EOL> interpreter = GMLInterpreter ( codec , tok ) <EOL> gml_tree = interpreter . interpret ( tokens ) <EOL> return gml_tree <EOL> def read ( fname , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> weight_fxn = kwargs . pop ( '<STR_LIT>' , None ) <EOL> gml_tree = make_tree ( fname , ** kwargs ) <EOL> if '<STR_LIT>' in gml_tree : <EOL> graph_tree = gml_tree [ '<STR_LIT>' ] <EOL> if ( isinstance ( gml_tree , list ) ) : <EOL> graph_tree = gml_tree [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' '<STR_LIT>' <EOL> return build_graph ( graph_tree , weight_fxn ) <EOL> else : <EOL> return None <EOL> def read_all ( fname , ** kwargs ) : <EOL> weight_fxn = kwargs . pop ( '<STR_LIT>' , None ) <EOL> gml_tree = make_tree ( fname , ** kwargs ) <EOL> if '<STR_LIT>' in gml_tree : <EOL> graph_tree = gml_tree [ '<STR_LIT>' ] <EOL> if not isinstance ( graph_tree , list ) : <EOL> graph_tree = [ gml_tree [ '<STR_LIT>' ] ] <EOL> graph_tree = gml_tree [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' '<STR_LIT>' <EOL> return build_graph ( graph_tree , weight_fxn ) <EOL> else : <EOL> return None <EOL> def build_graph ( graph_tree , weight_fxn ) : <EOL> is_bipartite = bool ( '<STR_LIT>' in graph_tree and graph_tree [ '<STR_LIT>' ] ) <EOL> is_directed = bool ( '<STR_LIT>' in graph_tree and graph_tree [ '<STR_LIT>' ] ) <EOL> if is_bipartite : <EOL> G = BipartiteGraph ( ) <EOL> elif is_directed : <EOL> G = DiGraph ( ) <EOL> else : <EOL> G = Graph ( ) <EOL> if '<STR_LIT>' in graph_tree : <EOL> nodes = graph_tree [ '<STR_LIT>' ] <EOL> if not isinstance ( nodes , list ) : <EOL> nodes = [ nodes ] <EOL> for node in nodes : <EOL> has_id = True <EOL> has_valid_id = True <EOL> if '<STR_LIT:id>' not in node : <EOL> has_id = False <EOL> has_valid_id = False <EOL> elif not isinstance ( node [ '<STR_LIT:id>' ] , int ) or node [ '<STR_LIT:id>' ] < <NUM_LIT:0> : <EOL> has_valid_id = False <EOL> node_idx = node [ '<STR_LIT:id>' ] <EOL> if is_bipartite : <EOL> is_in_U = node [ '<STR_LIT>' ] <EOL> standard_keys = set ( [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> node_data = { } <EOL> node_obj = None <EOL> zen_data = None <EOL> for key , val in node . items ( ) : <EOL> if key == '<STR_LIT:name>' : <EOL> node_obj = val <EOL> elif key == '<STR_LIT:label>' and node_obj is None : <EOL> node_obj = val <EOL> elif key == '<STR_LIT>' : <EOL> zen_data = val <EOL> else : <EOL> node_data [ key ] = val <EOL> if zen_data is not None : <EOL> if len ( node_data ) == <NUM_LIT:0> : <EOL> node_data = zen_data <EOL> else : <EOL> node_data [ '<STR_LIT>' ] = zen_data <EOL> elif len ( node_data ) == <NUM_LIT:0> : <EOL> node_data = None <EOL> if not isinstance ( node_obj , basestring ) and node_obj is not None : <EOL> if not isinstance ( node_obj , Hashable ) : <EOL> if not isinstance ( node_obj , Iterable ) : <EOL> node_obj = None <EOL> else : <EOL> node_obj = tuple ( node_obj ) <EOL> if is_bipartite : <EOL> G . add_node_by_class ( is_in_U , node_obj , node_data ) <EOL> elif has_id and has_valid_id : <EOL> if is_directed : <EOL> G . add_node_x ( node_idx , G . edge_list_capacity , <EOL> G . edge_list_capacity , node_obj , node_data ) <EOL> else : <EOL> G . add_node_x ( node_idx , G . edge_list_capacity , node_obj , <EOL> node_data ) <EOL> else : <EOL> if G . is_directed : <EOL> G . add_node ( nobj = node_obj , data = node_data ) <EOL> else : <EOL> G . add_node ( nobj = node_obj , data = node_data ) <EOL> if '<STR_LIT>' in graph_tree : <EOL> edges = graph_tree [ '<STR_LIT>' ] <EOL> if not isinstance ( edges , list ) : <EOL> edges = [ edges ] <EOL> for edge in edges : <EOL> source = None <EOL> target = None <EOL> if '<STR_LIT:source>' not in edge : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' % str ( edge ) ) <EOL> if '<STR_LIT:target>' not in edge : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' % str ( edge ) ) <EOL> weight = <NUM_LIT:1> <EOL> edge_idx = None <EOL> zen_data = None <EOL> edge_data = { } <EOL> for key , val in edge . items ( ) : <EOL> if key == '<STR_LIT:id>' : <EOL> edge_idx = val <EOL> if type ( val ) != int : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' % str ( edge ) ) <EOL> elif key == '<STR_LIT:source>' : <EOL> source = val <EOL> if type ( val ) != int or val < <NUM_LIT:0> : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' % str ( edge ) ) <EOL> elif key == '<STR_LIT:target>' : <EOL> target = val <EOL> if type ( val ) != int or val < <NUM_LIT:0> : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' % str ( edge ) ) <EOL> elif key == '<STR_LIT>' : <EOL> weight = float ( val ) <EOL> elif key == '<STR_LIT>' : <EOL> zen_data = val <EOL> else : <EOL> edge_data [ key ] = val <EOL> if weight_fxn != None : <EOL> weight = weight_fxn ( edge ) <EOL> if zen_data is not None : <EOL> if len ( edge_data ) == <NUM_LIT:0> : <EOL> edge_data = zen_data <EOL> else : <EOL> edge_data [ '<STR_LIT>' ] = zen_data <EOL> elif len ( edge_data ) == <NUM_LIT:0> : <EOL> edge_data = None ; <EOL> if edge_idx != None : <EOL> G . add_edge_x ( edge_idx , source , target , edge_data , weight ) <EOL> else : <EOL> G . add_edge_ ( source , target , edge_data , weight ) <EOL> return G <EOL> def resolve_write_mode ( kwargs ) : <EOL> write_node_data , write_edge_data = True , True <EOL> if '<STR_LIT>' in kwargs : <EOL> if type ( kwargs [ '<STR_LIT>' ] ) == tuple : <EOL> write_node_data , write_edge_data = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> write_node_data = write_edge_data = kwargs [ '<STR_LIT>' ] <EOL> if ( not isinstance ( write_node_data , bool ) or not <EOL> isintstance ( write_edge_data , bool ) ) : <EOL> raise zenException ( '<STR_LIT>' '<STR_LIT>' % ( <EOL> write_data , type ( write_data ) ) ) <EOL> use_node_zen_data , use_edge_zen_data = True , True <EOL> if '<STR_LIT>' in kwargs : <EOL> if type ( kwargs [ '<STR_LIT>' ] ) == tuple : <EOL> use_node_zen_data , use_edge_zen_data = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> use_node_zen_data = use_edge_zen_data = kwargs [ '<STR_LIT>' ] <EOL> if ( not isinstance ( write_node_data , bool ) or <EOL> not isintstance ( write_edge_data , bool ) ) : <EOL> raise zenException ( '<STR_LIT>' '<STR_LIT>' % ( <EOL> write_data , type ( write_data ) ) ) <EOL> return write_node_data , write_edge_data , use_node_zen_data , use_edge_zen_data <EOL> def resolve_codec ( kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> enc = kwargs [ '<STR_LIT>' ] <EOL> try : <EOL> assert ( isinstance ( enc . encode ( '<STR_LIT>' ) , basestring ) ) <EOL> enc . __name__ . encode ( '<STR_LIT:ascii>' ) <EOL> except ( AttributeError , AssertionError ) as e : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in kwargs : <EOL> if kwargs [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> enc = BasicGMLCodec ( ) <EOL> elif kwargs [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> enc = ZenGMLCodec ( ) <EOL> else : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' ) <EOL> else : <EOL> enc = BasicGMLCodec ( ) <EOL> if '<STR_LIT>' in kwargs : <EOL> str_enc = kwargs [ '<STR_LIT>' ] <EOL> try : <EOL> assert ( isinstance ( str_enc ( '<STR_LIT>' ) , basestring ) ) <EOL> except ( TypeError , AssertionError ) as e : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' ) <EOL> enc . encode_str = str_enc <EOL> if '<STR_LIT>' in kwargs : <EOL> str_dec = kwargs [ '<STR_LIT>' ] <EOL> try : <EOL> assert ( isinstance ( str_dec ( '<STR_LIT>' ) , basestring ) ) <EOL> except ( TypeError , AssertionError ) as e : <EOL> raise ZenException ( '<STR_LIT>' '<STR_LIT>' ) <EOL> enc . decode_str = str_dec <EOL> return enc </s>
