
    f!                       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 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 e	dd             e e	dd             e e	dd             e e	dd             e e	dd             e e	dd            iZ e e	ddd            Z e e	dd            ZdD ]&  Z e e	dde            Z e 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/d0d%Z%d.d&Z&d.d'Z'd1d(Z(d2d)Z) G d* d+e      Z*e*jV                  Z,e-d,k(  r e,        yy)3a  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)Path)Any)JSONFileConfigLoaderPyFileConfigLoader)
get_logger   )
JupyterApp)jupyter_config_dirjupyter_data_dir)ensure_dir_existsz{ipython_dir}nbextensionsz{jupyter_data}kernelsz	{profile}nbconfigz{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        t	        d      j                                     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strr   
expanduser     O/var/www/cvtools/html/venv/lib/python3.12/site-packages/jupyter_core/migrate.pyget_ipython_dirr(   H   s,     ::>>,D,>,I,I,K(LMMr&   c                   t               }t        j                  |       s|j                  d|        yt	        |      j                         rAt        j                  |      r|j                  d|       yt	        |      j                          |j                  d| |       t        t	        |      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   parentshutilcopytreesrcdstlogs      r'   migrate_dirr9   V   s    
,C::c?		"C(Cy::c?II)3/S	HHc*d3i&&'
OOCt,r&   c                p   t               }t        |      j                         r|j                  d|       y|j	                  d| |       t        t        |      j                         t        j                  | |       |rt        j                  t        |      d      5 }|j                         }ddd       |j                         D ]  \  }}|j                  |      } t        j                  t        |      dd      5 }|j                         ddd       yy# 1 sw Y   px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/   r.   r1   r   r2   r3   copyopenreaditemssubwrite)r6   r7   substitutionsr8   ftextpatreplacements           r'   migrate_filerJ   h   s    
 ,CCy		%s+HHc*d3i&&'
KKSYYtCy73 	q668D	 - 3 3 5 	.C77;-D	.YYtCy#8 	AGGDM		 		s    D D, D),D5c                    t               }t        |       j                         rt        | |      S t        |       j	                         rt        | |      S |j                  d|        y)z:Migrate one item

    dispatches to migrate_dir/_file
    zNothing to migrate for %sF)r
   r   is_filerJ   is_dirr9   r.   r5   s      r'   migrate_onerN      sW    
 ,CCyC%%Cy3$$II)3/r&   c                   t               }d}t        | d      }t        | d      }d}t        |      j                         rt        j                  |d      5 }|j	                         j                         }|j                         D ]6  }	|	j                         r|	j                         j                  d      r4d} n ddd       d}
t        |      j                         rbt        j                  |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//NrP   z*/zIgnoring empty %s)r
   r   rL   r@   rA   strip
splitlinesisspace
startswithendswithr.   r   rJ   )r6   r7   r8   migrated	custom_js
custom_csscustom_js_emptyrF   jslinecustom_css_emptycsss               r'   migrate_static_customr_      s   
 ,CHS+&Ic<(JOI YYy73 	q!B $**,*A*ABS*T&+O	 J!YYzG4 	K&&(.."C"~~d3JT8J	K 		%y1		%z2+c"2#|ItC7MNZc<9P QOA	 		K 	Ks&   AGG:G9AGGGc                   t               }t        t        |d    d|  d            }t        t        |d    d|  d            }t        t        d}g }dD ]x  }||z   }||z   }	t        |      j                         s' ||   |      j                         }
|
r%t        ||	t              sU|j                  |       g|j                  d|       z |S )	zWMigrate a config file.

    Includes substitutions for updated configurable names.
    profileipython__configjupyter_configjupyter_)z.pyz.json)rE   z#Not migrating empty config file: %s)r
   r#   r   r	   r   r/   load_configrJ   config_substitutionsappendr.   )nameenvr8   src_basedst_baseloadersrW   extr6   r7   cfgs              r'   migrate_configrp      s    
 ,C43y>*xvW-EFGH43/01htfG4LMNH!%G H 
Fnn9'#,s#//1CS8LMOOC( 		?E
F Or&   c            	     :   t               t               t               t        t	        t               d            d} d}t
        j                         D ]R  \  }} |j                  di | } |j                  di | }t	        |      j                         sDt        ||      sQd}T t        D ]  }t        ||       sd} t        j                  di | }t        j                  di | }t	        |      j                         rt        ||      rd}t        | d          t	        j                   t	        | 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_datard   ipython_dirra   FTrd   rW   r>   r;   r<   )tzNr%   )r   r   r(   r#   r   
migrationsrB   formatr/   rN   config_migrationsrp   custom_src_tcustom_dst_tr_   r   r@   rD   r   nowr   utc	isoformat)
rj   rW   src_tdst_tr6   r7   ri   
custom_src
custom_dstrF   s
             r'   migrater      so    )*,.&(tO-/@AB	C H"((* uell!S!ell!S!9+c3"7H	 " $$H $$+s+J$$+s+JJ %::z%R c*+,	4,-z:C'	R ;VW	-779:; O; Os   AF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   r8   r1   )selfs    r'   startzJupyterMigrate.start  s    yHHMM56 r&   N)returnNone)__name__
__module____qualname____doc__ri   descriptionr   r%   r&   r'   r   r      s    "DK 7r&   r   __main__)r   r#   )r6   r#   r7   r#   r   bool)N)r6   
str | Pathr7   r   rE   r   r   r   )ri   r#   rj   r   r   z	list[Any])r   r   ).r   
__future__r   r    rer3   r   r   pathlibr   typingr   traitlets.config.loaderr   r	   traitlets.logr
   applicationr   pathsr   r   utilsr   r#   rv   ry   rz   security_filer6   r7   rx   compileregexrg   r(   r9   rJ   rN   r_   rp   r   r   launch_instancemainr   r%   r&   r'   <module>r      s  0 # 	 	  '   L $ # 7 $ _n-.D9I>4Z0[_i()3t4Di/P+Q[*%&D1CZ,P(Q
 4Xx894*H56U M
d;
M:
;C
d#]3
4CJsO
 ; 


 

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