ó
æNXc           @  sD  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 m
 Z
 y d d l m Z Wn! e k
 r¥ d d l m Z n Xe j j e
 ƒ  d ƒ Z e j j e d ƒ Z e d „ Z d	 „  Z d
 „  Z d e e d e d „ Z d „  Z e d k r1e j e ƒ  ƒ n  d d d g Z d S(   s‰  Utility function for installing MathJax javascript library into
your IPython nbextensions directory, for offline use.

 Authors:

 * Min RK
 * Mark Sienkiewicz
 * Matthias Bussonnier

To download and install MathJax:

From Python:

    >>> from IPython.external.mathjax import install_mathjax
    >>> install_mathjax()

From the command line:

    $ python -m IPython.external.mathjax

To a specific location:

    $ python -m IPython.external.mathjax -i /usr/share/

will install mathjax to /usr/share/mathjax

To install MathJax from a file you have already downloaded:

    $ python -m IPython.external.mathjax mathjax-xxx.tar.gz
    $ python -m IPython.external.mathjax mathjax-xxx.zip

It will not install MathJax if it is already there.  Use -r to
replace the existing copy of MathJax.

To find the directory where IPython would like MathJax installed:

    $ python -m IPython.external.mathjax -d

iÿÿÿÿ(   t   print_functionN(   t   get_ipython_dir(   t   urlopent   nbextensionst   mathjaxc         C  s×   t  j j t  j j |  t  j j ƒ ƒ } t  j j | ƒ sI t  j | ƒ n  t  j j |  ƒ rÏ | r€ t d |  ƒ t j	 |  ƒ t
 St  j j |  d ƒ } t  j j | ƒ sº t d |  ƒ ‚ n  t d | ƒ t Sn t
 Sd S(   s©   prepare the destination folder for mathjax install
    
    Returns False if mathjax appears to already be installed and there is nothing to do,
    True otherwise.
    s   removing existing MathJax at %ss
   MathJax.jss*   %s exists, but does not contain MathJax.jss   %s already existsN(   t   ost   patht   abspatht   joint   pardirt   existst   makedirst   printt   shutilt   rmtreet   Truet   IOErrort   False(   t   destt   replacet   parentt
   mathjax_js(    (    sP   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/external/mathjax.pyt   prepare_destQ   s    'c         C  s{   t  j d |  d d ƒ } | j j } t j j t j j | t j j ƒ ƒ } | j | ƒ t j	 t j j | | ƒ | ƒ d S(   s:   extract a tarball from filelike `fd` to destination `dest`t   fileobjt   modes   r|gzN(
   t   tarfilet   opent   firstmemberR   R   R   R   R	   t
   extractallt   rename(   t   fdR   t   tart   topdirR   (    (    sP   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/external/mathjax.pyt   extract_tark   s
    'c         C  sy   t  j |  d ƒ } | j ƒ  d } t j j t j j | t j j ƒ ƒ } | j | ƒ t j	 t j j | | ƒ | ƒ d S(   s;   extract a zip file from filelike `fd` to destination `dest`t   ri    N(
   t   zipfilet   ZipFilet   namelistR   R   R   R   R	   R   R   (   R   R   t   zR    R   (    (    sP   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/external/mathjax.pyt   extract_zip{   s
    's   2.4.0c   	      C  s£   y t  | | ƒ } Wn( t k
 r= } t d | | f ƒ d SX| sH d S| d k r„ d |  } t d | ƒ t | ƒ } | j } n  t d | ƒ | | | ƒ d S(   s  Download and/or install MathJax for offline use.

    This will install mathjax to the nbextensions dir in your IPYTHONDIR.

    MathJax is a ~15MB download, and ~150MB installed.

    Parameters
    ----------

    replace : bool [False]
        Whether to remove and replace an existing install.
    dest : str [IPYTHONDIR/nbextensions/mathjax]
        Where to install mathjax
    tag : str ['2.4.0']
        Which tag to download. Default is '2.4.0', the current stable release,
        but alternatives include 'v1.1a' and 'master'.
    file : file like object [ defualt to content of https://github.com/mathjax/MathJax/tarball/#{tag}]
        File handle from which to untar/unzip/... mathjax
    extractor : function
        Method to use to untar/unzip/... `file`
    s$   ERROR %s, require write access to %si   i    s4   https://github.com/mathjax/MathJax/archive/%s.tar.gzs"   Downloading mathjax source from %ss   Extracting to %sN(   R   t   OSErrorR   t   NoneR   t   fp(	   t   tagR   R   t   filet	   extractort   anything_to_dot   et   mathjax_urlt   response(    (    sP   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/external/mathjax.pyt   install_mathjaxŠ   s    
c       	   C  s8  t  j d d ƒ }  |  j d d d t d d ƒ|  j d d	 d
 d d d ƒ|  j d d d
 d d d ƒ|  j d d d d d d d ƒ|  j ƒ  } t j j | j d ƒ } | j	 rÀ t
 | ƒ d  S| j } | j r!| j } | j d ƒ ró t } n t } t d t | d ƒ d | d | d | ƒ St d | d | ƒ Sd  S(   Nt   descriptions.   Install mathjax from internet or local archives   -is   --install-dirt   defaultt   helpsH   custom installation directory. Mathjax will be installed in here/mathjaxs   -ds   --print-destt   actiont
   store_trues/   print where mathjax would be installed and exits   -rs	   --replaces7   Whether to replace current mathjax if it already existst   filenames2   the local tar/zip-ball filename containing mathjaxt   nargst   ?t   metavarR   s   .zipR,   t   rbR   R-   R   (   t   argparset   ArgumentParsert   add_argumentR   t
   parse_argsR   R   R   t   install_dirt
   print_destR   R   R8   t   endswithR'   R!   R2   R   (   t   parsert   pargsR   R   t   fnameR-   (    (    sP   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/external/mathjax.pyt   mainµ   sD    			
				(t   __main__R2   RG   t   default_dest(   t   __doc__t
   __future__R    R=   R   R   t   sysR   R#   t   IPython.pathsR   t   urllib.requestR   t   ImportErrort   urllib2R   R   R   RI   R   R   R!   R'   R)   R2   RG   t   __name__t   exitt   __all__(    (    (    sP   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/external/mathjax.pyt   <module>(   s,   		+	5