<s> """<STR_LIT>""" <EOL> __author__ = """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> import networkx as nx <EOL> from networkx . exception import NetworkXError <EOL> from networkx . utils import is_string_like , open_file <EOL> @ open_file ( <NUM_LIT:0> , mode = '<STR_LIT:rb>' ) <EOL> def read_gml ( path , encoding = '<STR_LIT>' , relabel = False ) : <EOL> """<STR_LIT>""" <EOL> lines = ( line . decode ( encoding ) for line in path ) <EOL> G = parse_gml ( lines , relabel = relabel ) <EOL> return G <EOL> def parse_gml ( lines , relabel = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from pyparsing import ParseException <EOL> except ImportError : <EOL> try : <EOL> from matplotlib . pyparsing import ParseException <EOL> except : <EOL> raise ImportError ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> try : <EOL> data = "<STR_LIT>" . join ( lines ) <EOL> gml = pyparse_gml ( ) <EOL> tokens = gml . parseString ( data ) <EOL> except ParseException as err : <EOL> print ( ( err . line ) ) <EOL> print ( ( "<STR_LIT:U+0020>" * ( err . column - <NUM_LIT:1> ) + "<STR_LIT>" ) ) <EOL> print ( err ) <EOL> raise <EOL> def wrap ( tok ) : <EOL> listtype = type ( tok ) <EOL> result = { } <EOL> for k , v in tok : <EOL> if type ( v ) == listtype : <EOL> result [ str ( k ) ] = wrap ( v ) <EOL> else : <EOL> result [ str ( k ) ] = v <EOL> return result <EOL> multigraph = False <EOL> if tokens . directed == <NUM_LIT:1> : <EOL> G = nx . MultiDiGraph ( ) <EOL> else : <EOL> G = nx . MultiGraph ( ) <EOL> for k , v in tokens . asList ( ) : <EOL> if k == "<STR_LIT>" : <EOL> vdict = wrap ( v ) <EOL> node = vdict [ '<STR_LIT:id>' ] <EOL> G . add_node ( node , attr_dict = vdict ) <EOL> elif k == "<STR_LIT>" : <EOL> vdict = wrap ( v ) <EOL> source = vdict . pop ( '<STR_LIT:source>' ) <EOL> target = vdict . pop ( '<STR_LIT:target>' ) <EOL> if G . has_edge ( source , target ) : <EOL> multigraph = True <EOL> G . add_edge ( source , target , attr_dict = vdict ) <EOL> else : <EOL> G . graph [ k ] = v <EOL> if not multigraph : <EOL> if G . is_directed ( ) : <EOL> G = nx . DiGraph ( G ) <EOL> else : <EOL> G = nx . Graph ( G ) <EOL> if relabel : <EOL> mapping = [ ( n , d [ '<STR_LIT:label>' ] ) for n , d in G . node . items ( ) ] <EOL> x , y = zip ( * mapping ) <EOL> if len ( set ( y ) ) != len ( G ) : <EOL> raise NetworkXError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> G = nx . relabel_nodes ( G , dict ( mapping ) ) <EOL> return G <EOL> def pyparse_gml ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from pyparsing import Literal , CaselessLiteral , Word , Forward , ZeroOrMore , Group , Dict , Optional , Combine , ParseException , restOfLine , White , alphas , alphanums , nums , OneOrMore , quotedString , removeQuotes , dblQuotedString , Regex <EOL> except ImportError : <EOL> try : <EOL> from matplotlib . pyparsing import Literal , CaselessLiteral , Word , Forward , ZeroOrMore , Group , Dict , Optional , Combine , ParseException , restOfLine , White , alphas , alphanums , nums , OneOrMore , quotedString , removeQuotes , dblQuotedString , Regex <EOL> except : <EOL> raise ImportError ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> lbrack = Literal ( "<STR_LIT:[>" ) . suppress ( ) <EOL> rbrack = Literal ( "<STR_LIT:]>" ) . suppress ( ) <EOL> pound = ( "<STR_LIT:#>" ) <EOL> comment = pound + Optional ( restOfLine ) <EOL> integer = Word ( nums + '<STR_LIT:->' ) . setParseAction ( lambda s , l , t : [ int ( t [ <NUM_LIT:0> ] ) ] ) <EOL> real = Regex ( r"<STR_LIT>" ) . setParseAction ( <EOL> lambda s , l , t : [ float ( t [ <NUM_LIT:0> ] ) ] ) <EOL> dblQuotedString . setParseAction ( removeQuotes ) <EOL> key = Word ( alphas , alphanums + '<STR_LIT:_>' ) <EOL> value_atom = ( real | integer | Word ( alphanums ) | dblQuotedString ) <EOL> value = Forward ( ) <EOL> keyvalue = Group ( key + value ) <EOL> value << ( value_atom | Group ( lbrack + ZeroOrMore ( keyvalue ) + rbrack ) ) <EOL> node = Group ( Literal ( "<STR_LIT>" ) + lbrack + Group ( OneOrMore ( keyvalue ) ) + rbrack ) <EOL> edge = Group ( Literal ( "<STR_LIT>" ) + lbrack + Group ( OneOrMore ( keyvalue ) ) + rbrack ) <EOL> creator = Group ( Literal ( "<STR_LIT>" ) + Optional ( restOfLine ) ) <EOL> version = Group ( Literal ( "<STR_LIT>" ) + Optional ( restOfLine ) ) <EOL> graphkey = Literal ( "<STR_LIT>" ) . suppress ( ) <EOL> graph = Dict ( Optional ( creator ) + Optional ( version ) + graphkey + lbrack + ZeroOrMore ( ( node | edge | keyvalue ) ) + rbrack ) <EOL> graph . ignore ( comment ) <EOL> return graph <EOL> def generate_gml ( G ) : <EOL> """<STR_LIT>""" <EOL> def listify ( d , indent , indentlevel ) : <EOL> result = '<STR_LIT>' <EOL> for k , v in d . items ( ) : <EOL> if type ( v ) == dict : <EOL> v = listify ( v , indent , indentlevel + <NUM_LIT:1> ) <EOL> result += ( indentlevel + <NUM_LIT:1> ) * indent + string_item ( k , v , indentlevel * indent ) + '<STR_LIT:\n>' <EOL> return result + indentlevel * indent + "<STR_LIT:]>" <EOL> def string_item ( k , v , indent ) : <EOL> if type ( v ) == dict : <EOL> v = listify ( v , indent , <NUM_LIT:2> ) <EOL> elif is_string_like ( v ) : <EOL> v = '<STR_LIT>' % v <EOL> elif type ( v ) == bool : <EOL> v = int ( v ) <EOL> return "<STR_LIT>" % ( k , v ) <EOL> if hasattr ( G , '<STR_LIT>' ) : <EOL> graph_attr = G . graph_attr <EOL> else : <EOL> graph_attr = { } <EOL> if hasattr ( G , '<STR_LIT>' ) : <EOL> node_attr = G . node_attr <EOL> else : <EOL> node_attr = { } <EOL> indent = <NUM_LIT:2> * '<STR_LIT:U+0020>' <EOL> count = iter ( range ( len ( G ) ) ) <EOL> node_id = { } <EOL> yield "<STR_LIT>" <EOL> if G . is_directed ( ) : <EOL> yield indent + "<STR_LIT>" <EOL> for k , v in G . graph . items ( ) : <EOL> if k == '<STR_LIT>' : <EOL> continue <EOL> yield indent + string_item ( k , v , indent ) <EOL> for n in G : <EOL> yield indent + "<STR_LIT>" <EOL> nid = G . node [ n ] . get ( '<STR_LIT:id>' , next ( count ) ) <EOL> node_id [ n ] = nid <EOL> yield <NUM_LIT:2> * indent + "<STR_LIT>" % nid <EOL> label = G . node [ n ] . get ( '<STR_LIT:label>' , n ) <EOL> if is_string_like ( label ) : <EOL> label = '<STR_LIT>' % label <EOL> yield <NUM_LIT:2> * indent + '<STR_LIT>' % label <EOL> if n in G : <EOL> for k , v in G . node [ n ] . items ( ) : <EOL> if k == '<STR_LIT:id>' or k == '<STR_LIT:label>' : continue <EOL> yield <NUM_LIT:2> * indent + string_item ( k , v , indent ) <EOL> yield indent + "<STR_LIT:]>" <EOL> for u , v , edgedata in G . edges_iter ( data = True ) : <EOL> yield indent + "<STR_LIT>" <EOL> yield <NUM_LIT:2> * indent + "<STR_LIT>" % node_id [ u ] <EOL> yield <NUM_LIT:2> * indent + "<STR_LIT>" % node_id [ v ] <EOL> for k , v in edgedata . items ( ) : <EOL> if k == '<STR_LIT:source>' : continue <EOL> if k == '<STR_LIT:target>' : continue <EOL> yield <NUM_LIT:2> * indent + string_item ( k , v , indent ) <EOL> yield indent + "<STR_LIT:]>" <EOL> yield "<STR_LIT:]>" <EOL> @ open_file ( <NUM_LIT:1> , mode = '<STR_LIT:wb>' ) <EOL> def write_gml ( G , path ) : <EOL> """<STR_LIT>""" <EOL> for line in generate_gml ( G ) : <EOL> line += '<STR_LIT:\n>' <EOL> path . write ( line . encode ( '<STR_LIT>' ) ) <EOL> def setup_module ( module ) : <EOL> from nose import SkipTest <EOL> try : <EOL> import pyparsing <EOL> except : <EOL> try : <EOL> import matplotlib . pyparsing <EOL> except : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> def teardown_module ( module ) : <EOL> import os <EOL> os . unlink ( '<STR_LIT>' ) <EOL> os . unlink ( '<STR_LIT>' ) </s>
