U
    lu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mZ ddlm	Z	 ddl
Z
ddlmZ ddlmZmZmZmZmZ ddlmZ dd	lmZ d
dlmZ dddddZded< dddiidfdddiidfdZded< G dd deZejZdS )znbclient cli.    )annotationsN)Path)dedent)
JupyterApp)BoolIntegerListUnicodedefault)catch_config_error)__version__   )NotebookClientzNbClientApp.timeoutzNbClientApp.startup_timeoutzNbClientApp.kernel_namezNbClientApp.output_base)timeoutstartup_timeoutkernel_nameoutputzdict[str, str]nbclient_aliasesNbClientAppallow_errorsTzLErrors are ignored and execution is continued until the end of the notebook.inplacez/Overwrite input notebook with executed results.)zallow-errorsr   zdict[str, typing.Any]nbclient_flagsc                      s<  e Zd ZdZeeZdZeZ	e
ZdZee ddjddZedded	d
jddZededdjddZededdjddZededdjddZededdjddZededdjddZeddedd
jddZedddddZed&ddd fddZddd d!Zd"dd#d$d%Z  Z S )'r   zE
    An application used to execute notebook files (``*.ipynb``)
    zjupyter-executez7An application used to execute notebook files (*.ipynb)zPath of notebooks to convert)helpT)configNz
            The time to wait (in seconds) for output from executions.
            If a cell execution takes longer, a TimeoutError is raised.
            ``-1`` will disable the timeout.
            )
allow_noner   <   z
            The time to wait (in seconds) for the kernel to start.
            If kernel startup takes longer, a RuntimeError is
            raised.
            FaB  
            When a cell raises an error the default behavior is that
            execution is stopped and a :py:class:`nbclient.exceptions.CellExecutionError`
            is raised.
            If this flag is provided, errors are ignored and execution
            is continued until the end of the notebook.
            zskip-executionz^
            Name of the cell tag to use to denote a cell that should be skipped.
             z
            Name of kernel to use to execute the cells.
            If not set, use the kernel_spec embedded in the notebook.
            z
            Default is execute notebook without writing the newly executed notebook.
            If this flag is provided, the newly generated notebook will
            overwrite the input notebook.
            a8  
            Write executed notebook to this file base name.
            Supports pattern replacements ``'{notebook_name}'``,
            the name of the input notebook file without extension.
            Note that output is always relative to the parent directory of the
            input notebook.
            Z	log_levelint)returnc                 C  s   t jS )N)loggingINFO)self r"   0/tmp/pip-unpacked-wheel-72rqhnyi/nbclient/cli.py_log_level_default   s    zNbClientApp._log_level_defaultzlist[str] | NoneNone)argvr   c                   sn   t  | |  | _| js&td t| jdkrT| jdk	rTd| jkrTd}t|| jD ]}| 	| qZdS )zInitialize the app.r   Nz{notebook_name}zgIf passing multiple notebooks with `--output=output` option, output string must contain {notebook_name})
super
initializeget_notebooks	notebookssysexitlenoutput_base
ValueErrorrun_notebook)r!   r&   msgpath	__class__r"   r#   r)      s    



zNbClientApp.initializez	list[str]c                 C  s   | j r| j }n| j}|S )zGet the notebooks for the app.)
extra_argsr+   )r!   r+   r"   r"   r#   r*      s    zNbClientApp.get_notebooksstr)notebook_pathr   c           	   
   C  s  | j d|  t|d}|j }| jr6|}n0| jrb|j| jj	|dj
dd}nd}|r|j sd|j d}t|| }tj|dd	}W 5 Q R X t|| j| j| j| j| jd
d|iid}|  |r| j d|  t|| dS )zRun a notebook by path.z
Executing z.ipynbr   )Znotebook_nameNzCannot write to directory=z that does not exist   )Z
as_versionmetadatar3   )r   r   skip_cells_with_tagr   r   	resourceszSave executed results to )loginfor   with_suffixparentabsoluter   r/   joinpathformatnameis_dirr0   opennbformatreadr   r   r   r;   r   r   executewrite)	r!   r8   Z
input_pathr3   Zoutput_pathr2   fnbclientr"   r"   r#   r1      s<    


zNbClientApp.run_notebook)N)!__name__
__module____qualname____doc__r	   r   versionrD   r   aliasesr   flagsdescriptionr   tagr+   r   r   r   r   r   r   r;   r   r   r/   r
   r$   r   r)   r*   r1   __classcell__r"   r"   r4   r#   r   0   s   
	
		
)rQ   
__future__r   r   r,   typingpathlibr   textwrapr   rG   Zjupyter_core.applicationr   Z	traitletsr   r   r   r	   r
   Ztraitlets.configr   Znbclientr   rM   r   r   __annotations__r   r   Zlaunch_instancemainr"   r"   r"   r#   <module>   sF   
 
  0