
 ,[c           @   s:  d  Z  d d l Z d d l m Z y0 d d l m Z m Z d d l m Z m Z WnU e	 k
 r y0 d d l
 m Z m Z d d l
 m Z m Z Wq e	 k
 r q Xn Xy d d l j Z Wn e	 k
 r d Z n Xd d l Z d d l m Z m Z d d d	 d
 d d d d g Z e d d d d e e d   Z e d d d d e e d   Z d e d  Z e d d d e e d   Z e d  Z d e f d     YZ d e f d     YZ  d e f d     YZ! d e  f d      YZ" e d k re Z# n e Z# d e f d!     YZ$ d"   Z% d#   Z& d S($   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(   t   Elementt   ElementTree(   t   tostringt
   fromstring(   t	   open_filet   make_strt   write_graphmlt   read_graphmlt   generate_graphmlt   write_graphml_xmlt   write_graphml_lxmlt   parse_graphmlt   GraphMLWritert   GraphMLReaderi   t   modet   wbs   utf-8c         C   s9   t  d | d | d |  } | j |   | j |  d S(   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.
    t   encodingt   prettyprintt   infer_numeric_typesN(   R   t   add_graph_elementt   dump(   t   Gt   pathR   R   R   t   writer(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR
   H   s    #	c      
   C   s2   t  | d |  d | d | d | } | j   d S(   s  Write 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.
    t   graphR   R   R   N(   t   GraphMLWriterLxmlR   (   R   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR   q   s    "	c         c   sH   t  d | d |  } | j |   x t |  j   D] } | Vq5 Wd S(   s  Generate 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.
    R   R   N(   R   R   t   strt
   splitlines(   R   R   R   R   t   line(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR	      s    i    t   rbc         C   s   t  d | d |  } t | d |    } t |  d k r d } |  j d  |  j   } | j d |  } t | d |   } t |  d k r t j d   q n  | d S(	   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_typeR   i    s7   <graphml xmlns="http://graphml.graphdrawing.org/xmlns">s	   <graphml>t   strings%   file not successfully read as graphml(   R   t   listt   lent   seekt   readt   replacet   nxt   NetworkXError(   R   R   R    t   readert   glistt   headert	   old_bytest	   new_bytes(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR      s    5c         C   s   t  d |  } t | d |    } t |  d k r d } |  j d |  } t | d |   } t |  d k r t j d   q n  | d S(   s  Read 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.

    R   R!   i    s7   <graphml xmlns="http://graphml.graphdrawing.org/xmlns">s	   <graphml>s%   file not successfully read as graphml(   R   R"   R#   R&   R'   R(   (   t   graphml_stringR   R)   R*   R+   t
   new_string(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR      s    1t   GraphMLc           B   s  e  Z d  Z d Z d Z d j d  d g  Z y e d  e Z	 e
 Z Wn e k
 rZ n Xe
 d f e d f e d f e	 d f e
 d	 f e d
 f e d f e d f e d f g	 Z y d d l Z Wn n Xe j d f e j d f e j d f e j d f e j
 d	 f e j d	 f e j d	 f e j d	 f e j d	 f e j d	 f e j d	 f e j d	 f e j d	 f e j d	 f e j d	 f e j  d	 f g e Z e! e  Z" e! d   e D  Z# i e$ d 6e% d 6e% d 6e% d 6e$ d 6e$ d 6Z& RS(   s%   http://graphml.graphdrawing.org/xmlnss)   http://www.w3.org/2001/XMLSchema-instances!   http://www.yworks.com/xml/graphmlt    s5   http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsdi90  t   integert   yfilesR!   t   intt   longt   floatt   doublet   booleaniNc         c   s   |  ] } t  |  Vq d  S(   N(   t   reversed(   t   .0t   a(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pys	   <genexpr>c  s    t   truet   falset   0i    t   1i   ('   t   __name__t
   __module__t
   NS_GRAPHMLt   NS_XSIt   NS_Yt   joint   SCHEMALOCATIONt   chrR   t   unicodeR4   R5   t
   ValueErrorR6   t   boolt   typest   numpyt   npt   float64t   float32t   float16t   float_t   int8t   int16t   int32t   int64t   uint8t   uint16t   uint32t   uint64t   int_t   intct   intpt   dictt   xml_typet   python_typet   Truet   Falset   convert_bool(    (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR0   :  sF   	

	c           B   s   e  Z d d  e e d  Z d   Z d   Z d   Z d d d  Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z d d  Z RS(   s   utf-8c         C   s   y d d  l  } Wn# t k
 r5 d } t |   n Xt |  _ | |  _ | |  _ | |  _ |  j d i |  j d 6|  j d 6|  j	 d 6 |  _
 i  |  _ t t  |  _ t t  |  _ | d  k	 r |  j |  n  d  S(   Nis3   GraphML writer requires xml.elementtree.ElementTreet   graphmlt   xmlnss	   xmlns:xsis   xsi:schemaLocation(   t   xml.etree.ElementTreet   ImportErrorR   t	   myElementR   R   R   RB   RC   RF   t   xmlt   keysR    R"   t
   attributest   sett   attribute_typest   NoneR   (   t   selfR   R   R   R   Rh   t   msg(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt   __init__s  s$    					
	c         C   s;   |  j  r |  j |  j  n  t |  j  j |  j  } | S(   N(   R   t   indentRh   R   t   decodeR   (   Rn   t   s(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt   __str__  s    	c         C   s   |  j  r |  j | | f } y t d  t } t } Wn t k
 rU t } t } n Xt |  d k r t | k rx t S| | k r | St	 | k r t	 S| | k r | St Sq t
 |  d Sn
 t |  Sd S(   s  Infer 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'.
        i90  i   i    N(   R   Rl   RG   R4   R   RI   R5   RH   R#   R6   R"   t   type(   Rn   t   namet   scopet   valueRK   t
   local_longt   local_unicode(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt	   attr_type  s*    		


c   
      C   s   | | | f } y |  j  | SWn t k
 r d t t |  j    } | |  j  | <i | d 6| d 6| d 6| d 6} |  j d |  } | d  k	 r |  j d  }	 t |  |	 _ | j |	  n  |  j	 j
 d |  n X| S(	   Ns   d%it   idt   fors	   attr.names	   attr.typet   keyt   defaulti    (   Ri   t   KeyErrorR#   R"   Rg   Rm   R   t   textt   appendRh   t   insert(
   Rn   Rv   R{   Rw   R   t   keys_keyt   new_idt
   key_kwargst   key_elementt   default_element(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt   get_key  s"    

t   allc   	      C   sr   | |  j  k r+ d } t j | |   n  |  j | |  j  | | |  } |  j d d | } t |  | _ | S(   sn   
        Make a data element for an edge or a node. Keep a log of the
        type in the keys table.
        s2   GraphML writer does not support %s as data values.t   dataR~   (   R^   R'   R(   R   Rg   R   R   (	   Rn   Rv   t   element_typeRx   Rw   R   Ro   t   keyidt   data_element(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt   add_data  s    c         C   sp   xi | j    D][ \ } } |  j t |  | f j t |   |  j | j | | | | j |  g  q Wd S(   s   Appends attribute data to edges or nodes, and stores type information
        to be added later. See add_graph_element.
        N(   t   itemsRl   R   t   addRu   Rj   R   t   get(   Rn   Rw   t   xml_objR   R   t   kt   v(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt   add_attributes  s    &c         C   sz   | j  j d i   } x^ | j d t  D]J \ } } |  j d d t |  } |  j d | | |  | j |  q( Wd  S(   Nt   node_defaultR   t   nodeR|   (   R   R   t   nodesR`   Rg   R   R   R   (   Rn   R   t   graph_elementR   R   R   t   node_element(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt	   add_nodes  s
    c   	   
   C   s2  | j    r x| j d t d t  D]} \ } } } } |  j d d t |  d t |  d t |  } | j j d i   } |  j d | | |  | j |  q% Wn x | j d t  D]n \ } } } |  j d d t |  d t |  } | j j d i   } |  j d | | |  | j |  q Wd  S(   NR   Ri   t   edget   sourcet   targetR|   t   edge_default(	   t   is_multigrapht   edgesR`   Rg   R   R   R   R   R   (	   Rn   R   R   t   uR   R~   R   t   edge_elementR   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt	   add_edges  s    +"c      	   C   sR  | j    r d } n d } | j j d d  } | d k rT |  j d d | } n |  j d d | d | } i  } d   | j j   D } |  j d | | |  |  j | |  |  j | |  xw |  j	 j   D]f \ } } xW | D]O \ } }	 }
 } | j
 |  j t |  |  j | |
 |	  t |	  |
 |   q Wq W|  j j
 |  d S(   s=   
        Serialize graph G in GraphML to the stream.
        t   directedt
   undirectedR|   R   t   edgedefaultc         S   s+   i  |  ]! \ } } | d k r | |  q S(   R   R   (   R   R   (    (   R:   R   R   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pys
   <dictcomp>
  s   	 	N(   t   is_directedR   t   popRm   Rg   R   R   R   R   Rj   R   R   R   R{   Rh   (   Rn   R   t   default_edge_typet   graphidR   R   R   R   R   R   Rw   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR     s*    		c         C   s"   x | D] } |  j  |  q Wd S(   s+    Add many graphs to this GraphML document. N(   R   (   Rn   t
   graph_listR   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt
   add_graphs  s    c         C   sK   |  j  r |  j |  j  n  t |  j  } | j | d |  j d t d  S(   NR   t   xml_declaration(   R   Rq   Rh   R   t   writeR   R`   (   Rn   t   streamt   document(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR      s    	i    c         C   s   d | d } t  |  r | j s4 | j j   rD | d | _ n  | j s^ | j j   rj | | _ n  x" | D] } |  j | | d  qq W| j s | j j   r | | _ q n, | r | j s | j j   r | | _ n  d  S(   Ns   
s     i   (   R#   R   t   stript   tailRq   (   Rn   t   elemt   levelt   i(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyRq   &  s     N(   R@   RA   Rm   R`   Ra   Rp   Rt   R{   R   R   R   R   R   R   R   R   Rq   (    (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR   r  s   			%					#		t   IncrementalElementc           B   s    e  Z d  Z d   Z d   Z RS(   s   Wrapper for _IncrementalWriter providing an Element like interface.

    This wrapper does not intend to be a complete implementation but rather to
    deal with those calls used in GraphMLWriter.
    c         C   s   | |  _  | |  _ d  S(   N(   Rh   R   (   Rn   Rh   R   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyRp   >  s    	c         C   s   |  j  j | d |  j d  S(   Nt   pretty_print(   Rh   R   R   (   Rn   t   element(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR   B  s    (   R@   RA   t   __doc__Rp   R   (    (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR   7  s   	R   c           B   sA   e  Z d d  e e d  Z d   Z d   Z d   Z d   Z	 RS(   s   utf-8c         C   s   t  j |  _ | |  _ | |  _ | |  _ t  j | d | |  _ |  j j   |  _	 |  j	 j
   g  |  _ |  j |  _ |  j	 j d i |  j d 6|  j d 6|  j d 6 |  _ |  j j   i  |  _ t t  |  _ | d  k	 r |  j |  n  d  S(   NR   Rc   Rd   s	   xmlns:xsis   xsi:schemaLocation(   t	   lxmletreeR   Rg   t	   _encodingt   _prettyprintR   t   xmlfilet	   _xml_baset	   __enter__t   _xmlt   write_declarationRh   t   _keysR   RB   RC   RF   t   _graphmlRi   R    Rk   Rl   Rm   R   (   Rn   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyRp   G  s(    					

	c      	   C   s  | j    r d } n d } | j j d d  } | d k rW |  j j d d | } n |  j j d d | d | } d   | j j   D } | j j d i   } | j j d i   } x@ | j   D]2 \ } }	 |  j t	 |  d f j
 t |	   q WxR | j   D]D \ } }	 |  j |  j | d |	  }
 |  j t	 |  |
 d d  qWi  } x| | j d	 t  D]h \ } } xY | j   D]K \ } }	 |  j t	 |  d
 f j
 t |	   | | k r|	 | | <qqWqiWx[ | j   D]M \ } }	 |  j |  j | d
 |	  } |  j t	 |  | d
 | j |   qW| j   r1i  } x | j d t d	 t  D]n \ } }	 } } xY | j   D]K \ } }	 |  j t	 |  d f j
 t |	   | | k r}|	 | | <q}q}Wq^WxD| j   D]M \ } }	 |  j |  j | d |	  } |  j t	 |  | d | j |   qWn i  } x | j d	 t  D]k \ } }	 } xY | j   D]K \ } }	 |  j t	 |  d f j
 t |	   | | k rf|	 | | <qfqfWqJWx[ | j   D]M \ } }	 |  j |  j | d |	  } |  j t	 |  | d | j |   qWx* |  j D] } |  j j | d |  j q!Wt |  j |  j  } | ; |  j d | | i   |  j | |  |  j | |  Wd QXd S(   s=   
        Serialize graph G in GraphML to the stream.
        R   R   R|   R   R   c         S   s+   i  |  ]! \ } } | d k r | |  q S(   R   R   (   R   R   (    (   R:   R   R   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pys
   <dictcomp>|  s   	 	R   R   R   R   Ri   R   R   N(   R   R   R   Rm   R   R   R   R   Rl   R   R   Ru   R^   R{   R   R   R`   R   R   Rh   R   R   R   R   R   R   (   Rn   R   R   R   R   t	   graphdataR   R   R   R   R   Rj   R   t   dt   TR   t   ekeyR~   t   incremental_writer(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR   g  sj    		* &)+&,"&)c         C   ss   xl | j    D]^ \ } } |  j t |  |  j t |  | |  t |  | | j |   } | j |  q Wd S(   s   Appends attribute data.N(   R   R   R   R{   R   R   (   Rn   Rw   R   R   R   R   R   R   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR     s
    c         C   s   t  j |   S(   N(   t   objectRt   (   Rn   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyRt     s    c         C   s0   |  j  j d  d  d   |  j j d  d  d   d  S(   N(   R   t   __exit__Rm   R   (   Rn   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR     s    N(
   R@   RA   Rm   R`   Ra   Rp   R   R   Rt   R   (    (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR   F  s   		J		c           B   s\   e  Z d  Z e e d  Z d d d  Z d d  Z d   Z	 d   Z
 d   Z d   Z RS(	   s:   Read a GraphML document.  Produces NetworkX graph objects.c         C   s^   y d d  l  } Wn# t k
 r5 d } t |   n X| |  _ | |  _ t |  _ i  |  _ d  S(   Nis3   GraphML reader requires xml.elementtree.ElementTree(   Re   Rf   R   R    Ra   t
   multigrapht   edge_ids(   Rn   R   R    Rh   Ro   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyRp     s    			c         c   s   | d  k	 r! t d |  |  _ n* | d  k	 r? t |  |  _ n t d   |  j |  j  \ } } x5 |  j j d |  j  D] } |  j | | |  Vq} Wd  S(   Nt   files/   Must specify either 'path' or 'string' as kwargs	   {%s}graph(	   Rm   R   Rh   R   RI   t   find_graphml_keyst   findallRB   t
   make_graph(   Rn   R   R!   Ri   t   defaultst   g(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt   __call__  s     c         C   s#  | j  d d   } | d  k rH | d k r9 t j   } qH t j   } n  i  | j d <i  | j d <x | j   D] \ } } | | d } | | d }	 | | d }
 | d k r | j d j i |
 |  |	 6 n  | d	 k ro | j d j i |
 |  |	 6 qo qo W| j d
 |  j	  } | d  k	 r=t j
 d   n  x4 | j d |  j	  D] } |  j | | | |  qTWx1 | j d |  j	  D] } |  j | | |  qW|  j | |  } | j j |  |  j s| j   rt j |  } n t j |  } t j | d |  j d d n  | S(   NR   R   R   R   R}   Rv   Ru   R   R   s   {%s}hyperedges)   GraphML reader doesn't support hyperedgess   {%s}nodes   {%s}edget   valuesR|   (   R   Rm   R'   t   MultiDiGrapht
   MultiGraphR   R   t   updatet   findRB   R(   R   t   add_nodet   add_edget   decode_data_elementsR   R   t   DiGrapht   Grapht   set_edge_attributesR   (   Rn   t	   graph_xmlt   graphml_keysR   R   R   t   key_idRx   t   key_forRv   R_   t	   hyperedget   node_xmlt   edge_xmlR   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR     s<    $(	c   	      C   s   | j  d |  j  } | d k	 r2 t j d  n  |  j | j d   } |  j | |  } | j | |  | j	 j d  d k r | j  d |  j  } |  j
 | | | |  n  d S(   s!   Add a node to the graph.
        s   {%s}ports   GraphML port tag not supported.R|   s   yfiles.foldertypet   groups	   {%s}graphN(   R   RB   Rm   t   warningst   warnR   R   R   R   t   attribR   (	   Rn   R   R   R   R   t   portst   node_idR   R   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR     s    c         C   s  | j  d |  j  } | d k	 r2 t j d  n  | j d  } | j   rq | d k rq d } t j |   n  | j   r | d k r d } t j |   n  |  j	 | j d   } |  j	 | j d	   } |  j
 | |  }	 | j d
  }
 |
 r6|
 |  j | | f <y |  j |
  }
 WqEt k
 r2qEXn |	 j d  }
 | j | |  rct |  _ n  | j | | |
 |	 f g  d S(   s"   Add an edge to the graph.
        s   {%s}ports   GraphML port tag not supported.R   R=   s,   directed=false edge found in directed graph.R<   s-   directed=true edge found in undirected graph.R   R   R|   R~   N(   R   RB   Rm   R   R   R   R   R'   R(   R   R   R   R    RI   t   has_edgeR`   R   t   add_edges_from(   Rn   R   R   R   R   R   Ro   R   R   R   t   edge_id(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR     s0    c         C   s9  i  } x,| j  d |  j  D]} | j d  } y  | | d } | | d } Wn$ t k
 rx t j d |   n X| j } | d k	 r t t	 |   d k r | t
 k r |  j | j   | | <q1| |  | | <q t t	 |   d k r d }	 x d d d	 g D] }
 d
 |  j |
 |  j f } | j d |  } | d k	 rt| j d  | d <| j d  | d <n  |	 d k r| j d |  }	 qqW|	 d k	 r|	 j | d <n  xY d d d d d g D]B } d
 |  j | |  j f } | j d |  } | d k	 rPqqW| d k	 r1| j | d <q1q q W| S(   s:   Use the key information to decode the data XML if present.s   {%s}dataR~   Rv   Ru   s   Bad GraphML data: no key %si    t	   ShapeNodet   SVGNodet	   ImageNodes   {%s}%s/{%s}s
   %sGeometryt   xt   ys   %sNodeLabelt   labelt   PolyLineEdget
   SplineEdget   QuadCurveEdget
   BezierEdget   ArcEdges   %sEdgeLabelN(   R   RB   R   R   R'   R(   R   Rm   R#   R"   RJ   Rb   t   lowerRD   R   (   Rn   R   t   obj_xmlR   R   R~   t	   data_namet	   data_typeR   t
   node_labelR   t   preft   geometryt   et
   edge_label(    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR   F  sD    	$c   
      C   s7  i  } i  } x| j  d |  j  D]} | j d  } | j d  } | j d  } | j d  } | d k	 r | } d } n  | d k r d } t j d |  n  | d k r t j d	 |   n  i | d
 6|  j | d 6| j d  d 6| | <| j	 d |  j  }	 |	 d k	 r# |	 j
 | | <q# q# W| | f S(   s=   Extracts all the keys and key defaults from the xml.
        s   {%s}keyR|   s	   attr.types	   attr.names   yfiles.typeR3   R!   s#   No key type for id %s. Using strings   Unknown key for id %s.Rv   Ru   R}   s   {%s}defaultN(   R   RB   R   Rm   R   R   R'   R(   R_   R   R   (
   Rn   R   R   t   graphml_key_defaultsR   t   attr_idR{   t	   attr_namet   yfiles_typeR   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR   t  s,    	
N(   R@   RA   R   R   R4   Rp   Rm   R   R   R   R   R   R   (    (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyR     s   +		)	.c         C   s:   d d l  m } y d d  l } Wn | d   n Xd  S(   Ni(   t   SkipTests#   xml.etree.ElementTree not available(   t   noseR	  Re   (   t   moduleR	  Rh   (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt   setup_module  s
    c         C   s+   d d  l  } y | j d  Wn n Xd  S(   Nis   test.graphml(   t   ost   unlink(   R  R  (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt   teardown_module  s
    ('   R   R   t   collectionsR    t   xml.etree.cElementTreeR   R   R   R   Rf   Re   t
   lxml.etreet   etreeR   Rm   t   networkxR'   t   networkx.utilsR   R   t   __all__R`   Ra   R
   R   R	   R   R4   R   R   R   R0   R   R   R   R   R   R  R  (    (    (    s9   lib/python2.7/site-packages/networkx/readwrite/graphml.pyt   <module>-   sP   
	'&C>8|			