σ
¦Υ\c           @` sM  d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l m Z d d l m	 Z	 d d l
 m Z m Z m Z d d l m Z m Z m Z e d d	  Z d
   Z d   Z d   Z e j d  Z e j d  Z d d  Z d   Z d d d i  d d d  Z e d d g  Z e d d g  Z d   Z  d d d  Z! d   Z" d S(   i    (   t   absolute_importt   divisiont   print_functionN(   t   partiali   (   t   apply(   t   istaskt   get_dependenciest
   ishashable(   t   funcnamet   import_requiredt	   key_splitt   graphvizsm   Drawing dask graphs requires the `graphviz` python library and the `graphviz` system library to be installed.c         C` s²   |  d } | t  k r# |  d } n  t | d  rw t | j  d k ra d j t | j d   St | j d  } n t |  } t d   |  d D  rͺ d j |  S| Sd S(   sΔ   Label for a task on a dot graph.

    Examples
    --------
    >>> from operator import add
    >>> task_label((add, 1, 2))
    'add'
    >>> task_label((add, (add, 1, 2), 3))
    'add(...)'
    i    i   t   funcss   {0}(...)c         s` s   |  ] } t  |  Vq d  S(   N(   t   has_sub_tasks(   t   .0t   i(    (    s'   lib/python2.7/site-packages/dask/dot.pys	   <genexpr>'   s    N(   R   t   hasattrt   lenR   t   formatR   t   any(   t   taskt   funct   head(    (    s'   lib/python2.7/site-packages/dask/dot.pyt
   task_label   s    
c         C` s;   t  |   r t St |  t  r3 t d   |  D  St Sd S(   s&   Returns True if the task has sub tasksc         s` s   |  ] } t  |  Vq d  S(   N(   R   (   R   R   (    (    s'   lib/python2.7/site-packages/dask/dot.pys	   <genexpr>2   s    N(   R   t   Truet
   isinstancet   listR   t   False(   R   (    (    s'   lib/python2.7/site-packages/dask/dot.pyR   -   s
    c         C` s?   y t  t |    SWn$ t k
 r: t  t t  |     SXd  S(   N(   t   strt   hasht	   TypeError(   t   x(    (    s'   lib/python2.7/site-packages/dask/dot.pyt   name7   s    s   ([0-9a-z]{32})s>   ([0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12})c         C` s·   t  |   } x€ t t f D] } t j | |  } | d k	 r xo | j   D]^ } | d k	 r | j | t |   } d j	 |  } | | | <n d } | j
 | |  } qJ Wq q W| S(   s­  

    >>> label('x')
    'x'

    >>> label(('x', 1))
    "('x', 1)"

    >>> from hashlib import md5
    >>> x = 'x-%s-hello' % md5(b'1234').hexdigest()
    >>> x
    'x-81dc9bdb52d04dc20036dbd8313ed055-hello'

    >>> label(x)
    'x-#-hello'

    >>> from uuid import uuid1
    >>> x = 'x-%s-hello' % uuid1()
    >>> x  # doctest: +SKIP
    'x-4c1a3d7e-0b45-11e6-8334-54ee75105593-hello'

    >>> label(x)
    'x-#-hello'
    s   #{0}t   #N(   R   t   _HASHPATt   _UUIDPATt   ret   searcht   Nonet   groupst   getR   R   t   replace(   R   t   cachet   st   patternt   mt   ht   nt   label(    (    s'   lib/python2.7/site-packages/dask/dot.pyR0   B   s    c         C` sI   t  |  t  rA |  d }  t |   d k r7 |  \ }  n  t |   Sd Sd S(   s’    Label boxes in graph by chunk index

    >>> box_label(('x', 1, 2, 3))
    '(1, 2, 3)'
    >>> box_label(('x', 123))
    '123'
    >>> box_label('x')
    ''
    i   t    N(   R   t   tupleR   R   (   t   key(    (    s'   lib/python2.7/site-packages/dask/dot.pyt	   box_labelk   s    


t   BTc         K` se  | d  k r i  } n  | d  k r* i  } n  | p3 i  } | | d <| j |  t j d | d | d |  } t   }	 xκ|  j   D]ά\ }
 } t |
  } | |	 k rύ |	 j |  | j |
 i   } | j	 d t
 |
   | j	 d d  | j | |  n  t |  r,t |
 d f  } | |	 k r|	 j |  | j |
 i   } | j	 d t |
   | j	 d d	  | j | |  n  | j | |  xΛ t |  |
  D] } t |  } | |	 k r|	 j |  | j | i   } | j	 d t
 |   | j	 d d  | j | |  n  | j | |  qWq t |  r | |  k r | j t |  |  q q W| S(
   Nt   rankdirt
   graph_attrt	   node_attrt	   edge_attrR0   t   shapet   boxt   functiont   circle(   R&   t   updateR   t   Digrapht   sett   itemsR    t   addR(   t
   setdefaultR4   t   nodeR   R
   t   edgeR   R   (   t   dskt   data_attributest   function_attributesR6   R7   R8   R9   t   kwargst   gt   seent   kt   vt   k_namet   attrst	   func_namet   dept   dep_name(    (    s'   lib/python2.7/site-packages/dask/dot.pyt   to_graphviz~   sP    		
		t   jpegt   pngt   dott   pdfc         C` s   d   } y d d l  j } Wn t k
 r0 | SX|  t k rA | S|  t k r` t | j d |  S|  d k rs | j St d |    d S(   s  
    Get the appropriate IPython display class for `format`.

    Returns `IPython.display.SVG` if format=='svg', otherwise
    `IPython.display.Image`.

    If IPython is not importable, return dummy function that swallows its
    arguments and returns None.
    c          _` s   d  S(   N(   R&   (   t   argsRI   (    (    s'   lib/python2.7/site-packages/dask/dot.pyt   <lambda>½   R1   i    NR   t   svgs)   Unknown format '%s' passed to `dot_graph`(	   t   IPython.displayt   displayt   ImportErrort   IPYTHON_NO_DISPLAY_FORMATSt   IPYTHON_IMAGE_FORMATSR   t   Imaget   SVGt
   ValueError(   R   t   dummyR\   (    (    s'   lib/python2.7/site-packages/dask/dot.pyt   _get_display_cls³   s    
	t   mydaskc         K` s   t  |  |  } t | | |  S(   sΧ  
    Render a task graph using dot.

    If `filename` is not None, write a file to disk with that name in the
    format specified by `format`.  `filename` should not include an extension.

    Parameters
    ----------
    dsk : dict
        The graph to display.
    filename : str or None, optional
        The name (without an extension) of the file to write to disk.  If
        `filename` is None, no file will be written, and we communicate with
        dot using only pipes.  Default is 'mydask'.
    format : {'png', 'pdf', 'dot', 'svg', 'jpeg', 'jpg'}, optional
        Format in which to write output file.  Default is 'png'.
    **kwargs
        Additional keyword arguments to forward to `to_graphviz`.

    Returns
    -------
    result : None or IPython.display.Image or IPython.display.SVG  (See below.)

    Notes
    -----
    If IPython is installed, we return an IPython.display object in the
    requested format.  If IPython is not installed, we just return None.

    We always return None if format is 'pdf' or 'dot', because IPython can't
    display these formats natively. Passing these formats with filename=None
    will not produce any useful output.

    See Also
    --------
    dask.dot.to_graphviz
    (   RS   t   graphviz_to_file(   RF   t   filenameR   RI   RJ   (    (    s'   lib/python2.7/site-packages/dask/dot.pyt	   dot_graphΡ   s    %c         ` s  d d d d d d g } | d  k rk t   f d   | D  rk t j j    \   } | d j   } n  | d  k r d	 } n  |  j d
 |  } | s§ t d   n  t |  }   sΖ | d |  Sd j	   | g  } t
 | d   } | j |  Wd  QX| d |  S(   Ns   .pngs   .pdfs   .dots   .svgs   .jpegs   .jpgc         3` s$   |  ] }   j    j |  Vq d  S(   N(   t   lowert   endswith(   R   t   fmt(   Rg   (    s'   lib/python2.7/site-packages/dask/dot.pys	   <genexpr>ό   s    i   RU   R   sΜ   Graphviz failed to properly produce an image. This probably means your installation of graphviz is missing png support. See: https://github.com/ContinuumIO/anaconda-issues/issues/485 for more information.t   datat   .t   wbRg   (   R&   R   t   ost   patht   splitextRi   t   pipet   RuntimeErrorRd   t   joint   opent   write(   RJ   Rg   R   t   fmtsRl   t   display_clst   full_filenamet   f(    (   Rg   s'   lib/python2.7/site-packages/dask/dot.pyRf   ϊ   s     (	(#   t
   __future__R    R   R   R$   Ro   t	   functoolsR   t   compatibilityR   t   coreR   R   R   t   utilsR   R	   R
   R   R   R   R    t   compileR"   R#   R&   R0   R4   RS   t	   frozensetR_   R^   Rd   Rh   Rf   (    (    (    s'   lib/python2.7/site-packages/dask/dot.pyt   <module>   s*   		
	)	0	)