o
    Zh!                  
   @  s  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< qg dZej Z!e!dde!dde!dde!dd e!d!d"iZ"d?d%d&Z#d@d*d+Z$dAdBd/d0Z%d@d1d2Z&d@d3d4Z'dCd8d9Z(dDd:d;Z)G d<d= d=eZ*e*j+Z,e-d>kre,  dS dS )Ea  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}Znbextensionsz{jupyter_data}Zkernelsz	{profile}Znbconfigz{jupyter_config}ZstaticZcustom)Znotebook_secretZnotebook_cookie_secretznbsignatures.dbsecurity)notebook	nbconvertZ	qtconsolez\bIPythonQtConsoleApp\bZJupyterQtConsoleAppz\bIPythonWidget\bZJupyterWidgetz\bRichIPythonWidget\bZRichJupyterWidgetz\bIPython\.html\br   z\bIPython\.nbconvert\br   returnstrc                   C  s   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.
    Z
IPYTHONDIRz
~/.ipython)osenvirongetr   r   
expanduser r   r   K/var/www/html/lang_env/lib/python3.10/site-packages/jupyter_core/migrate.pyget_ipython_dirH   s   r   srcdstboolc                 C  s   t  }t| s|d|  dS t| r)t|r#|d| dS t|  |d| | tt|j	 t
j| |d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   parentshutilcopytreer   r   logr   r   r   migrate_dirV   s   

r+   
str | Pathsubstitutionsr   c                 C  s   t  }t| r|d| dS |d| | tt|j t| | |rstj	t|dd}|
 }W d   n1 s?w   Y  | D ]
\}}|||}qHtj	t|ddd}|| W d   dS 1 snw   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   utf-8encodingNwT)r	   r   r#   r"   r%   r   r&   r'   copyopenreaditemssubwrite)r   r   r-   r*   ftextpatreplacementr   r   r   migrate_fileh   s&   

r<   c                 C  sB   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_filer<   is_dirr+   r"   r)   r   r   r   migrate_one   s   

r?   c                 C  sj  t  }d}t| d}t| d}d}t| rJtj|dd$}|  }| D ]}	|	 s:|	 ds:d} nq)W d   n1 sEw   Y  d}
t| rytj|dd}|  }|d	oi|	d
}
W d   n1 stw   Y  |r|
d| |
r|
d| |r|
rdS t| |r|
st| |st|t|drd}|
st|t|drd}|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.   r/   )/**z//Nr@   z*/zIgnoring empty %s)r	   r   r=   r3   r4   strip
splitlinesisspace
startswithendswithr"   r   r<   )r   r   r*   migratedZ	custom_jsZ
custom_cssZcustom_js_emptyr8   jslineZcustom_css_emptycssr   r   r   migrate_static_custom   sD   

rK   nameenv	list[Any]c                 C  s   t  }tt|d  d|  d}tt|d  d|  d}ttd}g }dD ]-}|| }|| }	t| rU|| | }
|
rOt||	tdrN|	| q(|
d| q(|S )	zWMigrate a config file.

    Includes substitutions for updated configurable names.
    profileZipython__configjupyter_configZjupyter_)z.pyz.json)r-   z#Not migrating empty config file: %s)r	   r   r   r   r   r#   Zload_configr<   config_substitutionsappendr"   )rL   rM   r*   Zsrc_baseZdst_baseloadersrG   extr   r   cfgr   r   r   migrate_config   s&   
rW   c            
      C  s&  t  t t ttt dd} d}t D ]!\}}|jdi | }|jdi | }t| r7t	||r7d}qt
D ]	}t|| rCd}q:tjdi | }tjdi | }t| rat||rad}t| d  tjt| d dddd	}	|	tjtjd
  W d   |S 1 sw   Y  |S )z(Migrate IPython configuration to JupyterZprofile_default)Zjupyter_datarQ   Zipython_dirrO   FTrQ   rG   r1   r.   r/   )tzNr   )r   r   r   r   r   
migrationsr5   formatr#   r?   config_migrationsrW   custom_src_tcustom_dst_trK   r   r3   r7   r   nowr   utc	isoformat)
rM   rG   Zsrc_tZdst_tr   r   rL   Z
custom_srcZ
custom_dstr8   r   r   r   migrate   s6   

ra   c                   @  s"   e Zd ZdZdZdZd	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                 C  s   t  s| jd dS dS )zStart the application.zFound nothing to migrate.N)ra   r*   r%   )selfr   r   r   start  s   zJupyterMigrate.startN)r   rc   )__name__
__module____qualname____doc__rL   descriptionre   r   r   r   r   rb      s
    rb   __main__)r   r   )r   r   r   r   r   r   )N)r   r,   r   r,   r-   r   r   r   )rL   r   rM   r   r   rN   )r   r   ).ri   
__future__r   r   rer'   r   r   pathlibr   typingr   Ztraitlets.config.loaderr   r   Ztraitlets.logr	   Zapplicationr   pathsr   r   utilsr   r   rY   r\   r]   Zsecurity_filer   r   r[   compileregexrR   r   r+   r<   r?   rK   rW   ra   rb   Zlaunch_instancemainrf   r   r   r   r   <module>   sV   

	



0
!
