o
    !wi!                  
   @  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}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                   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.
    
IPYTHONDIRz
~/.ipython)osenvirongetr   r   
expanduser r$   r$   Q/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/jupyter_core/migrate.pyget_ipython_dirH   s   r&   srcdstboolc                 C  s   t  }t| }t|}t| s|d|  dS | r/t| r+|d| dS |  |d| | 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   anyiterdirdebugexistsrmdirinfor   parentshutilcopytree)r'   r(   logsrc_pathdst_pathr$   r$   r%   migrate_dirV   s   
r9   
str | Pathsubstitutionsr   c           	      C  s   t  }t|}| r|d| dS |d| | t|j t| | |ri|	 }|
 }W d   n1 s:w   Y  | D ]
\}}|||}qC|	d}|| W d   dS 1 sdw   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   r0   r/   r2   r   r3   r4   copyopenreaditemssubwrite)	r'   r(   r;   r6   r8   ftextpatreplacementr$   r$   r%   migrate_filej   s(   



rG   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_filerG   is_dirr9   r/   )r'   r(   r6   r$   r$   r%   migrate_one   s   

rJ   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.cssTutf-8encoding)/**z//NrN   z*/zIgnoring empty %s)r	   r   rH   r>   r?   strip
splitlinesisspace
startswithendswithr/   r   rG   )r'   r(   r6   migrated	custom_js
custom_csscustom_js_emptyrC   jslinecustom_css_emptycssr$   r$   r%   migrate_static_custom   sD   

r]   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.
    profileipython__configjupyter_configjupyter_)z.pyz.json)r;   z#Not migrating empty config file: %s)r	   r   r   r   r   r0   load_configrG   config_substitutionsappendr/   )r^   r_   r6   src_basedst_baseloadersrU   extr'   r(   cfgr$   r$   r%   migrate_config   s&   
rn   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 Jupyterprofile_default)jupyter_datard   ipython_dirra   FTrd   rU   r<   rK   rL   )tzNr$   )r   r   r&   r   r   
migrationsr@   formatr0   rJ   config_migrationsrn   custom_src_tcustom_dst_tr]   r   r>   rB   r   nowr   utc	isoformat)
r_   rU   src_tdst_tr'   r(   r^   
custom_src
custom_dstrC   r$   r$   r%   migrate   s6   

r   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)r   r6   r2   )selfr$   r$   r%   start  s   zJupyterMigrate.startN)r   r   )__name__
__module____qualname____doc__r^   descriptionr   r$   r$   r$   r%   r      s
    r   __main__)r   r   )r'   r   r(   r   r   r)   )N)r'   r:   r(   r:   r;   r   r   r)   )r^   r   r_   r   r   r`   )r   r)   ).r   
__future__r   r    rer4   r   r   pathlibr   typingr   traitlets.config.loaderr   r   traitlets.logr	   applicationr   pathsr   r   utilsr   r   rs   rv   rw   security_filer'   r(   ru   compileregexrg   r&   r9   rG   rJ   r]   rn   r   r   launch_instancemainr   r$   r$   r$   r%   <module>   sV   

	



0
!
