ó
q]c           @  s4  d  d l  m Z d  d l 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 d d	 l m Z e j j Z i e d
 d ƒ e d d ƒ 6e d
 d ƒ e d d ƒ 6e d d ƒ e d d ƒ 6Z e d d d ƒ Z e d d ƒ Z x9 d, D]1 Z e d d e ƒ Z e d
 e ƒ Z e e e <q%Wd d d g Z e j Z i d e d ƒ 6d e d ƒ 6d e d ƒ 6d e d  ƒ 6d e d! ƒ 6Z d" „  Z d# „  Z  d d$ „ Z" d% „  Z# d& „  Z$ d' „  Z% d( „  Z& d) e f d* „  ƒ  YZ' e' j( Z) e* d+ k r0e) ƒ  n  d S(-   iÿÿÿÿ(   t   unicode_literalsN(   t   datetime(   t   PyFileConfigLoadert   JSONFileConfigLoader(   t
   get_loggeri   (   t   ensure_dir_exists(   t   jupyter_config_dirt   jupyter_data_dir(   t
   JupyterAppu   {jupyter_data}u   nbextensionsu   {ipython_dir}u   kernelsu   {jupyter_config}u   nbconfigu	   {profile}u   staticu   customu   notebook_secretu   notebook_cookie_secretu   nbsignatures.dbu   securityu   notebooku	   nbconvertu	   qtconsoleu   JupyterQtConsoleAppu   \bIPythonQtConsoleApp\bu   JupyterWidgetu   \bIPythonWidget\bu   RichJupyterWidgetu   \bRichIPythonWidget\bu   \bIPython\.html\bu   \bIPython\.nbconvert\bc           C  s   t  j j d t  j j d ƒ ƒ S(   u£  Return the IPython directory location.

    Not imported from IPython because the IPython implementation
    ensures that a writable directory exists,
    creating a temporary directory if not.
    We don't want to trigger that when checking if migration should happen.

    We only need to support the IPython < 4 behavior for migration,
    so importing for forward-compatibility and edge cases is not important.
    u
   IPYTHONDIRu
   ~/.ipython(   t   ost   environt   gett   patht
   expanduser(    (    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyt   get_ipython_dirC   s    c         C  sº   t  ƒ  } t j |  ƒ s- | j d |  ƒ t St j j | ƒ rs t j | ƒ rc | j d | ƒ t St j | ƒ n  | j d |  | f ƒ t	 t j j
 | ƒ ƒ t j |  | d t ƒt S(   u#   Migrate a directory from src to dstu   No files in %su   %s already existsu   Copying %s -> %st   symlinks(   R   R	   t   listdirt   debugt   FalseR   t   existst   rmdirt   infoR   t   dirnamet   shutilt   copytreet   True(   t   srct   dstt   log(    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyt   migrate_dirQ   s    	c         C  sï   t  ƒ  } t j j | ƒ r0 | j d | ƒ t S| j d |  | f ƒ t t j j | ƒ ƒ t	 j
 |  | ƒ | rë t | ƒ  } | j ƒ  } Wd QXx, | j ƒ  D] \ } } | j | | ƒ } q¡ Wt | d ƒ  } | j | ƒ Wd QXn  t S(   u–   Migrate a single file from src to dst
    
    substitutions is an optional dict of {regex: replacement} for performing replacements on the file.
    u   %s already existsu   Copying %s -> %sNu   w(   R   R	   R   R   R   R   R   R   R   R   t   copyt   opent   readt   itemst   subt   writeR   (   R   R   t   substitutionsR   t   ft   textt   patt   replacement(    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyt   migrate_filed   s    	c         C  s`   t  ƒ  } t j j |  ƒ r( t |  | ƒ St j j |  ƒ rG t |  | ƒ S| j d |  ƒ t Sd S(   u>   Migrate one item
    
    dispatches to migrate_dir/_file
    u   Nothing to migrate for %sN(	   R   R	   R   t   isfileR)   t   isdirR   R   R   (   R   R   R   (    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyt   migrate_one{   s    	c      	   C  sÒ  t  ƒ  } t } t |  d ƒ } t |  d ƒ } t } t j j | ƒ r± t | ƒ [ } | j ƒ  j	 ƒ  } x? | j
 ƒ  D]1 }	 |	 j ƒ  p— |	 j	 ƒ  j d	 ƒ ss t } Pqs qs WWd QXn  t }
 t j j | ƒ rt | ƒ 7 } | j ƒ  j	 ƒ  } | j d ƒ o| j d ƒ }
 Wd QXn  | r+| j d | ƒ n  |
 rE| j d | ƒ n  | rU|
 rUt St | ƒ | sm|
 rzt | ƒ n  | s¤t | t | d ƒ ƒ r¤t } q¤n  |
 sÎt | t | d ƒ ƒ rÎt } qÎn  | S(
   uz   Migrate non-empty custom.js,css from src to dst
    
    src, dst are 'custom' directories containing custom.{js,css}
    u	   custom.jsu
   custom.cssu   /*u   *u   //Nu   */u   Ignoring empty %s(   u   /*u   *u   //(   R   R   t   pjoinR   R	   R   R*   R   R    t   stript
   splitlinest   isspacet
   startswitht   endswithR   R   R)   (   R   R   R   t   migratedt	   custom_jst
   custom_csst   custom_js_emptyR%   t   jst   linet   custom_css_emptyt   css(    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyt   migrate_static_customŠ   sD    	'
c         C  sò   t  ƒ  } t d d ƒ j d |  |  } t d d ƒ j d |  |  } i t d 6t d 6} g  } xŒ d
 D]„ } | | } | | }	 t j j | ƒ rf | | | ƒ j ƒ  }
 |
 rÖ t	 | |	 d t
 ƒrç | j | ƒ qç qê | j d	 | ƒ qf qf W| S(   uZ   Migrate a config file
    
    Includes substitutions for updated configurable names.
    u	   {profile}u   ipython_{name}_configt   nameu   {jupyter_config}u   jupyter_{name}_configu   .pyu   .jsonR$   u#   Not migrating empty config file: %s(   u   .pyu   .json(   R   R-   t   formatR   R   R	   R   R   t   load_configR)   t   config_substitutionst   appendR   (   R<   t   envR   t   src_baset   dst_baset   loadersR3   t   extR   R   t   cfg(    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyt   migrate_config¿   s"    	


c    
      C  su  i t  ƒ  d 6t ƒ  d 6t ƒ  d 6t j j t ƒ  d ƒ d 6}  t } xe t j ƒ  D]W \ } } | j	 |    } | j	 |    } t j j
 | ƒ rP t | | ƒ r§ t } q§ qP qP Wx& t D] } t | |  ƒ r² t } q² q² Wt j	 |    } t j	 |    } t j j
 | ƒ rt | | ƒ rt } qn  t |  d ƒ t t j j |  d d ƒ d ƒ   }	 |	 j t j ƒ  j ƒ  ƒ Wd QX| S(	   u(   Migrate IPython configuration to Jupyteru   jupyter_datau   jupyter_configu   ipython_diru   profile_defaultu   profileu   migratedu   wN(   R   R   R   R	   R   t   joinR   t
   migrationsR!   R=   R   R,   R   t   config_migrationsRG   t   custom_src_tt   custom_dst_tR;   R   R   R#   R   t   utcnowt	   isoformat(
   RA   R3   t   src_tt   dst_tR   R   R<   t
   custom_srct
   custom_dstR%   (    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyt   migrateÚ   s0    


%t   JupyterMigratec           B  s   e  Z d  Z d Z d „  Z RS(   u   jupyter-migrateu¹  
    Migrate configuration and data from .ipython prior to 4.0 to Jupyter locations.
    
    This migrates:
    
    - config files in the default profile
    - kernels in ~/.ipython/kernels
    - notebook javascript extensions in ~/.ipython/extensions
    - custom.js/css to .jupyter/custom
    
    to their new Jupyter locations.
    
    All files are copied, not moved.
    If the destinations already exist, nothing will be done.
    c         C  s    t  ƒ  s |  j j d ƒ n  d  S(   Nu   Found nothing to migrate.(   RS   R   R   (   t   self(    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyt   start  s    	(   t   __name__t
   __module__R<   t   descriptionRV   (    (    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyRT   þ   s   u   __main__(   u   notebook_secretu   notebook_cookie_secretu   nbsignatures.db(+   t
   __future__R    R	   t   reR   R   t   traitlets.configR   R   t   traitlets.logR   t   utilsR   t   pathsR   R   t   applicationR   R   RH   R-   RI   RK   RL   t   security_fileR   R   RJ   t   compilet   regexR?   R   R   t   NoneR)   R,   R;   RG   RS   RT   t   launch_instancet   mainRW   (    (    (    s3   lib/python2.7/site-packages/jupyter_core/migrate.pyt   <module>   sN   					5		$	