B
    ,µ[Í•  ã               @   s  d Z ddlZddlZddlZddlmZmZ yddlm	Z	m
Z
mZmZ W nF ek
rŽ   yddlm	Z	m
Z
mZmZ W n ek
rˆ   Y nX Y nX ddddgZed	d
dddd„ƒZd dd„Zedddd!dd„ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ ZdS )"a¨  Read 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.
é    N)Ú	open_fileÚmake_str)ÚElementÚElementTreeÚ
SubElementÚtostringÚ
write_gexfÚ	read_gexfÚrelabel_gexf_graphÚgenerate_gexfé   Úwb)Úmodeúutf-8Tú1.2draftc             C   s&   t |||d}| | ¡ | |¡ dS )aß  Write 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
    )ÚencodingÚprettyprintÚversionN)Ú
GEXFWriterÚ	add_graphÚwrite)ÚGÚpathr   r   r   Úwriter© r   ú6lib/python3.7/site-packages/networkx/readwrite/gexf.pyr   &   s    0
c             c   s8   t |||d}| | ¡ xt|ƒ ¡ D ]
}|V  q&W dS )aù  Generate 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   r   N)r   r   ÚstrÚ
splitlines)r   r   r   r   r   Úliner   r   r   r   \   s
    (
ÚrbFc             C   s*   t ||d}|rt|| ƒƒ}n|| ƒ}|S )a–  Read 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/
    )Ú	node_typer   )Ú
GEXFReaderr
   )r   r    Zrelabelr   Úreaderr   r   r   r   r	   ‹   s
    $c               @   s,  e Zd Zi Zdddd ddg¡ddœZeed< d	d
dd d	dg¡ddœZeed< edfedfedfedfe	dfe
dfgZy.edƒZe edfedfedfedfg¡ W nH ek
rî   e edfedfedfedfedfedfedfg¡ Y nX e
eƒZe
dd„ eD ƒƒZdddddddddœZdd„ ZdS )ÚGEXFzhttp://www.gexf.net/1.1draftz http://www.gexf.net/1.1draft/vizz)http://www.w3.org/2001/XMLSchema-instanceú z%http://www.gexf.net/1.1draft/gexf.xsdz1.1)ÚNS_GEXFÚNS_VIZÚNS_XSIÚSCHEMALOCATIONÚVERSIONz1.1draftzhttp://www.gexf.net/1.2draftz http://www.gexf.net/1.2draft/vizz%http://www.gexf.net/1.2draft/gexf.xsdz1.2z1.2draftZintegerÚfloatÚdoubleÚbooleanÚstringiÝ  ÚlongZ
liststringZanyURIc             c   s   | ]}t |ƒV  qd S )N)Úreversed)Ú.0Úar   r   r   ú	<genexpr>á   s    zGEXF.<genexpr>TF)ÚtrueZfalseÚTrueÚFalseÚ0r   Ú1r   c             C   s^   | j  |¡}|d kr"t d| ¡‚|d | _|d | _|d | _|d | _|d | _|| _	d S )NzUnknown GEXF version %s.r%   r&   r'   r)   )
ÚversionsÚgetÚnxÚNetworkXErrorr%   r&   r'   r(   r)   r   )Úselfr   Údr   r   r   Úset_versionë   s    




zGEXF.set_versionN)Ú__name__Ú
__module__Ú__qualname__r8   Újoinr=   Úintr*   ÚboolÚlistÚdictÚtypesÚchrZblurbÚextendr   Ú
ValueErrorr.   ZunicodeÚxml_typeÚpython_typeÚconvert_boolr>   r   r   r   r   r#   ·   sX   

r#   c               @   sˆ   e Zd Zd$dd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd%d"d#„ZdS )&r   Núutf-8Tú1.2draftc             C   sò   ydd l m  m} W n tk
r2   tdƒ‚Y nX || _|| _|  |¡ td| j| j	| j
| jdœƒ| _| d| j¡ t ¡ | _t ¡ | _i | _i | jd< i | jd< i | jd d< i | jd d	< i | jd d< i | jd d	< |d k	rî|  |¡ d S )
Nr   z0GEXF writer requires xml.elementtree.ElementTreeZgexf)Zxmlnsz	xmlns:xsizxsi:schemaLocationr   ÚvizÚnodeÚedgeÚdynamicÚstatic)Úxml.etree.ElementTreeZetreer   ÚImportErrorr   r   r>   r   r%   r'   r(   r)   ÚxmlZregister_namespacer&   Ú	itertoolsÚcountÚedge_idÚattr_idÚattrr   )r<   Úgraphr   r   r   ZETr   r   r   Ú__init__ú   s0    




zGEXFWriter.__init__c             C   s(   | j r|  | j¡ t| jƒ | j¡}|S )N)r   ÚindentrW   r   Údecoder   )r<   Úsr   r   r   Ú__str__  s    zGEXFWriter.__str__c             C   s„   |j  d¡dkrd}nd}| ¡ r(d}nd}|j  dd¡}td|||d	}|| _|  ||¡ |  ||¡ |  ||¡ | j 	|¡ d S )
Nr   rS   rT   ÚdirectedÚ
undirectedÚnameÚ r]   )Údefaultedgetyper   re   )
r]   r9   Úis_directedr   Úgraph_elementÚadd_metaÚ	add_nodesÚ	add_edgesrW   Úappend)r<   r   r   Údefaultre   ri   r   r   r   r   !  s    zGEXFWriter.add_graphc             C   s<   t dƒ}d tj¡t|dƒ_t d¡t|dƒ_| |¡ d S )NÚmetazNetworkX {}Zcreatorz%d/%m/%YZlastmodified)	r   Úformatr:   Ú__version__r   ÚtextÚtimeZstrftimerm   )r<   r   ri   Zmeta_elementr   r   r   rj   5  s    zGEXFWriter.add_metac          	   C   s€  t dƒ}xf|jddD ]T\}}| ¡ }t| d|¡ƒ}d|i}t| d|¡ƒ}	|	|d< y| d¡}
t|
ƒ|d< W n tk
rˆ   Y nX y$| d¡}t|ƒ|d< |  |¡ W n tk
rÂ   Y nX y$| d¡}t|ƒ|d< |  |¡ W n tk
rü   Y nX t d|Ž}|j d
i ¡}|  	||¡}| j
dkr<|  ||¡}n|  ||¡}|  ||¡}|  d	|||¡}| |¡ qW | |¡ d S )NÚnodesT)ÚdataÚidÚlabelÚpidÚstartÚendrQ   Únode_defaultz1.1)rQ   )r   rt   Úcopyr   ÚpopÚKeyErrorÚalter_graph_mode_timeformatr]   r9   Úadd_parentsr   Ú
add_slicesÚ
add_spellsÚadd_vizÚadd_attributesrm   )r<   r   ri   Únodes_elementrQ   ru   Ú	node_dataÚnode_idÚkwrw   rx   ry   rz   Únode_elementrn   r   r   r   rk   <  sF    



zGEXFWriter.add_nodesc          	      s¼  ‡ fdd„}t dƒ}x–||ƒD ]ˆ\}}}}dt|ƒi}	y| d¡}
t|
ƒ|	d< W n tk
rh   Y nX y| d¡}t|ƒ|	d< W n tk
r˜   Y nX y$| d¡}t|ƒ|	d< ˆ  |¡ W n tk
rÒ   Y nX y$| d¡}t|ƒ|	d< ˆ  |¡ W n tk
r   Y nX t|j|  d|¡ƒ}t|j|  d|¡ƒ}t d||d
œ|	—Ž}|j di ¡}ˆ jdkrxˆ  	||¡}nˆ  
||¡}ˆ  ||¡}ˆ  d	|||¡}| |¡ q W | |¡ d S )Nc             3   s¾   |   ¡ rjx°| jdddD ]L\}}}}| ¡ }|j|d | dd ¡}|d krVtˆ jƒ}||||fV  qW nPxN| jddD ]>\}}}| ¡ }| dd ¡}|d kr¨tˆ jƒ}||||fV  qxW d S )NT)ru   Úkeys)Úkeyrv   )ru   )Zis_multigraphÚedgesr|   Úupdater}   ÚnextrZ   )r   ÚuÚvr‹   ru   Ú	edge_datarZ   )r<   r   r   Úedge_key_datae  s    

z+GEXFWriter.add_edges.<locals>.edge_key_datarŒ   rv   ÚweightÚtypery   rz   rR   )ÚsourceÚtargetÚedge_defaultz1.1)rR   )r   r   r}   r~   r   rt   r9   r]   r   r   r‚   rƒ   r„   rm   )r<   r   ri   r’   Úedges_elementr   r   r‹   r‘   rˆ   Zedge_weightZ	edge_typery   rz   Z	source_idZ	target_idÚedge_elementrn   r   )r<   r   rl   d  sN    



zGEXFWriter.add_edgesc             C   sž  t dƒ}t|ƒdkr|S d}xp| ¡ D ]b\}}|dkr>d}t|ƒ}	t|tƒr"xD|D ]<\}
}}t|
ƒ}	|d k	sz|d k	rXd}|  |¡ |  |¡ P qXW |  t|ƒ| j	|	 |||¡}xÖ|D ]b\}
}}t dƒ}||j
d< t|
ƒ|j
d	< |d k	rút|ƒ|j
d
< |d k	rt|ƒ|j
d< | |¡ qºW q(d}|  t|ƒ| j	|	 |||¡}t dƒ}||j
d< t|tƒrtt|ƒ ¡ |j
d	< nt|ƒ|j
d	< | |¡ q(W | |¡ |S )NÚ	attvaluesr   rT   r‹   Únetworkx_keyrS   ZattvalueÚforÚvaluery   rz   )r   ÚlenÚitemsr”   Ú
isinstancerE   r   Úget_attr_idr   rK   Úattribrm   rD   Úlower)r<   Znode_or_edgeZxml_objru   rn   rš   r   Úkr   Zval_typeÚvalry   rz   r[   Úer   r   r   r„   ž  sL    







zGEXFWriter.add_attributesc             C   s
  y| j | | | S  tk
r   tt| jƒƒ}|| j | | |< |||dœ}td
|Ž}| |¡}	|	d k	rˆtdƒ}
t|	ƒ|
_| 	|
¡ d }x>| j
 d¡D ].}| d¡}| dd¡}||krš||krš|}qšW |d krö||dœ}td|Ž}| j
 d	|¡ | 	|¡ Y nX |S )N)rv   Útitler”   Ú	attributern   Ú
attributesÚclassr   rT   )r   rª   r   )r¨   )r©   )r\   r~   r   rŽ   r[   r   r9   r   rr   rm   ri   ÚfindallÚinsert)r<   r§   Z	attr_typeZedge_or_nodern   r   Znew_idZattr_kwargsr¨   Zdefault_titleZdefault_elementÚattributes_elementr1   Za_classZa_moder   r   r   r¡   Ì  s0    






zGEXFWriter.get_attr_idc       
   	   C   sÄ  |  dd¡}|rÀ| d¡}|d k	r®| jdkrdtd| j t| d¡ƒt| d¡ƒt| d¡ƒd	}n@td| j t| d¡ƒt| d¡ƒt| d¡ƒt| d
¡ƒd}| |¡ | d¡}|d k	ràtd| j t|ƒd}| |¡ | d¡}|d k	rtd| j t|ƒd}| |¡ | d¡}|d k	rn| d¡rNtd| j dt|ƒd}ntd| j t|ƒd}| |¡ | d¡}	|	d k	rÀtd| j t|	 d¡ƒt|	 d¡ƒt|	 d¡ƒd}| |¡ |S )NrP   FÚcolorz1.1z	{%s}colorÚrÚgÚb)r¯   r°   r±   r1   )r¯   r°   r±   r1   Úsizez{%s}size)r   Ú	thicknessz{%s}thicknessÚshapeZhttpz	{%s}shapeÚimage)r   ÚuriÚpositionz{%s}positionÚxÚyÚz)r¸   r¹   rº   )r}   r9   r)   r   r&   r   rm   Ú
startswith)
r<   Úelementr†   rP   r®   r¦   r²   r³   r´   r·   r   r   r   rƒ   ì  sL    

















zGEXFWriter.add_vizc             C   sT   |  dd¡}|rPtdƒ}x,|D ]$}tdƒ}t|ƒ|jd< | |¡ qW | |¡ |S )NÚparentsFÚparentrœ   )r}   r   r   r¢   rm   )r<   r‰   r†   r½   Úparents_elementÚpr¦   r   r   r   r€     s    

zGEXFWriter.add_parentsc             C   sX   |  dd¡}|rTtdƒ}x0|D ](\}}tdt|ƒt|ƒd}| |¡ qW | |¡ |S )NÚslicesFÚslice)ry   rz   )r}   r   r   rm   )r<   Únode_or_edge_elementÚnode_or_edge_datarÁ   Úslices_elementry   rz   r¦   r   r   r   r   %  s    
zGEXFWriter.add_slicesc             C   sŠ   |  dd¡}|r†tdƒ}xb|D ]Z\}}tdƒ}|d k	rNt|ƒ|jd< |  |¡ |d k	rnt|ƒ|jd< |  |¡ | |¡ qW | |¡ |S )NÚspellsFZspellry   rz   )r}   r   r   r¢   r   rm   )r<   rÃ   rÄ   rÆ   Úspells_elementry   rz   r¦   r   r   r   r‚   /  s    


zGEXFWriter.add_spellsc             C   sr   | j  d¡dkrn|d k	rnt|tƒr(d}n*t|tƒr8d}nt|tƒrHd}n
t d¡‚| j  d|¡ | j  dd¡ d S )	Nr   rT   Údater+   r.   z2timeformat should be of the type int, float or strÚ
timeformatrS   )	ri   r9   r    r   r*   rC   r:   r;   Úset)r<   Zstart_or_endrÉ   r   r   r   r   ?  s    


z&GEXFWriter.alter_graph_mode_timeformatc             C   s2   | j r|  | j¡ t| jƒ}|j|| jdd d S )NT)r   Zxml_declaration)r   r_   rW   r   r   r   )r<   ZfhZdocumentr   r   r   r   O  s    
zGEXFWriter.writer   c             C   s˜   dd|  }t |ƒrz|jr$|j ¡ s.|d |_|jr>|j ¡ sD||_x|D ]}|  ||d ¡ qJW |jrr|j ¡ s”||_n|r”|jrŽ|j ¡ s”||_d S )NÚ
z  r   )rž   rr   ÚstripÚtailr_   )r<   ÚelemÚlevelÚir   r   r   r_   V  s    

zGEXFWriter.indent)NrN   TrO   )r   )r?   r@   rA   r^   rb   r   rj   rk   rl   r„   r¡   rƒ   r€   r   r‚   r   r   r_   r   r   r   r   r   ÷   s     
 (:. .
r   c               @   sp   e Zd Zddd„Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )r!   Nú1.2draftc             C   sD   ydd l }W n tk
r(   tdƒ‚Y nX || _d| _|  |¡ d S )Nr   z1GEXF reader requires xml.elementtree.ElementTree.T)rU   rV   r    Úsimple_graphr>   )r<   r    r   rW   r   r   r   r^   j  s    zGEXFReader.__init__c             C   s|   t |d| _| j d| j ¡}|d k	r0|  |¡S x<| jD ]2}|  |¡ | j d| j ¡}|d k	r8|  |¡S q8W t d¡‚d S )N)Úfilez	{%s}graphz No <graph> element in GEXF file.)	r   rW   Úfindr%   Ú
make_graphr8   r>   r:   r;   )r<   Ústreamr°   r   r   r   r   Ú__call__u  s    

zGEXFReader.__call__c             C   sR  |  dd ¡}|dkrt ¡ }nt ¡ }|  dd¡}|dkrD||jd< |  d¡}|d k	r`||jd< |  d¡}|d k	r|||jd< |  dd¡}|dkrœd|jd< n
d	|jd< |  d
¡| _| jdkrÂd| _| d| j ¡}i }	i }
i }i }xˆ|D ]€}|  d¡}|dkr.|  |¡\}}|	 	|¡ |
 	|¡ |
|jd< qè|dkrf|  |¡\}}| 	|¡ | 	|¡ ||jd< qè‚ qèW ddd	ddœi}i }| 	|¡ | 	|¡ ||jd< | 
d| j ¡}|d k	râx(| d| j ¡D ]}|  |||	¡ qÊW | 
d| j ¡}|d k	r&x(| d| j ¡D ]}|  |||¡ qW | jrN| ¡ rDt |¡}n
t |¡}|S )Nrg   rc   re   rf   ry   rz   r   rS   rT   rÉ   rÈ   r-   z{%s}attributesrª   rQ   r{   rR   r—   r“   r+   )r”   r   r§   z	{%s}nodesz{%s}nodez	{%s}edgesz{%s}edge)r9   r:   ZMultiDiGraphZ
MultiGraphr]   rÉ   r«   r%   Úfind_gexf_attributesr   rÔ   Úadd_nodeÚadd_edgerÒ   rh   ZDiGraphZGraph)r<   Z	graph_xmlZedgedefaultr   Z
graph_nameZgraph_startZ	graph_endZ
graph_modeZattributes_elementsÚ	node_attrr{   Ú	edge_attrr—   r1   Z
attr_classZnaZndZeaZedr…   Únode_xmlr˜   Zedge_xmlr   r   r   rÕ   ‚  sp    






















zGEXFReader.make_graphc       	      C   sö   |   ||¡}|  ||¡}| jdkr0|  ||¡}n|  ||¡}|  ||¡}|  ||¡}| d¡}| jd k	rr|  |¡}| d¡}||d< | d|¡}|d k	r ||d< | 	d| j
 ¡}|d k	räx*| d| j
 ¡D ]}| j||||d qÊW |j|f|Ž d S )Nz1.1rv   rw   rx   z	{%s}nodesz{%s}node)Únode_pid)Údecode_attr_elementsr€   r   r   r‚   rƒ   Úadd_start_endr9   r    rÔ   r%   r«   rÙ   )	r<   r   rÝ   rÛ   rÞ   ru   r‡   Z
node_labelZsubnodesr   r   r   rÙ   Ñ  s(    




zGEXFReader.add_nodec             C   sR   | j }| d¡}|d k	r*| j| |ƒ|d< | d¡}|d k	rN| j| |ƒ|d< |S )Nry   rz   )rÉ   r9   rL   )r<   ru   rW   ÚttypeZ
node_startZnode_endr   r   r   rà   ô  s    

zGEXFReader.add_start_endc       	      C   s’  i }|  d| j ¡}|d k	r’| jdkrVt| d¡ƒt| d¡ƒt| d¡ƒdœ|d< n<t| d¡ƒt| d¡ƒt| d¡ƒt| dd	¡ƒd
œ|d< |  d| j ¡}|d k	r¼t| d¡ƒ|d< |  d| j ¡}|d k	ræt| d¡ƒ|d< |  d| j ¡}|d k	r*| d¡|d< |d dkr*| d¡|d< |  d| j ¡}|d k	rxt| dd¡ƒt| dd¡ƒt| dd¡ƒdœ|d< t|ƒdkrŽ||d< |S )Nz	{%s}colorz1.1r¯   r°   r±   )r¯   r°   r±   r®   r1   r   )r¯   r°   r±   r1   z{%s}sizer   r²   z{%s}thicknessr³   z	{%s}shaper´   rµ   r¶   z{%s}positionr¸   r   r¹   rº   )r¸   r¹   rº   r·   rP   )rÔ   r&   r)   rC   r9   r*   rž   )	r<   ru   rÝ   rP   r®   r²   r³   r´   r·   r   r   r   rƒ   ÿ  s<    


zGEXFReader.add_vizc             C   sV   |  d| j ¡}|d k	rRg |d< x0| d| j ¡D ]}| d¡}|d  |¡ q2W |S )Nz{%s}parentsr½   z
{%s}parentrœ   )rÔ   r%   r«   r9   rm   )r<   ru   rÝ   r¿   rÀ   r¾   r   r   r   r€   &  s    
zGEXFReader.add_parentsc             C   sd   |  d| j ¡}|d k	r`g |d< x>| d| j ¡D ]*}| d¡}| d¡}|d  ||f¡ q2W |S )Nz
{%s}slicesrÁ   z	{%s}slicery   rz   )rÔ   r%   r«   r9   rm   )r<   ru   Únode_or_edge_xmlrÅ   ra   ry   rz   r   r   r   r   /  s    

zGEXFReader.add_slicesc             C   s~   |  d| j ¡}|d k	rzg |d< | j}xR| d| j ¡D ]>}| j| | d¡ƒ}| j| | d¡ƒ}|d  ||f¡ q8W |S )Nz
{%s}spellsrÆ   z	{%s}spellry   rz   )rÔ   r%   rÉ   r«   rL   r9   rm   )r<   ru   râ   rÇ   rá   ra   ry   rz   r   r   r   r‚   9  s    zGEXFReader.add_spellsc             C   sj  |  d¡}| ¡ r$|dkr$t d¡‚| ¡ s>|dkr>t d¡‚|  d¡}|  d¡}| jd k	rp|  |¡}|  |¡}|  ||¡}|  ||¡}| jdkr |  ||¡}n|  	||¡}|  d	¡}|d k	rÆ||d	< | 
d
d ¡}	|	d k	rÞ|	}|  d¡}
|
d k	rüt|
ƒ|d< |  d¡}|d k	r||d< | ||¡r,d| _|j||fd|i|—Ž |dkrf|j||fd|i|—Ž d S )Nr”   rd   z(Undirected edge found in directed graph.rc   z(Directed edge found in undirected graph.r•   r–   z1.1rv   r›   r“   rw   Fr‹   Zmutual)r9   rh   r:   r;   r    rß   rà   r   r   r‚   r}   r*   Zhas_edgerÒ   rÚ   )r<   r   r™   rÜ   Zedge_directionr•   r–   ru   rZ   Zmultigraph_keyr“   Z
edge_labelr   r   r   rÚ   D  sD    











zGEXFReader.add_edgec          	   C   s.  i }|  d| j ¡}|d k	r*x| d| j ¡D ]ô}| d¡}y|| d }W n" tk
rr   t d| ¡‚Y nX || d }| d¡}	|dkrž| j|	 }	n| j| |	ƒ}	|| d	 d
kr| j	}
| j|
 | d¡ƒ}| j|
 | d¡ƒ}||kr||  
|	||f¡ n|	||fg||< q2|	||< q2W |S )Nz{%s}attvaluesz{%s}attvaluerœ   r§   zNo attribute defined for=%s.r”   r   r,   r   rS   ry   rz   )rÔ   r%   r«   r9   r~   r:   r;   rM   rL   rÉ   rm   )r<   Z	gexf_keysZobj_xmlr\   Zattr_elementr1   r‹   r§   Úatyper   rá   ry   rz   r   r   r   rß   z  s.    



zGEXFReader.decode_attr_elementsc             C   s¨   i }i }|  d¡}xŒ| d| j ¡D ]x}|  d¡}|  d¡}|  d¡}|||dœ||< | d| j ¡}	|	d k	r$|dkr„| j|	j }
n| j| |	jƒ}
|
||< q$W ||fS )	Nr   z{%s}attributerv   r§   r”   )r§   r”   r   z{%s}defaultr,   )r9   r«   r%   rÔ   rM   rr   rL   )r<   r­   ZattrsÚdefaultsr   r¤   r[   r§   rã   rn   r   r   r   r   rØ   œ  s    



zGEXFReader.find_gexf_attributes)NrÑ   )N)r?   r@   rA   r^   r×   rÕ   rÙ   rà   rƒ   r€   r   r‚   rÚ   rß   rØ   r   r   r   r   r!   g  s   
O
#'	
6"r!   c                s
  y‡ fdd„ˆ D ƒ‰W n t k
r4   t d¡‚Y nX tˆŽ \}}tt|ƒƒtˆ ƒkr`t d¡‚tˆƒ‰t ˆ ˆ¡}xˆ D ]ˆ}ˆ| }||j| d< |j|  	d¡ d|j| krÎˆˆ j| d  |j| d< d|j| krz‡fd	d„ˆ j| d D ƒ|j| d< qzW |S )
a  Relabel graph using "label" node keyword for node label.

    Parameters
    ----------
    G : graph
       A NetworkX graph read from GEXF data

    Returns
    -------
    H : graph
      A NetworkX graph with relabed nodes

    Raises
    ------
    NetworkXError
        If node labels are missing or not unique while relabel=True.

    Notes
    -----
    This function relabels the nodes in a NetworkX graph with the
    "label" attribute.  It also handles relabeling the specific GEXF
    node attributes "parents", and "pid".
    c                s   g | ]}|ˆ j | d  f‘qS )rw   )rt   )r0   r   )r   r   r   ú
<listcomp>Ë  s    z&relabel_gexf_graph.<locals>.<listcomp>zFFailed to relabel nodes: missing node labels found. Use relabel=False.zHFailed to relabel nodes: duplicate node labels found. Use relabel=False.rv   rw   rx   r½   c                s   g | ]}ˆ | ‘qS r   r   )r0   rÀ   )Úmappingr   r   rå   ß  s    )
r~   r:   r;   Úziprž   rÊ   rF   Zrelabel_nodesrt   r}   )r   r¸   r¹   ÚHÚnÚmr   )r   ræ   r   r
   ±  s$    

*c             C   s2   ddl m} ydd l}W n   |dƒ‚Y nX d S )Nr   )ÚSkipTestz%xml.etree.cElementTree not available.)Znoserë   Úxml.etree.cElementTree)Úmodulerë   rW   r   r   r   Úsetup_moduleä  s
    rî   c             C   s(   dd l }y| d¡ W n   Y nX d S )Nr   z	test.gexf)ÚosÚunlink)rí   rï   r   r   r   Úteardown_moduleí  s
    rñ   )r   Tr   )r   Tr   )NFr   )Ú__doc__rX   rs   Znetworkxr:   Znetworkx.utilsr   r   rì   r   r   r   r   rV   rU   Ú__all__r   r   r	   Úobjectr#   r   r!   r
   rî   rñ   r   r   r   r   Ú<module>   s6   
5
/
+@  r  L3	