
JOc           @   s  d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k	 Z	 d d k
 Z
 d d k Z d d k Z e i d e d d d   Z d d k l Z d d k l Z d	   Z e  e _  d
   Z d   Z d   Z h e i d 6e i d 6e i d 6e i d 6e d 6e i d 6e d 6e d 6Z d d k l Z l Z y d d k Z d   Z Wn( e  j
 o d d k! Z! d   Z n Xd Z" d e# f d     YZ$ d   Z% d d k& Z& e& i' d  d d k( i) Z* d d k+ i, Z, d d k& l- Z- d d k. Z. d   Z/ d   Z0 d    Z1 d! e2 f d"     YZ3 d d#  Z5 d$   Z6 d%   Z7 y d d& k8 l9 Z9 Wnk e  j
 o_ d' e i: j o e i; i< d(  Z9 qd) e i: j o e i; i< d*  Z9 qe2 d+   n Xd S(,   s  
A special directive for generating a matplotlib plot.

.. warning::

   This is a hacked version of plot_directive.py from Matplotlib.
   It's very much subject to change!


Usage
-----

Can be used like this::

    .. plot:: examples/example.py

    .. plot::

       import matplotlib.pyplot as plt
       plt.plot([1,2,3], [4,5,6])

    .. plot::

       A plotting example:

       >>> import matplotlib.pyplot as plt
       >>> plt.plot([1,2,3], [4,5,6])

The content is interpreted as doctest formatted if it has a line starting
with ``>>>``.

The ``plot`` directive supports the options

    format : {'python', 'doctest'}
        Specify the format of the input

    include-source : bool
        Whether to display the source code. Default can be changed in conf.py
    
and the ``image`` directive options ``alt``, ``height``, ``width``,
``scale``, ``align``, ``class``.

Configuration options
---------------------

The plot directive has the following configuration options:

    plot_include_source
        Default value for the include-source option

    plot_pre_code
        Code that should be executed before each plot.

    plot_basedir
        Base directory, to which plot:: file names are relative to.
        (If None or empty, file names are relative to the directoly where
        the file containing the directive is.)

    plot_formats
        File formats to generate. List of tuples or strings::

            [(suffix, dpi), suffix, ...]

        that determine the file format and the DPI. For entries whose
        DPI was omitted, sensible defaults are chosen.

    plot_html_show_formats
        Whether to show links to the files in HTML.

TODO
----

* Refactor Latex output; now it's plain images, but it would be nice
  to make them appear side-by-side, or in floats.

iNs   A plot_directive module is also available under matplotlib.sphinxext; expect this numpydoc.plot_directive module to be deprecated after relevant features have been integrated there.t
   stackleveli   c         C   s   |  t  _ |  i t  _ |  i t  _ |  i d d t  |  i d t t  |  i d d d d g t  |  i d d  t  |  i d	 t t  |  i d
 t	 t d d t f t
  d  S(   Nt   plot_pre_codet    t   plot_include_sourcet   plot_formatst   pngs	   hires.pngt   pdft   plot_basedirt   plot_html_show_formatst   ploti    i   (   t   setupt   appt   configt   confdirt   add_config_valuet   Truet   Falset   Nonet   add_directivet   plot_directivet   plot_directive_options(   R   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyR
   ]   s    	(   t
   directives(   t   nodesc	   	      C   s   t  | | | | | |  S(   N(   t   run(	   t   namet	   argumentst   optionst   contentt   linenot   content_offsett
   block_textt   statet   state_machine(    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyR   q   s    c         C   sk   |  p |  i    o t S|  i    i   d j o t S|  i    i   d	 j o t St d |    d  S(
   Nt   not   0t   falset   yest   1t   trues   "%s" unknown boolean(   s   noR"   s   false(   s   yesR%   s   true(   t   stripR   t   lowerR   t
   ValueError(   t   arg(    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   _option_booleanv   s    c         C   s   t  i |  d  S(   Nt   pythont   lisp(   s   pythonR-   (   R   t   choice(   R*   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   _option_format   s    c         C   s   t  i |  d  S(   Nt   topt   middlet   bottomt   leftt   centert   right(   s   tops   middles   bottoms   lefts   centers   right(   R   R.   (   R*   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   _option_align   s    	t   altt   heightt   widtht   scalet   alignt   classs   include-sourcet   format(   R   t   utilsc         K   s   t  i |   i |   S(   N(   t   jinja2t   Templatet   render(   t   templatet   kw(    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   format_template   s    c         K   s   t  i |  |  S(   N(   t   jinjat   from_string(   RB   RC   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyRD      s    s8  
{{ source_code }}

{{ only_html }}

   {% if source_link or (html_show_formats and not multi_image) %}
   (
   {%- if source_link -%}
   `Source code <{{ source_link }}>`__
   {%- endif -%}
   {%- if html_show_formats and not multi_image -%}
     {%- for img in images -%}
       {%- for fmt in img.formats -%}
         {%- if source_link or not loop.first -%}, {% endif -%}
         `{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
       {%- endfor -%}
     {%- endfor -%}
   {%- endif -%}
   )
   {% endif %}

   {% for img in images %}
   .. figure:: {{ build_dir }}/{{ img.basename }}.png
      {%- for option in options %}
      {{ option }}
      {% endfor %}

      {% if html_show_formats and multi_image -%}
        (
        {%- for fmt in img.formats -%}
        {%- if not loop.first -%}, {% endif -%}
        `{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
        {%- endfor -%}
        )
      {%- endif -%}
   {% endfor %}

{{ only_latex }}

   {% for img in images %}
   .. image:: {{ build_dir }}/{{ img.basename }}.pdf
   {% endfor %}

t	   ImageFilec           B   s#   e  Z d    Z d   Z d   Z RS(   c         C   s   | |  _  | |  _ g  |  _ d  S(   N(   t   basenamet   dirnamet   formats(   t   selfRH   RI   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   __init__   s    		c         C   s#   t  i i |  i d |  i | f  S(   Ns   %s.%s(   t   ost   patht   joinRI   RH   (   RK   R=   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   filename   s    c         C   s+   g  } |  i  D] } | |  i |  q ~ S(   N(   RJ   RP   (   RK   t   _[1]t   fmt(    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt	   filenames   s    (   t   __name__t
   __module__RL   RP   RS   (    (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyRG      s   		c   3      C   sr  |  o | o t  d   n | i } | i i i } | i d | i  | i d } t i	 i
 |  }	 |  o | i p& t i	 i |	 t i |  d   }
 n, t i	 i t i | i t i |  d   }
 t |
 d  i   } t i	 i |
  } n | }
 t i d i t t |    } | i i d d  d } | | i d <t i	 i t i	 i |
   \ } } d	 | | f } t i	 i |  \ } } | d- j o
 | } n d } | i d d  } t |  } | i d  o% | d d j o
 t } qt } n t |
 t i  } t i	 i
 |  } x% | i  t i	 i!  o | d } q%Wt i	 i t i	 i
 t i" i#  d |  } t i	 i$ |  p t i% |  n t i	 i& t i	 i t i" i' i( |   } t i	 i t t i |	  |  i t i	 i! d  } t | |	  i t i	 i! d  } | d | | } y" t) | |
 | | |  } g  } Wn[ t* j
 oO } | i+ i, } | i- d d | | f d | } | g  f g } | g } n Xg  } xt. |  D]\ } \ }  }! | d o | oA d g }" |" g  }# |  i/ d  D] }$ |# |$ i0   q~# 7}" nE d d g }" |" g  }% |  i/ d  D] }$ |% d |$ i0   qC~% 7}" d i |"  }& n d }& g  }' | i1   D]. \ }( }) |( d. j o |' d |( |) f qq~' }* d  }+ d! }, | d j o
 | }- n d  }- t3 t4 d" | d# | d$ |- d% t5 |!  d j d& |+ d' |, d( |* d) |! d* |& d+ | i6 
}. | i7 |. i/ d   | i7 d  qW| o | i8 | d |
 n t i	 i$ |  p t i% |  n xg | D]_ \ }  }! xP |! D]H }/ x? |/ i9   D]1 }0 t: i; |0 t i	 i | t i	 i |0    qWqWqW|
 | j oI t i	 i | | |  }1 t |1 d,  }2 |2 i< t= |   |2 i>   n | S(/   Ns1   plot:: directive can't have both args and contents   include-sourcet   sourcei    t   rs   
t   _plot_counteri   s   %s-%d.pys   .pys   .rsts   .txtR   t   .t   -R=   R,   R   t   /i   s%   Exception occurred in plotting %s: %st   lines   .. code-block:: pythons       %sR7   R8   R9   R:   R;   R<   s   :%s: %ss   .. only:: htmls   .. only:: latext   dest_dirt	   build_dirt   source_linkt   multi_imaget	   only_htmlt
   only_latexR   t   imagest   source_codet   html_show_formatst   w(   s   .pys   .rsts   .txt(   s   alts   heights   widths   scales   aligns   class(?   t   RuntimeErrort   documentt   settingst   envR   t
   setdefaultR   t
   attributesRM   RN   RI   R   RO   R   t   uriR
   R   t   opent   readRH   t   textwrapt   dedentt   mapt   strt   gett   splitextt   replacet   contains_doctestt   has_keyR   R   t   relpatht
   startswitht   sepR   t
   doctreedirt   existst   makedirst   abspatht   buildert   outdirt   makefigt	   PlotErrort   memot   reportert   system_messaget	   enumeratet   splitt   rstript   itemsR   RD   t   TEMPLATEt   lenR   t   extendt   insert_inputRS   t   shutilt   copyfilet   writet   unescape_doctestt   close(3   R   R   R   R    R   R   Rh   R   t   rst_filet   rst_dirt   source_file_namet   codet   output_baset   countert   baset   extt
   source_extt
   is_doctestt   source_rel_namet   source_rel_dirR^   R]   t   dest_dir_linkt   build_dir_linkR_   t   resultst   errorst   errR   t   smt   total_linest   jt
   code_pieceRc   t   linesRQ   t   rowt   _[2]Rd   t   _[3]t   keyt   valt   optsRa   Rb   t   src_linkt   resultt   imgt   fnt   target_namet   f(    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyR      s    	
!$


 	!	
	 	8
.+
   "t   Agg(   t   _pylab_helpersc         C   s\   y t  |  d d  t SWn t j
 o n Xt i  d t i  } | i |   } t |  S(   Ns   <string>t   execs   ^\s*>>>(   t   compileR   t   SyntaxErrort   ret   Mt   searcht   bool(   t   textRW   t   m(    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyRw   {  s    c         C   s   t  |   p |  Sd } x~ |  i d  D]m } t i d |  } | o | | i d  d 7} q( | i   o | d | i   d 7} q( | d 7} q( W| S(   s`   
    Extract code from a piece of text, which contains either Python code
    or doctests.

    R   s   
s   ^\s*(>>>|\.\.\.) (.*)$i   s   # (   Rw   R   R   t   matcht   groupR'   (   R   R   R\   R   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyR     s     c         C   s   g  } t  |   } g  } x |  i d  D]u } | o | i   d j p | o@ | i   d j o- | i |  | i d i |   g  } q( | i |  q( Wd i |  i   o | i d i |   n | S(   s#   
    Split code at plt.show()

    s   
s
   plt.show()s   >>> plt.show()(   Rw   R   R'   t   appendRO   (   R   t   partsR   t   partR\   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   split_code_at_show  s     
R   c           B   s   e  Z RS(    (   RT   RU   (    (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyR     s   c   	   	   B   sD  e  i   } e e i  } | d  j	 oB e  i i e  i i |   } e  i |  e i i	 d |  n e i
 } e i   e _
 e i } | g e _ z| yH e |   }  | d  j o
 h  } n | p e i i | Un |  | UWn- e e f j
 o } e e i     n XWd  e  i |  | e _ | e i (| e _
 X| S(   Ni    (   RM   t   getcwdt   listt   sysRN   R   R   RI   t   chdirt   insertt   stdoutt	   cStringIOt   StringIOt   argvR   R
   R   R   t	   Exceptiont
   SystemExitR   t	   tracebackt
   format_exc(	   R   t	   code_patht   nst   pwdt   old_sys_pathRI   R   t   old_sys_argvR   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   run_code  s2    		
	

c         C   s6   t  i i |  p" t  i |  i t  i |   i j  S(   sh   
    Returns True if derivative is out-of-date wrt original,
    both of which are full file paths.
    (   RM   RN   R}   t   statt   st_mtime(   t   originalt   derived(    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   out_of_date  s    c         C   s  h d d 6d d 6d d 6} g  } x | i  D] } t | t  o# | i | | i | d  f  q+ t |  t t f j o> t |  d j o+ | i t | d  t	 | d	  f  q+ t
 d
 |   q+ Wt |   } t }	 t | |  }
 xH | D]@ \ } } t | |
 i |   o t }	 Pn |
 i i |  q W|	 o |  |
 g f g Sg  } t }	 x t |  D] \ } } g  } x t d  D] } t d | | | f |  }
 xH | D]@ \ } } t | |
 i |   o t }	 Pn |
 i i |  qW|	 p | d j }	 Pn | i |
  qW|	 p Pn | i | | f  qlW|	 o | Sg  } h  } xXt |  D]J\ } } t i d  t | | |  g  } t i i   } x t |  D] \ } } t |  d	 j o& t |  d	 j o t | |  }
 n t d | | | f |  }
 | i |
  xw | D]o \ } } y& | i i i |
 i |  d | Wn* t i j
 o } t
 t i      n X|
 i i |  q)WqW| i | | f  qiW| S(   s{   
    Run a pyplot script *code* and save the images under *output_dir*
    with file names derived from *output_base*

    iP   R   i   s	   hires.pngi2   R   i   i    i   s)   invalid image format "%r" in plot_formatsi  s   %s_%02d_%02dt   allt   dpi(!   R   t
   isinstanceRs   R   Rt   t   typet   tupleR   R   t   intR   R   R   RG   R   RP   R   RJ   R   t   xranget   pltR   R   R   t   Gcft   get_all_fig_managerst   canvast   figuret   savefigt
   exceptionst   BaseExceptionR   R   (   R   R   t
   output_dirR   R   t   default_dpiRJ   RR   t   code_piecest
   all_existsR   R=   R   R   t   iR   Rc   R   R   t   fig_managerst   figmanR   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyR     s    
 #,+      &	 &(   Ry   t   posixc         C   s   d d k  l } l } l } l } l } l } |  p t d   n | |  i |  } | |   i |  }	 t	 | | |	 g   }
 | g t	 |  |
 |	 |
 } | p | S| |   S(   s#   Return a relative version of a pathi(   R{   t   curdirRO   R   t   commonprefixt   pardirs   no path specified(
   t   os.pathR{   R   RO   R   R   R   R)   R   R   (   RN   t   startR{   R   RO   R   R   R   t
   start_listt	   path_listR   t   rel_list(    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyRy   K  s    .t   ntc         C   s  d d k  l } l } l } l } l } l } l } |  p t d   n | |  i	 |  }	 | |   i	 |  }
 |	 d i
   |
 d i
   j ow | |   \ } } | |  \ } } t |  t |  Ao t d |  | f   qt d |
 d |	 d f   n xY t t t |	  t |
    D], } |	 | i
   |
 | i
   j o Pq/q/W| d 7} | g t |	  | |
 | } | p | S| |   S(   s#   Return a relative version of a pathi(   R{   R   RO   R   R   R   t   splituncs   no path specifiedi    s,   Cannot mix UNC and non-UNC paths (%s and %s)s&   path is on drive %s, start on drive %si   (   R   R{   R   RO   R   R   R   R   R)   R   R(   R   t   ranget   minR   (   RN   R   R{   R   RO   R   R   R   R   R   R   t   unc_patht   restt	   unc_startR   R   (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyRy   ^  s,    4!" !	
s,   Unsupported platform (no relpath available!)(=   t   __doc__R   RM   t   globR   t   impt   warningsR   R   Rp   R   t   sphinxt   warnt   FutureWarningR
   t   docutils.parsers.rstR   t   docutilsR   R   R+   R/   R6   t	   unchangedt   length_or_unitlesst    length_or_percentage_or_unitlesst   nonnegative_intt   class_optionR   R>   R?   RD   t   ImportErrorRE   R   t   objectRG   R   t
   matplotlibt   uset   matplotlib.pyplott   pyplotR   t   matplotlib.imaget   imageR   R   Rw   R   R   Rg   R   R   R   R   R   R   Ry   t   builtin_module_namesRN   R   (    (    (    s:   /Users/rgommers/Code/scipy/doc/sphinxext/plot_directive.pyt   <module>L   sh   x							




.				(			b