ó
_y]c           @   s$  d  d l  m Z d  d l Z d  d l Z d  d l m Z d  d l m Z m 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 y d  d
 l m Z Wn e k
 rÇ e Z n Xd e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t	   b64decodeN(   t   warn(   t   QtCoret   QtGui(   t   ensure_dir_exists(   t   Bool(   t   save_svgt   svg_to_clipboardt   svg_to_imagei   (   t   JupyterWidget(   t   latex_to_pngt
   LatexErrorc           B   s   e  Z d  Z RS(   s   Exception for Latex errors(   t   __name__t
   __module__t   __doc__(    (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR      s   t   RichIPythonWidgetc           B   s   e  Z d  Z RS(   s4   Dummy class for config inheritance. Destroyed below.(   R   R   R   (    (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR      s   t   RichJupyterWidgetc           B   s  e  Z d  Z d Z e e ƒ Z e Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z e d d	 „ Z e d d
 „ Z e d d „ Z e d „ Z d „  Z d „  Z d „  Z d d d „ Z d d „ Z d d „ Z d d „ Z d „  Z d d „ Z RS(   s²    An JupyterWidget that supports rich text, including lists, images, and
        tables. Note that raw performance will be reduced compared to the plain
        text version.
    s0   ipykernel.pylab.backend_payload.add_plot_payloadc         O   sW   d | d <t  t |  ƒ j | | Ž  |  j |  j _ i  |  _ d t j j	 ƒ  k |  _
 d S(   s%    Create a RichJupyterWidget.
        t   richt   kindt   jpegN(   t   superR   t   __init__t   _get_image_tagt   _html_exportert	   image_tagt   _name_to_svg_mapR   t   QImageReadert   supportedImageFormatst   _jpg_supported(   t   selft   argst   kw(    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR   1   s
    
	c         C   s    t  |  _ t t |  ƒ j ƒ  d S(   sª    Shows a dialog to export HTML/XML in various formats.

        Overridden in order to reset the _svg_warning_displayed flag prior
        to the export running.
        N(   t   Falset   _svg_warning_displayedR   R   t   export_html(   R   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR"   G   s    	c            s  ˆ j  j | ƒ j ƒ  } | j t j j ƒ ‰  ˆ  rê t j ˆ ƒ } | j d ‡  ‡ f d †  ƒ | j d ‡  ‡ f d †  ƒ | j	 ƒ  ˆ j
 j ˆ  d	 ƒ ‰ ˆ d	 k	 r| j	 ƒ  | j d ‡ f d †  ƒ | j d ‡ ‡ f d †  ƒ qn t t ˆ ƒ j | ƒ } | S(
   sC    Reimplemented to return a custom context menu for images.
        s
   Copy Imagec              s   ˆ j  ˆ  ƒ S(   N(   t   _copy_image(    (   t   nameR   (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyt   <lambda>]   t    s   Save Image As...c              s   ˆ j  ˆ  ƒ S(   N(   t   _save_image(    (   R$   R   (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR%   ^   R&   s   Copy SVGc              s
   t  ˆ  ƒ S(   N(   R   (    (   t   svg(    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR%   d   R&   s   Save SVG As...c              s   t  ˆ ˆ  j ƒ S(   N(   R   t   _control(    (   R   R(   (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR%   f   R&   N(   R)   t   cursorForPositiont
   charFormatt   stringPropertyR   t   QTextFormatt	   ImageNamet   QMenut	   addActiont   addSeparatorR   t   gett   NoneR   R   t   _context_menu_make(   R   t   post   formatt   menu(    (   R$   R   R(   s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR4   U   s    

	c         C   s@   |  j  |  j t ƒ |  j |  j | ƒ t ƒ |  j  d t ƒ d S(   st   Append the Out[] prompt  and make the output nicer

        Shared code for some the following if statement
        s   
N(   t   _append_plain_textt
   output_sept   Truet   _append_htmlt   _make_out_prompt(   R   t   msgt   prompt_number(    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyt   _pre_image_appendn   s    c         C   s#  |  j  j d | j d d ƒ ƒ |  j | ƒ r|  j ƒ  | d } | j d d ƒ } | d } | d d } d | k r² |  j | | ƒ |  j | d t ƒ |  j |  j	 t ƒ qd	 | k r|  j | | ƒ t
 | d	 j d
 ƒ ƒ } |  j | t d | j d	 d ƒ ƒ|  j |  j	 t ƒ qd | k r•|  j r•|  j | | ƒ t
 | d j d
 ƒ ƒ } |  j | t d | j d d ƒ ƒ|  j |  j	 t ƒ qd | k r|  j | | ƒ y |  j | d t ƒ Wn$ t k
 rït t |  ƒ j | ƒ SX|  j |  j	 t ƒ qt t |  ƒ j | ƒ Sn  d S(   s/   Overridden to handle rich data types, like SVG.s   execute_result: %st   contentR&   t   execution_counti    t   datat   metadatas   image/svg+xmls	   image/pngt   asciis
   image/jpegs
   text/latexN(   t   logt   debugR2   t   include_outputt   flush_clearoutputR?   t   _append_svgR:   R;   t   output_sep2R    t   encodet   _append_pngR3   R   t   _append_jpgt   _append_latexR   R   R   t   _handle_display_datat   _handle_execute_result(   R   R=   R@   R>   RB   RC   t   pngt   jpg(    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyRP   w   s>    




c         C   sª  |  j  j d | j d d ƒ ƒ |  j | ƒ r¦|  j ƒ  | d d } | d d } |  j  j d | j d d ƒ ƒ d | k rœ | d } |  j | t ƒ q¦d | k ræ t | d j d	 ƒ ƒ } |  j	 | t d | j d d ƒ ƒq¦d
 | k r9|  j r9t | d
 j d	 ƒ ƒ } |  j | t d | j d
 d ƒ ƒq¦d | k rt ry |  j | d t ƒ Wq£t k
 r‰t t |  ƒ j | ƒ SXq¦t t |  ƒ j | ƒ Sn  d S(   s/   Overridden to handle rich data types, like SVG.s   display_data: %sR@   R&   RB   RC   s   display: %ss   image/svg+xmls	   image/pngRD   s
   image/jpegs
   text/latexN(   RE   RF   R2   RG   RH   RI   R:   R    RK   RL   R3   R   RM   R
   RN   R   R   R   RO   (   R   R=   RB   RC   R(   RQ   RR   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyRO   ›   s*    

%%c         C   s   d d g } d d d d d d g } g  | D] } | d	 ^ q+ } | | | } g  | D] } d
 j  | ƒ j ƒ  ^ qV } d d d d g } xp | | D]d \ }	 }
 | t |	 ƒ t |
 ƒ !} |	 | k sÒ |
 | k rÖ t S| j |	 ƒ r” | j |
 ƒ r” t Sq” Wt S(   ss   
        Determine if a Latex string is in math mode

        This is the only mode supported by qtconsole
        t   matht   displaymatht   equationt   eqnarraymultlinet   gathert   alignt   flalignt   alignatt   *s   \begin{{{0}}} \end{{{0}}}s   \[s   \]s   \(s   \)s   $$t   $(   s   \[s   \](   s   \(s   \)(   s   $$s   $$(   R\   R\   (   R6   t   splitt   lenR    t
   startswitht   endswithR:   (   R   t   latext
   basic_envst   starable_envst   envt	   star_envst   envst
   env_syntaxt   math_syntaxt   startt   endt   inner(    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyt   _is_latex_math¼   s    (c         C   s³   d } |  j | ƒ r0 t | d t d d ƒ} n  | d k r | j d ƒ r | j d ƒ r y t | d t d d ƒ} Wq t k
 r‰ q Xn  | r© |  j | | | ƒ n t ‚ d S(   s!    Append latex data to the widget.t   wrapt   backendt   dvipngR\   t
   matplotlibN(	   R3   Rl   R
   R    R_   R`   t	   ExceptionRL   R   (   R   Ra   t   before_promptRC   RQ   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyRN   ×   s    *c         C   s    |  j  |  j | | d | ƒd S(   s#    Append raw JPG data to the widget.RC   N(   t   _append_customt   _insert_jpg(   R   RR   Rr   RC   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyRM   ì   s    c         C   s    |  j  |  j | | d | ƒd S(   s,    Append raw PNG data to the widget.
        RC   N(   Rs   t   _insert_png(   R   RQ   Rr   RC   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyRL   ð   s    c         C   s   |  j  |  j | | ƒ d S(   s,    Append raw SVG data to the widget.
        N(   Rs   t   _insert_svg(   R   R(   Rr   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyRI   õ   s    c         C   s`   |  j  j ƒ  } t | j ƒ  ƒ } | j t j j t j	 | ƒ | ƒ t j
 ƒ  } | j | ƒ | S(   sr    Adds the specified QImage to the document and returns a
            QTextImageFormat that references it.
        (   R)   t   documentt   strt   cacheKeyt   addResourceR   t   QTextDocumentt   ImageResourceR   t   QUrlt   QTextImageFormatt   setName(   R   t   imageRw   R$   R6   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyt
   _add_imageú   s    c         C   s)   |  j  | ƒ } t j j ƒ  j | ƒ d S(   s@    Copies the ImageResource with 'name' to the clipboard.
        N(   t
   _get_imageR   t   QApplicationt	   clipboardt   setImage(   R   R$   R€   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR#     s    c         C   s4   |  j  j ƒ  } | j t j j t j | ƒ ƒ } | S(   sE    Returns the QImage stored as the ImageResource with 'name'.
        (   R)   Rw   t   resourceR   R{   R|   R   R}   (   R   R$   Rw   R€   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR‚     s    RQ   c   
      C   sç  | d k rAy |  j  | j d ƒ ƒ } Wn t k
 rI d | j d ƒ SX| d k	 r» t | ƒ t j j | ƒ } | j d | | j d ƒ | f d ƒ r´ d | | j d ƒ | f Sd Sqãt	 j
 ƒ  } t	 j | ƒ } | j t	 j j ƒ | j | | j ƒ  ƒ | j ƒ  d	 | t j d
 d t | j ƒ  j ƒ  j ƒ  ƒ ƒ f Sn¢ | d k rßy  t |  j | j d ƒ ƒ } WnF t k
 rµ|  j s±t j j |  d d t j j ƒ t |  _ n  d SX| j d ƒ }	 |	 d k s×t ‚ | |	 Sd Sd S(   sg   Return (X)HTML mark-up for the image-tag given by match.

        Parameters
        ----------
        match : re.SRE_Match
            A match to an HTML image tag as exported by Qt, with
            match.group("Name") containing the matched image ID.

        path : string|None, optional [default None]
            If not None, specifies a path to which supporting files may be
            written (e.g., for linked images).  If None, all images are to be
            included inline.

        format : "png"|"svg"|"jpg", optional [default "png"]
            Format for returned or referenced images.
        RQ   RR   R$   s   <b>Couldn't find image %s</b>s   %s/qt_img%s.%st   PNGs   <img src="%s/qt_img%s.%s">s   <b>Couldn't save image!</b>s'   <img src="data:image/%s;base64,
%s
" />s   (.{60})s   \1\nR(   s   Error converting PNG to SVG.sÔ   Cannot convert PNG images to SVG, export with PNG figures instead. If you want to export matplotlib figures as SVG, add to your ipython config:

	c.InlineBackend.figure_format = 'svg'

And regenerate the figures.së   <b>Cannot convert  PNG images to SVG.</b>  You must export this session with PNG images. If you want to export matplotlib figures as SVG, add to your config <span>c.InlineBackend.figure_format = 'svg'</span> and regenerate the figures.s   <svgiÿÿÿÿs    <b>Unrecognized image format</b>N(   RQ   RR   (    R‚   t   groupt   KeyErrorR3   R   t   ost   patht   basenamet   saveR   t
   QByteArrayt   QBuffert   opent	   QIODevicet	   WriteOnlyt   uppert   closet   ret   subRx   t   toBase64RB   t   decodeR   R!   R   t   QMessageBoxt   warningt   OkR:   t   findt   AssertionError(
   R   t   matchR‹   R6   R€   t   relpatht   bat   buffer_R(   t   offset(    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR     sD    
	
5 	c         C   s   |  j  | | d d | ƒd S(   s%    Insert raw PNG data into the widget.RR   RC   N(   t   _insert_img(   R   t   cursorRR   RC   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyRt   ]  s    c         C   s   |  j  | | d d | ƒd S(   s.    Insert raw PNG data into the widget.
        RQ   RC   N(   R£   (   R   R¤   RQ   RC   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyRu   a  s    c   	      C   s<  | r- | j  d d ƒ } | j  d d ƒ } n
 d } } y© t j ƒ  } | j | | j ƒ  ƒ | r | r | j | | t j j	 t j j
 ƒ } nP | r· | r· | j | t j j
 ƒ } n( | rß | rß | j | t j j
 ƒ } n  Wn% t k
 r|  j | d | ƒ n1 X|  j | ƒ } | j ƒ  | j | ƒ | j ƒ  d S(   s     insert a raw image, jpg or png t   widtht   heights   Received invalid %s data.N(   R2   R3   R   t   QImaget   loadFromDataR“   t   scaledR   t   Qtt   IgnoreAspectRatiot   SmoothTransformationt   scaledToWidtht   scaledToHeightt
   ValueErrort   _insert_plain_textR   t   insertBlockt   insertImage(	   R   R¤   t   imgt   fmtRC   R¥   R¦   R€   R6   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR£   f  s*    
	
c         C   s{   y t  | ƒ } Wn! t k
 r3 |  j | d ƒ nD X|  j | ƒ } | |  j | j ƒ  <| j ƒ  | j | ƒ | j ƒ  d S(   s-    Insert raw SVG data into the widet.
        s   Received invalid SVG data.N(   R   R¯   R°   R   R   R$   R±   R²   (   R   R¤   R(   R€   R6   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyRv   €  s    
R‡   c         C   sš   t  j |  j d ƒ } | j t  j j ƒ | j | j ƒ  ƒ | j d | | j ƒ  f ƒ | j ƒ  r– | j	 ƒ  d } |  j
 | ƒ } | j | | ƒ n  d S(   s@    Shows a save dialog for the ImageResource with 'name'.
        s
   Save Images   %s file (*.%s)i    N(   R   t   QFileDialogR)   t   setAcceptModet
   AcceptSavet   setDefaultSuffixt   lowert   setNameFiltert   exec_t   selectedFilesR‚   R   (   R   R$   R6   t   dialogt   filenameR€   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR'   Ž  s    N(   R   R   R   t   _payload_source_plotR   R    R   R!   R   R"   R4   R?   RP   RO   Rl   R3   RN   RM   RL   RI   R   R#   R‚   R   Rt   Ru   R£   Rv   R'   (    (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR      s0   						$	!				I	c           B   s   e  Z d  Z d „  Z RS(   s(   Deprecated class. Use RichJupyterWidget.c         O   s*   t  d t ƒ t t |  ƒ j | | Ž  d  S(   Ns6   RichIPythonWidget is deprecated, use RichJupyterWidget(   R   t   DeprecationWarningR   R   R   (   R   t   aR   (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR   Ÿ  s    (   R   R   R   R   (    (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyR     s   (   t   base64R    RŠ   R•   t   warningsR   t   qtconsole.qtR   R   t   ipython_genutils.pathR   t	   traitletsR   t   qtconsole.svgR   R   R   t   jupyter_widgetR	   t   IPython.lib.latextoolsR
   t   ImportErrorR3   Rq   R   R   R   (    (    (    s<   lib/python2.7/site-packages/qtconsole/rich_jupyter_widget.pyt   <module>   s"   
ÿ 