o
    i                   
   @   s  d 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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< qcg 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 d1d$d%Z!d&d' Z"d(d) Z#d*d+ Z$d,d- Z%G d.d/ d/eZ&e&j'Z(e)d0kre(  dS dS )2a  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


    N)datetimetimezone)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   c                   C   s   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 r"   r"   O/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyter_core/migrate.pyget_ipython_dirD   s   r$   c                 C   s   t  }t| s|d|  dS tj|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   dirnameshutilcopytreesrcdstlogr"   r"   r#   migrate_dirR   s   


r4   c                 C   s   t  }tj|r|d| dS |d| | ttj| t	| | |rnt
|dd}| }W d   n1 s=w   Y  | D ]
\}}|||}qFt
|ddd}|| W d   dS 1 siw   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-   r.   copyopenreaditemssubwrite)r1   r2   substitutionsr3   ftextpatreplacementr"   r"   r#   migrate_filee   s&   

rD   c                 C   sB   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    isfilerD   isdirr4   r)   r0   r"   r"   r#   migrate_one|   s   

rG   c                 C   sf  t  }d}t| d}t| d}d}tj|rIt|dd$}|  }| D ]}	|		 s9|	 
ds9d} nq(W d   n1 sDw   Y  d}
tj|rwt|dd}|  }|
d	og|d
}
W d   n1 srw   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.cssTr5   r6   )/**z//NrH   z*/zIgnoring empty %s)r   pjoinr   r    rE   r:   r;   strip
splitlinesisspace
startswithendswithr)   r   rD   )r1   r2   r3   migrated	custom_js
custom_csscustom_js_emptyr@   jslinecustom_css_emptycssr"   r"   r#   migrate_static_custom   sD   

rX   c                 C   s   t  }tddjd
d| i|}tddjd
d| i|}ttd}g }dD ]-}|| }|| }	tj|rS|| | }
|
rMt	||	t
drL|| q&|d| q&|S )zWMigrate a config file.

    Includes substitutions for updated configurable names.
    r   zipython_{name}_confignamer   zjupyter_{name}_config)z.pyz.json)r?   z#Not migrating empty config file: %sNr"   )r   rJ   formatr   r   r   r    r*   load_configrD   config_substitutionsappendr)   )rY   envr3   src_basedst_baseloadersrP   extr1   r2   cfgr"   r"   r#   migrate_config   s&   
rd   c            
      C   s(  t  t t tjt dd} d}t D ]!\}}|jdi | }|jdi | }tj	|r7t
||r7d}qtD ]	}t|| rCd}q:tjdi | }tjdi | }tj	|rat||rad}t| d  ttj| 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_datajupyter_configipython_dirprofileFTrg   rP   r8   r5   r6   )tzNr"   )r
   r	   r$   r   r    join
migrationsr<   rZ   r*   rG   config_migrationsrd   custom_src_tcustom_dst_trX   r   r:   r>   r   nowr   utc	isoformat)
r^   rP   src_tdst_tr1   r2   rY   
custom_src
custom_dstr@   r"   r"   r#   migrate   s6   

rw   c                   @   s    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.
    c                 C   s   t  s| jd dS dS )zStart the application.zFound nothing to migrate.N)rw   r3   r,   )selfr"   r"   r#   start  s   zJupyterMigrate.startN)__name__
__module____qualname____doc__rY   descriptionrz   r"   r"   r"   r#   rx      s
    rx   __main__)N)*r~   r   rer.   r   r   traitlets.config.loaderr   r   traitlets.logr   applicationr   pathsr	   r
   utilsr   r    rk   rJ   rl   rn   ro   security_filer1   r2   rm   compileregexr\   r$   r4   rD   rG   rX   rd   rw   rx   launch_instancemainr{   r"   r"   r"   r#   <module>   sR   


	
0!
