Yc@s:dZddlZddlmZy0ddlmZmZddlmZmZWnUe k ry0ddl mZmZddl mZmZWqe k rqXnXyddl j Z Wne k rdZ nXddlZddlmZmZddd d d d d dgZeddddeedZeddddeedZdedZedddeedZedZdefdYZd efdYZ defdYZ!de fd YZ"e dkreZ#neZ#defd!YZ$d"Z%d#Z&dS($s} ******* GraphML ******* Read and write graphs in GraphML format. This implementation does not support mixed graphs (directed and unidirected edges together), hyperedges, nested graphs, or ports. "GraphML is a comprehensive and easy-to-use file format for graphs. It consists of a language core to describe the structural properties of a graph and a flexible extension mechanism to add application-specific data. Its main features include support of * directed, undirected, and mixed graphs, * hypergraphs, * hierarchical graphs, * graphical representations, * references to external data, * application-specific attribute data, and * light-weight parsers. Unlike many other file formats for graphs, GraphML does not use a custom syntax. Instead, it is based on XML and hence ideally suited as a common denominator for all kinds of services generating, archiving, or processing graphs." http://graphml.graphdrawing.org/ Format ------ GraphML is an XML format. See http://graphml.graphdrawing.org/specification.html for the specification and http://graphml.graphdrawing.org/primer/graphml-primer.html for examples. iN(t defaultdict(tElementt ElementTree(ttostringt fromstring(t open_filetmake_strt write_graphmlt read_graphmltgenerate_graphmltwrite_graphml_xmltwrite_graphml_lxmlt parse_graphmlt GraphMLWritert GraphMLReaderitmodetwbsutf-8cCs9td|d|d|}|j||j|dS(s Write G in GraphML XML format to path Parameters ---------- G : graph A networkx graph path : file or string File or filename to write. Filenames ending in .gz or .bz2 will be compressed. encoding : string (optional) Encoding for text data. prettyprint : bool (optional) If True use line breaks and indenting in output XML. infer_numeric_types : boolean Determine if numeric types should be generalized. For example, if edges have both int and float 'weight' attributes, we infer in GraphML that both are floats. Examples -------- >>> G = nx.path_graph(4) >>> nx.write_graphml(G, "test.graphml") Notes ----- It may be a good idea in Python2 to convert strings to unicode before giving the graph to write_gml. At least the strings with either many characters to escape. This implementation does not support mixed graphs (directed and unidirected edges together) hyperedges, nested graphs, or ports. tencodingt prettyprinttinfer_numeric_typesN(R tadd_graph_elementtdump(tGtpathRRRtwriter((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR Hs#  c Cs2t|d|d|d|d|}|jdS(sWrite G in GraphML XML format to path This function uses the LXML framework and should be faster than the version using the xml library. Parameters ---------- G : graph A networkx graph path : file or string File or filename to write. Filenames ending in .gz or .bz2 will be compressed. encoding : string (optional) Encoding for text data. prettyprint : bool (optional) If True use line breaks and indenting in output XML. infer_numeric_types : boolean Determine if numeric types should be generalized. For example, if edges have both int and float 'weight' attributes, we infer in GraphML that both are floats. Examples -------- >>> G = nx.path_graph(4) >>> nx.write_graphml_lxml(G, "fourpath.graphml") # doctest: +SKIP Notes ----- This implementation does not support mixed graphs (directed and unidirected edges together) hyperedges, nested graphs, or ports. tgraphRRRN(tGraphMLWriterLxmlR(RRRRRR((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR qs" ccsHtd|d|}|j|xt|jD] }|Vq5WdS(sGenerate GraphML lines for G Parameters ---------- G : graph A networkx graph encoding : string (optional) Encoding for text data. prettyprint : bool (optional) If True use line breaks and indenting in output XML. Examples -------- >>> G = nx.path_graph(4) >>> linefeed = chr(10) # linefeed = >>> s = linefeed.join(nx.generate_graphml(G)) # doctest: +SKIP >>> for line in nx.generate_graphml(G): # doctest: +SKIP ... print(line) Notes ----- This implementation does not support mixed graphs (directed and unidirected edges together) hyperedges, nested graphs, or ports. RRN(R Rtstrt splitlines(RRRRtline((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR s itrbcCstd|d|}t|d|}t|dkrd}|jd|j}|jd|}t|d|}t|dkrtjdqn|dS( s!Read graph in GraphML format from path. Parameters ---------- path : file or string File or filename to write. Filenames ending in .gz or .bz2 will be compressed. node_type: Python type (default: str) Convert node ids to this type edge_key_type: Python type (default: int) Convert graphml edge ids to this type as key of multi-edges Returns ------- graph: NetworkX graph If no parallel edges are found a Graph or DiGraph is returned. Otherwise a MultiGraph or MultiDiGraph is returned. Notes ----- Default node and edge attributes are not propagated to each node and edge. They can be obtained from `G.graph` and applied to node and edge attributes if desired using something like this: >>> default_color = G.graph['node_default']['color'] # doctest: +SKIP >>> for node, data in G.nodes(data=True): # doctest: +SKIP ... if 'color' not in data: ... data['color']=default_color >>> default_color = G.graph['edge_default']['color'] # doctest: +SKIP >>> for u, v, data in G.edges(data=True): # doctest: +SKIP ... if 'color' not in data: ... data['color']=default_color This implementation does not support mixed graphs (directed and unidirected edges together), hypergraphs, nested graphs, or ports. For multigraphs the GraphML edge "id" will be used as the edge key. If not specified then they "key" attribute will be used. If there is no "key" attribute a default NetworkX multigraph edge key will be provided. Files with the yEd "yfiles" extension will can be read but the graphics information is discarded. yEd compressed files ("file.graphmlz" extension) can be read by renaming the file to "file.graphml.gz". t node_typet edge_key_typeRis7s tstrings%file not successfully read as graphml(Rtlisttlentseektreadtreplacetnxt NetworkXError(RRR treadertglisttheadert old_bytest new_bytes((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyRs5  cCstd|}t|d|}t|dkrd}|jd|}t|d|}t|dkrtjdqn|dS(sRead graph in GraphML format from string. Parameters ---------- graphml_string : string String containing graphml information (e.g., contents of a graphml file). node_type: Python type (default: str) Convert node ids to this type Returns ------- graph: NetworkX graph If no parallel edges are found a Graph or DiGraph is returned. Otherwise a MultiGraph or MultiDiGraph is returned. Examples -------- >>> G = nx.path_graph(4) >>> linefeed = chr(10) # linefeed = >>> s = linefeed.join(nx.generate_graphml(G)) >>> H = nx.parse_graphml(s) Notes ----- Default node and edge attributes are not propagated to each node and edge. They can be obtained from `G.graph` and applied to node and edge attributes if desired using something like this: >>> default_color = G.graph['node_default']['color'] # doctest: +SKIP >>> for node, data in G.nodes(data=True): # doctest: +SKIP ... if 'color' not in data: ... data['color']=default_color >>> default_color = G.graph['edge_default']['color'] # doctest: +SKIP >>> for u, v, data in G.edges(data=True): # doctest: +SKIP ... if 'color' not in data: ... data['color']=default_color This implementation does not support mixed graphs (directed and unidirected edges together), hypergraphs, nested graphs, or ports. For multigraphs the GraphML edge "id" will be used as the edge key. If not specified then they "key" attribute will be used. If there is no "key" attribute a default NetworkX multigraph edge key will be provided. RR!is7s s%file not successfully read as graphml(RR"R#R&R'R((tgraphml_stringRR)R*R+t new_string((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR s1tGraphMLc BseZdZdZdZdjddgZyedeZ e Z Wne k rZnXe dfedfedfe dfe d fe d fe d fe d fed fg ZeeZedeDZied6ed6ed6ed6ed6ed6ZRS(s%http://graphml.graphdrawing.org/xmlnss)http://www.w3.org/2001/XMLSchema-instances!http://www.yworks.com/xml/graphmlt s5http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsdi90tintegertyfilesR!tinttlongtfloattdoubletbooleanccs|]}t|VqdS(N(treversed(t.0ta((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pys Rsttruetfalset0it1i(t__name__t __module__t NS_GRAPHMLtNS_XSItNS_YtjointSCHEMALOCATIONtchrRtunicodeR4R5t ValueErrorR6tboolttypestdicttxml_typet python_typetTruetFalset convert_bool(((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR0:s,       cBseZddeedZdZdZdZdddZ dZ dZ d Z d Z d Zd Zd dZRS(sutf-8cCsyddl}Wn#tk r5d}t|nXt|_||_||_||_|jdi|jd6|jd6|j d6|_ i|_ t t |_t t|_|dk r|j|ndS(Nis3GraphML writer requires xml.elementtree.ElementTreetgraphmltxmlnss xmlns:xsisxsi:schemaLocation(txml.etree.ElementTreet ImportErrorRt myElementRRRRBRCRFtxmltkeysRR"t attributestsettattribute_typestNoneR(tselfRRRRRWtmsg((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyt__init__bs$          cCs;|jr|j|jnt|jj|j}|S(N(RtindentRWRtdecodeR(R]ts((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyt__str__ys cCs|jr|j||f}ytdt}t}Wntk rUt}t}nXt|dkrt|krxtS||kr|St |krt S||kr|StSqt |dSn t |SdS(sInfer the attribute type of data named name. Currently this only supports inference of numeric types. If self.infer_numeric_types is false, type is used. Otherwise, pick the most general of types found across all values with name and scope. This means edges with data named 'weight' are treated separately from nodes with data named 'weight'. i90iiN( RR[RGR4RRIR5RHR#R6R"ttype(R]tnametscopetvalueRKt local_longt local_unicode((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyt attr_types*         c Cs|||f}y|j|SWntk rdtt|j}||j|s N(t is_directedRtpopR\RVRLRRRRRYRqR~RRjRW( R]Rtdefault_edge_typetgraphidRRnRzRRRRf((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyRs*       cCs"x|D]}|j|qWdS(s+ Add many graphs to this GraphML document. N(R(R]t graph_listR((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyt add_graphs s cCsK|jr|j|jnt|j}|j|d|jdtdS(NRtxml_declaration(RR`RWRtwriteRRO(R]tstreamtdocument((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyRs icCsd|d}t|r|j s4|jj rD|d|_n|j s^|jj rj||_nx"|D]}|j||dqqW|j s|jj r||_qn,|r|j s|jj r||_ndS(Ns s i(R#RptstripttailR`(R]telemtlevelti((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR`s    N(R@RAR\RORPR_RcRjRxR~RRRRRRR`(((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR as    %     #  tIncrementalElementcBs eZdZdZdZRS(sWrapper for _IncrementalWriter providing an Element like interface. This wrapper does not intend to be a complete implemenation but rather to deal with those calls used in GraphMLWriter. cCs||_||_dS(N(RWR(R]RWR((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR_-s cCs|jj|d|jdS(Nt pretty_print(RWRR(R]telement((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyRq1s(R@RAt__doc__R_Rq(((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR&s RcBsAeZddeedZdZdZdZdZ RS(sutf-8cCstj|_||_||_||_tj|d||_|jj|_ |j j g|_ |j |_ |j j di|jd6|jd6|jd6|_|jji|_tt|_|dk r|j|ndS(NRRRRSs xmlns:xsisxsi:schemaLocation(t lxmletreeRRVt _encodingt _prettyprintRtxmlfilet _xml_baset __enter__t_xmltwrite_declarationRWt_keysRRBRCRFt_graphmlRXRRZR[R\R(R]RRRRR((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR_6s(             c Cs|jrd}nd}|jjdd}|dkrW|jjdd|}n|jjdd|d|}d|jjD}|jjdi}|jjdi}x@|jD]2\}} |jt |dfj t | qWxR|jD]D\}} |j |j |d| } |jt || ddqWi} x||jd tD]h\} } xY| jD]K\}} |jt |d fj t | || kr| | |ks RRRzRRXRRN(RRRR\RRRRR[RRRdRMRjRxRRORRRWRRRRRR(R]RRRRt graphdataRRRRR{RYRtdtTRtekeyRmtincremental_writer((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyRVsj     * & ) +& ,"& )cCssxl|jD]^\}}|jt||jt|||t|||j|}|j|q WdS(sAppends attribute data.N(RR~RRjRRq(R]RfRRzRnRRR}((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyRs cCs tj|S(N(tobjectRc(R]((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyRcscCs0|jjddd|jjddddS(N(Rt__exit__R\R(R]((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyRsN( R@RAR\RORPR_RRRcR(((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR5s    J  cBsYeZdZeedZdddZdZdZ dZ dZ dZ RS( s:Read a GraphML document. Produces NetworkX graph objects.cCs^yddl}Wn#tk r5d}t|nX||_||_t|_i|_dS(Nis3GraphML reader requires xml.elementtree.ElementTree(RTRURR RPt multigraphtedge_ids(R]RR RWR^((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyR_s    ccs|dk r!td||_n*|dk r?t||_n td|j|j\}}x5|jjd|jD]}|j|||Vq}WdS(Ntfiles/Must specify either 'path' or 'string' as kwargs {%s}graph( R\RRWRRItfind_graphml_keystfindallRBt make_graph(R]RR!RXtdefaultstg((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/graphml.pyt__call__s    cCs|jdd}|dkr-tj}n tj}i|jd-sP       '&C >'{