
    ˌh!                       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<   ( g 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$d/d0d%Z%d.d&Z&d.d'Z'd1d(Z(d2d)Z) G d* d+e      Z*e*jV                  Z,e-d,k(  r e,        yy)3a  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   c                     t         j                  j                  dt        t	        d      j                                     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strr   
expanduser     S/var/www/html/aiagenthome/venv/lib/python3.12/site-packages/jupyter_core/migrate.pyget_ipython_dirr(   H   s,     ::>>,D,>,I,I,K(LMMr&   c                   t               }t        |       }t        |      }t        |j                               s|j	                  d|        y|j                         r<t        |j                               r|j	                  d|       y|j                          |j                  d| |       t        |j                         t        j                  | |d       y)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)srcdstlogsrc_pathdst_paths        r'   migrate_dirr;   V   s    
,CCyHCyHx!"		"C(x!"II)3/HHc*hoo&
OOCt,r&   c                "   t               }t        |      }|j                         r|j                  d|       y|j	                  d| |       t        |j                         t        j                  | |       |r|j                         5 }|j                         }ddd       |j                         D ]  \  }}|j                  |      } |j                  d      5 }|j                         ddd       yy# 1 sw Y   `xY w# 1 sw Y   yxY w)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)	r6   r7   substitutionsr8   r:   ftextpatreplacements	            r'   migrate_filerI   j   s    
 ,CCyH		%s+HHc*hoo&
KKS]]_668D  - 3 3 5C77;-D !6]]31GGDM   _  s   	C9D9DDc                    t               }t        |       j                         rt        | |      S t        |       j	                         rt        | |      S |j                  d|        y)z:Migrate one item

    dispatches to migrate_dir/_file
    zNothing to migrate for %sF)r
   r   is_filerI   is_dirr;   r/   )r6   r7   r8   s      r'   migrate_onerM      sW    
 ,CCyC%%Cy3$$II)3/r&   c                   t               }d}t        | d      }t        | d      }d}t        |      j                         rt        j                  |d      5 }|j	                         j                         }|j                         D ]6  }	|	j                         r|	j                         j                  d      r4d} n ddd       d}
t        |      j                         rbt        j                  |d      5 }|j	                         j                         }|j                  d	      xr |j                  d
      }
ddd       |r|j                  d|       |
r|j                  d|       |r|
ryt        |       |r|
st        |       |st        |t        |d            rd}|
st        |t        |d            rd}|S # 1 sw Y   xY w# 1 sw Y   xY w)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//NrR   z*/zIgnoring empty %s)r
   r   rK   r?   r@   strip
splitlinesisspace
startswithendswithr/   r   rI   )r6   r7   r8   migrated	custom_js
custom_csscustom_js_emptyrE   jslinecustom_css_emptycsss               r'   migrate_static_customra      s   
 ,CHS+&Ic<(JOI YYy73q!B$**,*A*ABS*T&+O ( 4 J!YYzG4&&(.."C"~~d3JT8J 5 		%y1		%z2+c"2#|ItC7MNZc<9P QOA 43 54s&   AGG:G9AGGGc                   t               }t        t        |d    d|  d            }t        t        |d    d|  d            }t        t        d}g }dD ]x  }||z   }||z   }	t        |      j                         s' ||   |      j                         }
|
r%t        ||	t              sU|j                  |       g|j                  d|       z |S )	zWMigrate a config file.

    Includes substitutions for updated configurable names.
    profileipython__configjupyter_configjupyter_)z.pyz.json)rD   z#Not migrating empty config file: %s)r
   r#   r   r	   r   r0   load_configrI   config_substitutionsappendr/   )nameenvr8   src_basedst_baseloadersrY   extr6   r7   cfgs              r'   migrate_configrr      s    
 ,C43y>*xvW-EFGH43/01htfG4LMNH!%G Hnn9'#,s#//1CS8LMOOC( 		?E   Or&   c            	     :   t               t               t               t        t	        t               d            d} d}t
        j                         D ]R  \  }} |j                  di | } |j                  di | }t	        |      j                         sDt        ||      sQd}T t        D ]  }t        ||       sd} t        j                  di | }t        j                  di | }t	        |      j                         rt        ||      rd}t        | d          t	        j                   t	        | d   d      dd	      5 }	|	j#                  t%        j&                  t(        j*                  
      j-                                ddd       |S # 1 sw Y   |S xY w)z(Migrate IPython configuration to Jupyterprofile_default)jupyter_datarf   ipython_dirrc   FTrf   rY   r=   rO   rP   )tzNr%   )r   r   r(   r#   r   
migrationsrA   formatr0   rM   config_migrationsrr   custom_src_tcustom_dst_tra   r   r?   rC   r   nowr   utc	isoformat)
rl   rY   src_tdst_tr6   r7   rk   
custom_src
custom_dstrE   s
             r'   migrater      sb    )*,.&(tO-/@AB	C H"((*uell!S!ell!S!9+c3"7H	 + "$$H " $$+s+J$$+s+JJ %::z%R c*+,	4,-z:C'	RVW	-779: 
S O 
S Os   AFFc                       e Zd ZdZdZdZddZy)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                P    t               s| j                  j                  d       yy)zStart the application.zFound nothing to migrate.N)r   r8   r2   )selfs    r'   startzJupyterMigrate.start  s    yHHMM56 r&   N)returnNone)__name__
__module____qualname____doc__rk   descriptionr   r%   r&   r'   r   r      s    "DK 7r&   r   __main__)r   r#   )r6   r#   r7   r#   r   bool)N)r6   
str | Pathr7   r   rD   r   r   r   )rk   r#   rl   r   r   z	list[Any])r   r   ).r   
__future__r   r    rer4   r   r   pathlibr   typingr   traitlets.config.loaderr   r	   traitlets.logr
   applicationr   pathsr   r   utilsr   r#   rx   r{   r|   security_filer6   r7   rz   compileregexri   r(   r;   rI   rM   ra   rr   r   r   launch_instancemainr   r%   r&   r'   <module>r      s  0 # 	 	  '   L $ # 7 $ _n-.D9I>4Z0[_i()3t4Di/P+Q[*%&D1CZ,P(Q
 4Xx894*H56UM
d;
M:
;C
d#]3
4CJsO V
 ; 


 

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