U
    kuf(                     @  s  U d Z ddlmZ ddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZ ddlmZmZ dd	lmZ d
dlmZmZmZmZmZmZmZ d
dl m!Z!m"Z" i Z#de$d< e%ej&e're#(ej& dddZ)e#(e) i Z*de$d< e%ej+e're*(ej+ ddej,iidfdddiidfdddiidfdZ-de$d< e*(e- G dd de.Z/G d d deZ0G d!d" d"e0Z1e2d#kre03  dS )$zh
A base Application class for Jupyter applications.

All Jupyter applications should inherit from this.
    )annotationsN)deepcopy)Path)which)BoolListUnicodeobserve)Applicationcatch_config_error)ConfigFileNotFound   )allow_insecure_writesissue_insecure_write_warningjupyter_config_dirjupyter_config_pathjupyter_data_dirjupyter_pathjupyter_runtime_dir)ensure_dir_existsensure_event_loopzdict[str, t.Any]base_aliaseszApplication.log_levelzJupyterApp.config_file)z	log-levelconfig
base_flagsr
   Z	log_levelz8set log level to logging.DEBUG (maximize logging output)
JupyterAppgenerate_configTzgenerate default config file
answer_yesz1Answer yes to any questions instead of prompting.)debugzgenerate-configy_jupyter_flagsc                   @  s   e Zd ZdZdS )NoStartz6Exception to raise when an application shouldn't startN)__name__
__module____qualname____doc__ r%   r%   </tmp/pip-unpacked-wheel-a5h937xx/jupyter_core/application.pyr    B   s   r    c                      s  e Zd ZdZdZdZeZeZ	ddddZ
ee Zddd	d
Ze ZddddZeddddZe ZddddZe ZddddZeddddddZeddddZedddZdddd Zedd!dZeddd"dZddd#d$Zddd%d&Z d9d'dd( fd)d*Z!ddd+d,d-Z"ed'dd.d/Z#e Z$e%d:ddd1d2d3Z&ddd4d5Z'e(d;dddd6 fd7d8Z)  Z*S )<r   z#Base class for Jupyter applicationsZjupyterzA Jupyter Applicationintreturnc                 C  s   t jS N)loggingINFOselfr%   r%   r&   _log_level_defaultO   s    zJupyterApp._log_level_defaultz	list[str]c                 C  s   t  S r*   )r   r-   r%   r%   r&   _jupyter_path_defaultT   s    z JupyterApp._jupyter_path_defaultstrc                 C  s   t  S r*   )r   r-   r%   r%   r&   _config_dir_defaultY   s    zJupyterApp._config_dir_defaultc                 C  s"   t  }| j|kr|d| j |S )Nr   )r   
config_dirinsert)r.   pathr%   r%   r&   config_file_paths\   s    
zJupyterApp.config_file_pathsc                 C  s   t  }t|dd |S N  mode)r   r   )r.   dr%   r%   r&   _data_dir_defaultf   s    zJupyterApp._data_dir_defaultc                 C  s   t  }t|dd |S r7   )r   r   )r.   rdr%   r%   r&   _runtime_dir_defaultm   s    zJupyterApp._runtime_dir_defaultruntime_dirt.AnyNone)changer)   c                 C  s   t |d dd d S )Nnewr8   r9   )r   )r.   rB   r%   r%   r&   _runtime_dir_changedr   s    zJupyterApp._runtime_dir_changedFTzGenerate default config file.)r   helpzSpecify a config file to load.c                 C  s   | j s
dS | j ddd S )N -__config)namereplacer-   r%   r%   r&   _config_file_name_defaultz   s    z$JupyterApp._config_file_name_defaultzFull path of a config file.zAnswer yes to any prompts.c              	     s   | j r| j  ntt| j| jd  t  rz| jszd}dd fdd}| }|dsltd | }qR|d	rzd
S | 	 }td   t
t j d tjt ddd}|| W 5 Q R X d
S )z-Write our default config to a .py config filez.pyrF   r1   r(   c                    s>   d  } zt |  pdW S  tk
r8   td Y dS X d S )Nz'Overwrite %s with default config? [y/N]nrF   )inputlowerKeyboardInterruptprint)promptconfig_filer%   r&   ask   s    z,JupyterApp.write_default_config.<locals>.ask)r   rM   zPlease answer 'yes' or 'no'rM   NzWriting default config to: %sr8   wzutf-8)r:   encoding)rT   r1   r   r3   config_file_nameexistsr   
startswithrQ   Zgenerate_config_filer   parentresolveopenwrite)r.   ZanswerrU   Zconfig_textfr%   rS   r&   write_default_config   s"    

zJupyterApp.write_default_configc                 C  sz   zt t | jdd}W n( tk
r,   Y n" tk
r@   Y dS X |  dS ddlm}m} t |  spdS |  dS )z"Migrate config/data from IPython 3Zmigratedzr+Nr   )get_ipython_dirmigrate)	r   r]   r3   FileNotFoundErrorOSErrorcloserb   ra   rY   )r.   Zf_markerra   rb   r%   r%   r&   migrate_config   s    zJupyterApp.migrate_configbool)suppress_errorsr)   c                   s   | j d| j d}zt j|| jd W n" tk
rL   | j d| Y nX | jrhtj	| j\}}n| j}| j
}|r||krdS zt j||d W nR tk
r   | j d| Y n2 tk
r   |r| jrԂ | j jd|dd	 Y nX dS )
zLoad the config file.

        By default, errors in loading config are handled, and a warning
        printed on screen. For testing, the suppress_errors option is set
        to False, so errors will make tests fail.
        zSearching %s for config filesZjupyter_config)r5   zConfig file %s not foundNz#Config file not found, skipping: %szError loading config file: %sT)exc_info)logr   r6   superload_config_filer   rT   osr5   splitrX   	ExceptionZraise_config_file_errorswarning)r.   rh   Zbase_configr5   rX   	__class__r%   r&   rl      s.    

zJupyterApp.load_config_file)rJ   r)   c                 C  s   | j  d| }t|pdS )NrG   rF   )rJ   r   )r.   rJ   r%   r%   r&   _find_subcommand   s    zJupyterApp._find_subcommandc                 C  s   t | jp| jp| jS )z\Return whether we are dispatching to another command

        or running ourselves.
        )rg   r   subapp
subcommandr-   r%   r%   r&   _dispatching   s    zJupyterApp._dispatchingNargvr)   c                 C  s   |dkrt jdd }|r<| |d }|r<|| _|| _dS | | t| j}| jrZdS |   | 	  | 
| tr~t  dS )zInitialize the application.Nr   r   )sysrx   rs   ru   parse_command_liner   r   rv   rf   rl   Zupdate_configr   r   )r.   rx   ZsubcZ	cl_configr%   r%   r&   
initialize   s"    


zJupyterApp.initializec                 C  sZ   | j r,t| j | j g| jdd   t | jrB| j  t | jrV|   t dS )zStart the whole thingr   N)	ru   rm   execvrx   r    rt   startr   r`   r-   r%   r%   r&   r}     s     
zJupyterApp.startrx   kwargsr)   c                   sD   t  }zt jf d|i| W n tk
r6   Y dS X |  dS )z+Launch an instance of a Jupyter Applicationrx   N)r   rk   launch_instancer    re   )clsrx   r   looprq   r%   r&   r     s    zJupyterApp.launch_instance)T)N)N)+r!   r"   r#   r$   rJ   descriptionr   aliasesr   flagsr/   r   r   r   r0   r3   r2   propertyr6   data_dirr<   r?   r>   r	   rD   r   r   rX   rL   rT   r   r`   rf   rl   rs   rv   ru   r   r{   r}   classmethodr   __classcell__r%   r%   rq   r&   r   F   sL   
'c                   @  sl   e Zd ZdZdZdZdZddddd	d
ZddddZe	dddddddZ
e	dddddddZdS )JupyterAsyncAppz3A Jupyter application that runs on an asyncio loop.Zjupyter_asynczAn Async Jupyter ApplicationFNr@   rA   rw   c                   s   dS )z+Initialize the application asynchronoously.Nr%   )r.   rx   r%   r%   r&   initialize_async*  s    z JupyterAsyncApp.initialize_asyncr(   c                   s   dS )z%Run the application in an event loop.Nr%   r-   r%   r%   r&   start_async-  s    zJupyterAsyncApp.start_asyncr~   c                   s8   | j f |}|| ||I d H  | I d H  d S r*   )instancer{   r   r   )r   rx   r   Zappr%   r%   r&   _launch_instance0  s    
z JupyterAsyncApp._launch_instancec                 K  s.   t | j}| j|f|}|| |  dS )z2Launch an instance of an async Jupyter ApplicationN)r   _prefer_selector_loopr   Zrun_until_completere   )r   rx   r   r   coror%   r%   r&   r   7  s    

zJupyterAsyncApp.launch_instance)N)N)N)r!   r"   r#   r$   rJ   r   r   r   r   r   r   r   r%   r%   r%   r&   r   !  s   r   __main__)4r$   
__future__r   r+   rm   ry   typingtcopyr   pathlibr   shutilr   Z	traitletsr   r   r   r	   Ztraitlets.config.applicationr
   r   Ztraitlets.config.loaderr   pathsr   r   r   r   r   r   r   utilsr   r   r   __annotations__
isinstancer   dictupdateZ_jupyter_aliasesr   r   DEBUGr   ro   r    r   r   r!   r   r%   r%   r%   r&   <module>   sL   $	


 \
