
    Mh!                    ^   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 ]2Z e e	dde                    Z e e	de                    Zeee<   3g dZej         Z! e!d          d e!d          d e!d          d e!d          d  e!d!          d"iZ"d8d%Z#d9d)Z$d:d;d-Z%d9d.Z&d9d/Z'd<d3Z(d=d4Z) G d5 d6e          Z*e*j+        Z,e-d7k    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)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   returnstrc                     t           j                            dt          t	          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getr   r   
expanduser     T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_core/migrate.pyget_ipython_dirr)   H   s6     :>>,D,>,>,I,I,K,K(L(LMMMr'   srcdstboolc                   t                      }t          |           }t          |          }t          |                                          s|                    d|            dS |                                rMt          |                                          r|                    d|           dS |                                 |                    d| |           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   anyiterdirdebugexistsrmdirinfor   parentshutilcopytree)r*   r+   logsrc_pathdst_paths        r(   migrate_dirr=   V   s    
,,CCyyHCyyHx!!"" 		"C(((u x!!"" 	II)3///5HHc***ho&&&
OCt,,,,4r'   
str | Pathsubstitutionsr   c                   t                      }t          |          }|                                r|                    d|           dS |                    d| |           t          |j                   t          j        | |           |r|	                                5 }|
                                }ddd           n# 1 swxY w Y   |                                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.
    r.   Fr/   NwT)r
   r   r4   r3   r6   r   r7   r8   copyopenreaditemssubwrite)	r*   r+   r?   r:   r<   ftextpatreplacements	            r(   migrate_filerL   j   s   
 ,,CCyyH 		%s+++uHHc***ho&&&
KS ]]__ 	6688D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 - 3 3 5 5 	. 	.C77;--DD]]3 	1GGDMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	4s$    CCCD33D7:D7c                   t                      }t          |                                           rt          | |          S t          |                                           rt          | |          S |                    d|            dS )z:Migrate one item

    dispatches to migrate_dir/_file
    zNothing to migrate for %sF)r
   r   is_filerL   is_dirr=   r3   )r*   r+   r:   s      r(   migrate_onerP      s{    
 ,,CCyy &C%%%Cyy %3$$$II)3///5r'   c                z   t                      }d}t          | d          }t          | d          }d}t          |                                          rt          j        |d          5 }|                                                                }|                                D ]A}	|	                                s+|	                                                    d          sd} nBddd           n# 1 swxY w Y   d}
t          |                                          r~t          j        |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.cssTutf-8encoding)/**z//NrU   z*/zIgnoring empty %s)r
   r   rN   rC   rD   strip
splitlinesisspace
startswithendswithr3   r   rL   )r*   r+   r:   migrated	custom_js
custom_csscustom_js_emptyrH   jslinecustom_css_emptycsss               r(   migrate_static_customrd      s   
 ,,CHS+&&Ic<((JOI   Yy7333 	q!!B   $**,,*A*ABS*T*T &+OE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 J!! KYzG444 	K&&((..""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 # |ItC7M7MNN  Zc<9P9P Q Q Os&   *A=C33C7:C77AFFFnameenv	list[Any]c                   t                      }t          t          |d          d|  d                    }t          t          |d          d|  d                    }t          t          d}g }dD ]}||z   }||z   }	t          |                                          rh ||         |                                          }
|
r-t          ||	t                    r|	                    |           |
                    d|           |S )	zWMigrate a config file.

    Includes substitutions for updated configurable names.
    profileipython__configjupyter_configjupyter_)z.pyz.json)r?   z#Not migrating empty config file: %s)r
   r   r   r	   r   r4   load_configrL   config_substitutionsappendr3   )re   rf   r:   src_basedst_baseloadersr\   extr*   r+   cfgs              r(   migrate_configrv      s*   
 ,,C43y>+-E-E-E-EFFGGH43/024Lt4L4L4LMMNNH!% G H 
F 
Fnn99 	F'#,s##//11C FS8LMMM )OOC((( 		?EEEOr'   c            	        t                      t                      t                      t          t	          t                      d                    d} d}t
                                          D ]R\  }} |j        di | } |j        di | }t	          |                                          rt          ||          rd}St          D ]}t          ||           rd}t          j        di | }t          j        di | }t	          |                                          rt          ||          rd}t          | d                    t	          j        t	          | 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_datarl   ipython_dirri   FTrl   r\   rA   rR   rS   )tzNr&   )r   r   r)   r   r   
migrationsrE   formatr4   rP   config_migrationsrv   custom_src_tcustom_dst_trd   r   rC   rG   r   nowr   utc	isoformat)
rf   r\   src_tdst_tr*   r+   re   
custom_src
custom_dstrH   s
             r(   migrater      s    )**,..&((tO--/@AABB	 C H"((**  uel!!S!!el!!S!!99 	+c3"7"7 	H!  $$$ 	H$++s++J$++s++JJ   %::z%R%R  c*+,,,	4,-z::C'	R	R	R ;VW	---7799:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ; Os   2A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:   r6   )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"   rer8   r   r   pathlibr   typingr   traitlets.config.loaderr   r	   traitlets.logr
   applicationr   pathsr   r   utilsr   r   r|   r   r   security_filer*   r+   r~   compileregexro   r)   r=   rL   rP   rd   rv   r   r   launch_instancemainr   r&   r'   r(   <module>r      sk   0 # " " " " " 				 				  ' ' ' ' ' ' ' '             L L L L L L L L $ $ $ $ $ $ # # # # # # 7 7 7 7 7 7 7 7 $ $ $ $ $ $ C_n--..DD9I>4Z4Z0[0[C_i(())33tt4Di/P/P+Q+QC[*%%&&DD1CZ,P,P(Q(Q
 s44Xx8899s44*H5566U  M
#dd;
M::
;
;C
#dd#]33
4
4CJsOO::: 

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