
    lCe!                       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"d6d$Z#d7d(Z$d8d9d+Z%d7d,Z&d7d-Z'd:d1Z(d;d2Z) G d3 d4e          Z*e*j+        Z,e-d5k    r e,             dS dS )<a  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   returnstrc                 |    t           j                            dt           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     4lib/python3.11/site-packages/jupyter_core/migrate.pyget_ipython_dirr+   I   s*     :>>,(:(:<(H(HIIIr)   srcdstboolc                   t                      }t          j        |           s|                    d|            dS t          j                            |          r@t          j        |          r|                    d|           dS t          j        |           |                    d| |           t          t          j        	                    |                     t          j        | |d           dS )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r,   r-   logs      r*   migrate_dirr=   W   s    
,,C:c?? 		"C(((u	w~~c :c?? 	II)3///5HSMMMHHc***bgooc**+++
OCt,,,,4r)   substitutionsr   c                   t                      }t          j                            |          r|                    d|           dS |                    d| |           t          t          j                            |                     t          j	        | |           |rt          |d          5 }|                                }ddd           n# 1 swxY w Y   |                                D ]\  }}|                    ||          }t          |dd          5 }|                    |           ddd           n# 1 swxY w Y   dS )	zMigrate a single file from src to dst

    substitutions is an optional dict of {regex: replacement} for performing replacements on the file.
    r0   Fr1   utf-8encodingNwT)r	   r#   r&   r5   r4   r7   r   r8   r9   copyopenreaditemssubwrite)r,   r-   r>   r<   ftextpatreplacements           r*   migrate_filerN   j   s   
 ,,C	w~~c 		%s+++uHHc***bgooc**+++
KS #((( 	A6688D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 - 3 3 5 5 	. 	.C77;--DD#sW--- 	GGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	4s$   1CCCEEEc                
   t                      }t          j                            |           rt	          | |          S t          j                            |           rt          | |          S |                    d|            dS )z:Migrate one item

    dispatches to migrate_dir/_file
    zNothing to migrate for %sF)r	   r#   r&   isfilerN   isdirr=   r4   r;   s      r*   migrate_onerR      ss    
 ,,C	w~~c C%%%	s		 3$$$		-s333ur)   c                ^   t                      }d}t          | d          }t          | d          }d}t          j                            |          rt          |d          5 }|                                                                }|                                D ]A}	|		                                s+|	                                
                    d          sd} nBddd           n# 1 swxY w Y   d}
t          j                            |          ryt          |d          5 }|                                                                }|
                    d	          o|                    d
          }
ddd           n# 1 swxY w Y   |r|                    d|           |
r|                    d|           |r|
rdS t          |           |r|
st          |           |s t          |t          |d                    rd}|
s t          |t          |d                    rd}|S )zvMigrate non-empty custom.js,css from src to dst

    src, dst are 'custom' directories containing custom.{js,css}
    Fz	custom.jsz
custom.cssTr@   rA   )/**z//NrT   z*/zIgnoring empty %s)r	   pjoinr#   r&   rP   rE   rF   strip
splitlinesisspace
startswithendswithr4   r   rN   )r,   r-   r<   migrated	custom_js
custom_csscustom_js_emptyrJ   jslinecustom_css_emptycsss               r*   migrate_static_customrd      s   
 ,,CHc;''IsL))JO	w~~i   )g... 	!!!B   $**,,*A*ABS*T*T &+OE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	w~~j!! K*w/// 	K1&&((..""C"~~d33JT8J8J	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K  2		%y111 3		%z222 + uc "2 # |IuS+7N7NOO  ZsL9Q9Q R R Os&   #A=C,,C03C0)AFF
F
nameenv	list[Any]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                            |          rh ||         |                                          }
|
r-t          ||	t                    r|                    |           }|                    d|           |S )
zWMigrate a config file.

    Includes substitutions for updated configurable names.
    r   zipython_{name}_configre   r   zjupyter_{name}_config)z.pyz.json)r>   z#Not migrating empty config file: %sr(   )r	   rV   formatr   r   r#   r&   r5   load_configrN   config_substitutionsappendr4   )re   rf   r<   src_basedst_baseloadersr\   extr,   r-   cfgs              r*   migrate_configrr      s"   
 ,,CAu["9::ASStSsSSHHu')@AAHZZdZVYZZH!% G H 
F 
Fnn7>># 	F'#,s##//11C FS8LMMM )OOC((( 		?EEEOr)   c                    t                      t                      t                      t          j                            t                      d          d} d}t                                          D ]P\  }} |j        di | } |j        di | }t          j        	                    |          rt          ||          rd}Qt          D ]}t          ||           rd}t          j        di | }t          j        di | }t          j        	                    |          rt          ||          rd}t!          | d                    t#          t          j                            | d         d          dd	          5 }	|	                    t'          j        t*          j        
                                                     ddd           n# 1 swxY w Y   |S )z(Migrate IPython configuration to Jupyterprofile_default)jupyter_datajupyter_configipython_dirprofileFTrv   r\   rC   r@   rA   )tzNr(   )r   r   r+   r#   r&   join
migrationsrG   ri   r5   rR   config_migrationsrr   custom_src_tcustom_dst_trd   r   rE   rI   r   nowr   utc	isoformat)
rf   r\   src_tdst_tr,   r-   re   
custom_src
custom_dstrJ   s
             r*   migrater      s    )**,..&((7<< 1 13DEE	 C H"((**  uel!!S!!el!!S!!7>># 	;sC#8#8 	H!  $$$ 	H$++s++J$++s++J	w~~j!! &;J
&S&S  c*+,,,	bgll3/0*==sW	U	U	U ;YZ	---7799:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ; Os   <AGGGc                  "    e Zd ZdZdZdZddZdS )	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.
    r   Nonec                Z    t                      s| j                            d           dS dS )zStart the application.zFound nothing to migrate.N)r   r<   r7   )selfs    r*   startzJupyterMigrate.start  s3    yy 	7HMM566666	7 	7r)   N)r   r   )__name__
__module____qualname____doc__re   descriptionr   r(   r)   r*   r   r      s:        ""DK 7 7 7 7 7 7r)   r   __main__)r   r    )r,   r    r-   r    r   r.   )N)r,   r    r-   r    r>   r   r   r.   )re   r    rf   r   r   rg   )r   r.   ).r   
__future__r   r#   rer9   r   r   typingr   traitlets.config.loaderr   r   traitlets.logr	   applicationr   pathsr   r   utilsr   r&   rz   rV   r{   r}   r~   security_filer,   r-   r|   compileregexrk   r+   r=   rN   rR   rd   rr   r   r   launch_instancemainr   r(   r)   r*   <module>r      s    0 # " " " " " 				 				  ' ' ' ' ' ' ' '       L L L L L L L L $ $ $ $ $ $ # # # # # # 7 7 7 7 7 7 7 7 $ $ $ $ $ $
 	 
E/>**EE2BN,S,S	E/9%%uu-=y'I'I	E+z""EE*<j$I$I
 u[(H55u'22U  M
%Z
7
7C
% -
0
0CJsOO::: 

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