Yc@sdZddlZddlZddlZddlmZmZy&ddlm Z m Z m Z m Z WnKe k ry&ddlm Z m Z m Z m Z Wne k rnXnXddddgZed d d d ed dZd ed dZedd deed dZdefdYZdefdYZdefdYZdZdZdZdS(sRead and write graphs in GEXF format. GEXF (Graph Exchange XML Format) is a language for describing complex network structures, their associated data and dynamics. This implementation does not support mixed graphs (directed and undirected edges together). Format ------ GEXF is an XML format. See https://gephi.org/gexf/format/schema.html for the specification and https://gephi.org/gexf/format/basic.html for examples. iN(t open_filetmake_str(tElementt ElementTreet SubElementttostringt write_gexft read_gexftrelabel_gexf_grapht generate_gexfitmodetwbsutf-8s1.2draftcCs9td|d|d|}|j||j|dS(sWrite G in GEXF format to path. "GEXF (Graph Exchange XML Format) is a language for describing complex networks structures, their associated data and dynamics" [1]_. Node attributes are checked according to the version of the GEXF schemas used for parameters which are not user defined, e.g. visualization 'viz' [2]_. See example for usage. Parameters ---------- G : graph A NetworkX graph path : file or string File or file name to write. File names ending in .gz or .bz2 will be compressed. encoding : string (optional, default: 'utf-8') Encoding for text data. prettyprint : bool (optional, default: True) If True use line breaks and indenting in output XML. Examples -------- >>> G = nx.path_graph(4) >>> nx.write_gexf(G, "test.gexf") # visualization data >>> G.nodes[0]['viz'] = {'size': 54} >>> G.nodes[0]['viz']['position'] = {'x' : 0, 'y' : 1} >>> G.nodes[0]['viz']['color'] = {'r' : 0, 'g' : 0, 'b' : 256} Notes ----- This implementation does not support mixed graphs (directed and undirected edges together). The node id attribute is set to be the string of the node label. If you want to specify an id use set it as node data, e.g. node['a']['id']=1 to set the id of node 'a' to 1. References ---------- .. [1] GEXF File Format, https://gephi.org/gexf/format/ .. [2] GEXF viz schema 1.1, https://gephi.org/gexf/1.1draft/viz tencodingt prettyprinttversionN(t GEXFWritert add_graphtwrite(tGtpathR R Rtwriter((sm/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/gexf.pyR&s0  ccsNtd|d|d|}|j|xt|jD] }|Vq;WdS(sGenerate lines of GEXF format representation of G. "GEXF (Graph Exchange XML Format) is a language for describing complex networks structures, their associated data and dynamics" [1]_. Parameters ---------- G : graph A NetworkX graph encoding : string (optional, default: 'utf-8') Encoding for text data. prettyprint : bool (optional, default: True) If True use line breaks and indenting in output XML. version : string (default: 1.2draft) Version of GEFX File Format (see https://gephi.org/gexf/format/schema.html). Supported values: "1.1draft", "1.2draft" Examples -------- >>> G = nx.path_graph(4) >>> linefeed = chr(10) # linefeed= >>> s = linefeed.join(nx.generate_gexf(G)) # doctest: +SKIP >>> for line in nx.generate_gexf(G): # doctest: +SKIP ... print line Notes ----- This implementation does not support mixed graphs (directed and undirected edges together). The node id attribute is set to be the string of the node label. If you want to specify an id use set it as node data, e.g. node['a']['id']=1 to set the id of node 'a' to 1. References ---------- .. [1] GEXF File Format, https://gephi.org/gexf/format/ R R RN(RRtstrt splitlines(RR R RRtline((sm/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/gexf.pyR \s (  itrbcCs@td|d|}|r0t||}n ||}|S(sRead graph in GEXF format from path. "GEXF (Graph Exchange XML Format) is a language for describing complex networks structures, their associated data and dynamics" [1]_. Parameters ---------- path : file or string File or file name to write. File names ending in .gz or .bz2 will be compressed. node_type: Python type (default: None) Convert node ids to this type if not None. relabel : bool (default: False) If True relabel the nodes to use the GEXF node "label" attribute instead of the node "id" attribute as the NetworkX node label. version : string (default: 1.2draft) Version of GEFX File Format (see https://gephi.org/gexf/format/schema.html). Supported values: "1.1draft", "1.2draft" Returns ------- graph: NetworkX graph If no parallel edges are found a Graph or DiGraph is returned. Otherwise a MultiGraph or MultiDiGraph is returned. Notes ----- This implementation does not support mixed graphs (directed and undirected edges together). References ---------- .. [1] GEXF File Format, https://gephi.org/gexf/format/ t node_typeR(t GEXFReaderR(RRtrelabelRtreaderR((sm/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/gexf.pyRs $ tGEXFc BseZiZidd6dd6dd6djddgd6d d 6Zeed sttruetfalsetTruetFalset0it1icCs|jj|}|dkr4tjd|n|d|_|d|_|d|_|d|_|d|_ ||_ dS(NsUnknown GEXF version %s.RRR R#( tversionstgettNonetnxt NetworkXErrorRRR R"R#R(tselfRtd((sm/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/gexf.pyt set_versions      (t__name__t __module__R5tjoinR;tintR%tbooltlisttdictttypestchrtblurbtextendRt ValueErrorR)tunicodetxml_typet python_typeR1R2t convert_boolR<(((sm/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/gexf.pyRsZ                           RcBseZddeddZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZddZRS(sutf-8s1.2draftcCsGyddljj}Wntk r5tdnX||_||_|j|tdi|jd6|j d6|j d6|j d6|_ |j d|jtj|_tj|_i|_i|jd R7R1R`RdRRlRmRnRRRRRRRRRa(((sm/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/gexf.pyRs       : . .   RcBs}eZd ddZdZdZd dZdZdZdZ dZ d Z d Z d Z d ZRS(s1.2draftcCsSyddl}Wntk r/tdnX||_t|_|j|dS(Nis1GEXF reader requires xml.elementtree.ElementTree.(RTRVRR1t simple_graphR<(R:RRRW((sm/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/gexf.pyR`^s   cCstd||_|jjd|j}|dk rD|j|SxP|jD]E}|j||jjd|j}|dk rN|j|SqNWtj ddS(Ntfiles {%s}graphs No element in GEXF file.( RRWtfindRR7t make_graphR5R<R8R9(R:tstreamRR((sm/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/gexf.pyt__call__is    cCsJ|jdd}|dkr-tj}n tj}|jdd}|dkrg||jd|j|||dR7R`RRRR RRRRRR R(((sm/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/gexf.pyR[s O # ' 6 "c Csty.g|D]}||j|df^q }Wn tk rPtjdnXt|\}}tt|t|krtjdnt|}tj||}x|D]}||}||j|d<|j|j dd|j|kr#||j|d|j|ds2   & & 5/+@eK 3