
    @e!                    v   d Z ddlmZ ddlZddlZddlZddlmZm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 edd       edd       edd       edd       edd       edd      iZ eddd      Z edd      ZdD ]  Z edde      Z ede      Zeee<    g dZej@                  Z! e!d      d e!d      d e!d      d e!d      d e!d       d!iZ"d,d"Z#d-d#Z$d.d/d$Z%d-d%Z&d-d&Z'd0d'Z(d1d(Z) G d) d*e      Z*e*jV                  Z,e-d+k(  r e,        yy)2a  Migrating IPython < 4.0 to Jupyter

This *copies* configuration and resources to their new locations in Jupyter

Migrations:

- .ipython/
  - nbextensions -> JUPYTER_DATA_DIR/nbextensions
  - kernels ->  JUPYTER_DATA_DIR/kernels

- .ipython/profile_default/
  - static/custom -> .jupyter/custom
  - nbconfig -> .jupyter/nbconfig
  - security/

    - notebook_secret, notebook_cookie_secret, nbsignatures.db -> JUPYTER_DATA_DIR

  - ipython_{notebook,nbconvert,qtconsole}_config.py -> .jupyter/jupyter_{name}_config.py


    )annotationsN)datetimetimezone)Any)JSONFileConfigLoaderPyFileConfigLoader)
get_logger   )
JupyterApp)jupyter_config_dirjupyter_data_dir)ensure_dir_existsz{ipython_dir}nbextensionsz{jupyter_data}kernels	{profile}nbconfig{jupyter_config}staticcustom)notebook_secretnotebook_cookie_secretznbsignatures.dbsecurity)notebook	nbconvert	qtconsolez\bIPythonQtConsoleApp\bJupyterQtConsoleAppz\bIPythonWidget\bJupyterWidgetz\bRichIPythonWidget\bRichJupyterWidgetz\bIPython\.html\br   z\bIPython\.nbconvert\br   c                 |    t         j                  j                  dt         j                  j	                  d            S )a  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.
    
IPYTHONDIRz
~/.ipython)osenvirongetpath
expanduser     6/usr/lib/python3/dist-packages/jupyter_core/migrate.pyget_ipython_dirr)   I   s(     ::>>,(:(:<(HIIr'   c                   t               }t        j                  |       s|j                  d|        yt        j                  j                  |      r=t        j                  |      r|j                  d|       yt        j                  |       |j                  d| |       t        t        j                  j                  |             t        j                  | |d       y)z#Migrate a directory from src to dstzNo files in %sF%s already existsCopying %s -> %sT)symlinks)r	   r!   listdirdebugr$   existsrmdirinfor   dirnameshutilcopytreesrcdstlogs      r(   migrate_dirr:   W   s    
,C::c?		"C(	ww~~c::c?II)3/HHSMHHc*bggooc*+
OOCt,r'   c                D   t               }t        j                  j                  |      r|j	                  d|       y|j                  d| |       t        t        j                  j                  |             t        j                  | |       |ryt        |d      5 }|j                         }ddd       |j                         D ]  \  }}|j                  |      } t        |dd      5 }|j                         ddd       yy# 1 sw Y   ]xY w# 1 sw Y   yxY w)	zMigrate a single file from src to dst

    substitutions is an optional dict of {regex: replacement} for performing replacements on the file.
    r+   Fr,   utf-8encodingNwT)r	   r!   r$   r0   r/   r2   r   r3   r4   copyopenreaditemssubwrite)r7   r8   substitutionsr9   ftextpatreplacements           r(   migrate_filerK   j   s    
 ,C	ww~~c		%s+HHc*bggooc*+
KKS#( 	A668D	 - 3 3 5 	.C77;-D	.#sW- 	GGDM		 		s   D
.D
DDc                    t               }t        j                  j                  |       rt	        | |      S t        j                  j                  |       rt        | |      S |j                  d|        y)z:Migrate one item

    dispatches to migrate_dir/_file
    zNothing to migrate for %sF)r	   r!   r$   isfilerK   isdirr:   r/   r6   s      r(   migrate_onerO      sW    
 ,C	ww~~cC%%	s	3$$		-s3r'   c                   t               }d}t        | d      }t        | d      }d}t        j                  j	                  |      r}t        |d      5 }|j                         j                         }|j                         D ]6  }	|	j                         r|	j                         j                  d      r4d} n ddd       d}
t        j                  j	                  |      rXt        |d      5 }|j                         j                         }|j                  d	      xr |j                  d
      }
ddd       |r|j                  d|       |
r|j                  d|       |r|
ryt        |       |r|
st        |       |st        |t        |d            rd}|
st        |t        |d            rd}|S # 1 sw Y   xY w# 1 sw Y   xY w)zvMigrate non-empty custom.js,css from src to dst

    src, dst are 'custom' directories containing custom.{js,css}
    Fz	custom.jsz
custom.cssTr<   r=   )/**z//NrQ   z*/zIgnoring empty %s)r	   pjoinr!   r$   rM   rA   rB   strip
splitlinesisspace
startswithendswithr/   r   rK   )r7   r8   r9   migrated	custom_js
custom_csscustom_js_emptyrG   jslinecustom_css_emptycsss               r(   migrate_static_customra      s   
 ,CHc;'IsL)JO	ww~~i )g. 	!!B $**,*A*ABS*T&+O	 	ww~~j!*w/ 	K1&&(.."C"~~d3JT8J	K 		%y1		%z2+c"2#|IuS+7NOZsL9Q ROA	 		K 	Ks&   AF9F96F91AG9GGc                   t               } t        dd      j                  d	d| i|} t        dd      j                  d	d| i|}t        t        d}g }dD ]~  }||z   }||z   }	t
        j                  j                  |      s- ||   |      j                         }
|
r%t        ||	t              s[|j                  |       m|j                  d|        |S )
zWMigrate a config file.

    Includes substitutions for updated configurable names.
    r   zipython_{name}_confignamer   zjupyter_{name}_config)z.pyz.json)rF   z#Not migrating empty config file: %sr&   )r	   rS   formatr   r   r!   r$   r0   load_configrK   config_substitutionsappendr/   )rc   envr9   src_basedst_baseloadersrY   extr7   r8   cfgs              r(   migrate_configrn      s    
 ,CAu["9:AAStSsSHHu')@AHHZdZVYZH!%G H 
Fnn77>>#'#,s#//1CS8LMOOC( 		?E
F Or'   c                 |   t               t               t               t        j                  j                  t               d      d} d}t        j                         D ]X  \  }} |j                  di | } |j                  di | }t        j                  j                  |      sJt        ||      sWd}Z t        D ]  }t        ||       sd} t        j                  di | }t        j                  di | }t        j                  j                  |      rt        ||      rd}t!        | d          t#        t        j                  j                  | d   d      dd	      5 }	|	j%                  t'        j(                  t*        j,                  
      j/                                ddd       |S # 1 sw Y   |S xY w)z(Migrate IPython configuration to Jupyterprofile_default)jupyter_datajupyter_configipython_dirprofileFTrr   rY   r?   r<   r=   )tzNr&   )r   r   r)   r!   r$   join
migrationsrC   rd   r0   rO   config_migrationsrn   custom_src_tcustom_dst_tra   r   rA   rE   r   nowr   utc	isoformat)
rh   rY   src_tdst_tr7   r8   rc   
custom_src
custom_dstrG   s
             r(   migrater      su    )*,.&(77<< 13DE	C H"((* uell!S!ell!S!77>>#;sC#8H	 " $$H $$+s+J$$+s+J	ww~~j!&;J
&S c*+,	bggll3/0*=sW	U ;YZ	-779:; O; Os   %AF11F;c                       e Zd ZdZdZdZddZy)JupyterMigratezA Jupyter Migration App.zjupyter-migratea  
    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                P    t               s| j                  j                  d       yy)zStart the application.zFound nothing to migrate.N)r   r9   r2   )selfs    r(   startzJupyterMigrate.start  s    yHHMM56 r'   N)returnNone)__name__
__module____qualname____doc__rc   descriptionr   r&   r'   r(   r   r      s    "DK 7r'   r   __main__)r   str)r7   r   r8   r   r   bool)N)r7   r   r8   r   rF   r   r   r   )rc   r   rh   r   r   z	list[Any])r   r   ).r   
__future__r   r!   rer4   r   r   typingr   traitlets.config.loaderr   r   traitlets.logr	   applicationr   pathsr   r   utilsr   r$   rv   rS   rw   ry   rz   security_filer7   r8   rx   compileregexrf   r)   r:   rK   rO   ra   rn   r   r   launch_instancemainr   r&   r'   r(   <module>r      sx  0 # 	 	  '  L $ # 7 $
 	 
/>*E2BN,S	/9%u-=y'I	+z"E*<j$I
 [(H5'2U M
Z
7C
 -
0CJsO
 ; 


 

$%'<	
	
"#%8	
	
#$k J&.-`6B7Z 74 %% zF r'   