Ñò
ÚEjMc           @   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.

iÿÿÿÿNsµ   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