a
    ø£BeÕ-  ã                   @   s¶   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ dd	lmZ d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdZG dd„ dƒZdS )é    )Úwraps)Údeepcopy)Ú
TraitError)ÚConfig)Ú
JupyterApp)Ú	ServerApp)ÚExtensionAppé   )ÚNotebookAppTraitsc                 C   s   dj | d�S )NzÛ'{trait_name}' was found in both NotebookApp and ServerApp. This is likely a recent change. This config will only be set in NotebookApp. Please check if you should also config these traits in ServerApp for your purpose.©Ú
trait_name©Úformatr   © r   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/notebook_shim/shim.pyÚNBAPP_AND_SVAPP_SHIM_MSG   s    ûÿr   c                 C   s   dj | d�S )Nz—'{trait_name}' has moved from NotebookApp to ServerApp. This config will be passed to ServerApp. Be sure to update your config before our next release.r   r   r   r   r   r   ÚNBAPP_TO_SVAPP_SHIM_MSG   s    üÿr   c                 C   s   dj | |d�S )Nzð'{trait_name}' is found in {extapp_name}, NotebookApp, and ServerApp. This is a recent change. This config will only be set in {extapp_name}. Please check if you should also config these traits in NotebookApp and ServerApp for your purpose.©r   Úextapp_namer   r   r   r   r   Ú#EXTAPP_AND_NBAPP_AND_SVAPP_SHIM_MSG"   s
    úÿr   c                 C   s   dj | |d�S )Nz×'{trait_name}' is found in both {extapp_name} and ServerApp. This is a recent change. This config will only be set in {extapp_name}. Please check if you should also config these traits in ServerApp for your purpose.r   r   r   r   r   r   ÚEXTAPP_AND_SVAPP_SHIM_MSG.   s
    úÿr   c                 C   s   dj | |d�S )NzÛ'{trait_name}' is found in both {extapp_name} and NotebookApp. This is a recent change. This config will only be set in {extapp_name}. Please check if you should also config these traits in NotebookApp for your purpose.r   r   r   r   r   r   ÚEXTAPP_AND_NBAPP_SHIM_MSG:   s
    úÿr   c                 C   s   dj | |d�S )Na  '{trait_name}' is not found in {extapp_name}, but it was found in both NotebookApp and ServerApp. This is likely a recent change. This config will only be set in ServerApp. Please check if you should also config these traits in NotebookApp for your purpose.r   r   r   r   r   r   Ú#NOT_EXTAPP_NBAPP_AND_SVAPP_SHIM_MSGF   s
    ùÿr   c                 C   s   dj | |d�S )Nzp'{trait_name}' has moved from {extapp_name} to ServerApp. Be sure to update your config before our next release.r   r   r   r   r   r   ÚEXTAPP_TO_SVAPP_SHIM_MSGS   s
    üÿr   c                 C   s   dj | |d�S )Nzr'{trait_name}' has moved from {extapp_name} to NotebookApp. Be sure to update your config before our next release.r   r   r   r   r   r   ÚEXTAPP_TO_NBAPP_SHIM_MSG]   s
    üÿr   )Zopen_browserÚ	log_levelZ
log_formatZdefault_urlZshow_bannerc                       s2   e Zd ZdZeejƒ‡ fdd„ƒZdd„ Z‡  ZS )ÚNotebookConfigShimMixinaô  A Mixin class for shimming configuration from
    NotebookApp to ServerApp. This class handles warnings, errors,
    etc.

    This class should be used during a transition period for apps
    that are switching from depending on NotebookApp to ServerApp.

    After one release cycle, this class can be safely removed
    from the inheriting class.

    TL;DR

    The entry point to shimming is at the `update_config` method.
    Once traits are loaded, before updating config across all
    configurable objects, this class injects a method to reroute
    traits to their *most logical* classes.

    This class raises warnings when:
        1. a trait has moved.
        2. a trait is redundant across classes.

    Redundant traits across multiple classes now must be
    configured separately, *or* removed from their old
    location to avoid this warning.

    For a longer description on how individual traits are handled,
    read the docstring under `shim_config_from_notebook_to_jupyter_server`.
    c                    s   |   |¡}tƒ  |¡ d S )N)Ú+shim_config_from_notebook_to_jupyter_serverÚsuperÚupdate_config)ÚselfÚconfigZshimmed_config©Ú	__class__r   r   r   Š   s    ÿz%NotebookConfigShimMixin.update_configc                 C   sº  | j j}| di ¡}| di ¡}| |i ¡}t|ƒ}i }i }i }	| j  ¡ t ¡  }
t ¡ }t ¡ t ¡  }| |¡ d}| 	¡ D ] \}}||v }||v }|t
v r°| ||i¡ n`|rÐ|rÐt|ƒ}| ||i¡ n@|rìt|ƒ}| ||i¡ n$|�r| ||i¡ ntd |¡ƒ‚|r€| j |¡ q€d}| 	¡ D �]P\}}||
v }||v }||v }|t
v �rl|	 ||i¡ �n t|||gƒ�r–t||ƒ}|	 ||i¡ nÖ|�r¼|�r¼t||ƒ}|	 ||i¡ n°|�râ|�rât||ƒ}|	 ||i¡ nŠ|�rø|	 ||i¡ nt|�r|�rt||ƒ}| ||i¡ nN|�r>t||ƒ}| ||i¡ n.|�r^t||ƒ}| ||i¡ ntd |¡ƒ‚|�r.| j |¡ �q.t||dœƒ}|	�r¬| t| j j|	iƒ¡ | |¡ |S )uÜ  Reorganizes a config object to reroute traits to their expected destinations
        after the transition from NotebookApp to ServerApp.

        A detailed explanation of how traits are handled:

        1. If the argument is prefixed with `ServerApp`,
            pass this trait to `ServerApp`.
        2. If the argument is prefixed with `NotebookApp`,
            * If the argument is a trait of `NotebookApp` *and* `ServerApp`:
                1. Raise a warningâ€”**for the extension developers**â€”that
                    there's redundant traits.
                2. Pass trait to `NotebookApp`.
            * If the argument is a trait of just `ServerApp` only
                (i.e. the trait moved from `NotebookApp` to `ServerApp`):
                1. Raise a "this trait has moved" **for the user**.
                3. Pass trait to `ServerApp`.
            * If the argument is a trait of `NotebookApp` only, pass trait
                to `NotebookApp`.
            * If the argument is not found in any object, raise a
                `"Trait not found."` error.
        3. If the argument is prefixed with `ExtensionApp`:
            * If the argument is a trait of `ExtensionApp`,
                `NotebookApp`, and `ServerApp`,
                1. Raise a warning about redundancy.
                2. Pass to the ExtensionApp
            * If the argument is a trait of `ExtensionApp` and `NotebookApp`,
                1. Raise a warning about redundancy.
                2. Pass to ExtensionApp.
            * If the argument is a trait of `ExtensionApp` and `ServerApp`,
                1. Raise a warning about redundancy.
                2. Pass to ExtensionApp.
            * If the argument is a trait of `ExtensionApp`.
                1. Pass to ExtensionApp.
            * If the argument is a trait of `NotebookApp` but not `ExtensionApp`,
                1. Raise a warning that trait has likely moved to NotebookApp.
                2. Pass to NotebookApp
            * If the arguent is a trait of `ServerApp` but not `ExtensionApp`,
                1. Raise a warning that the trait has likely moved to ServerApp.
                2. Pass to ServerApp.
            * else
                * Raise a TraitError: "trait not found."
        ÚNotebookAppr   NzTrait, {}, not found.)r$   r   )r#   Ú__name__Úpopr   Zclass_trait_namesr   r   r
   ÚupdateÚitemsÚIGNORED_TRAITSr   r   r   r   ÚlogÚwarningÚallr   r   r   r   r   r   r   )r    r!   r   Znbapp_configZsvapp_configZextapp_configZconfig_shimZsvapp_config_shimZnbapp_config_shimZextapp_config_shimZextapp_traitsZsvapp_traitsZnbapp_traitsZwarning_msgr   Ztrait_valueZin_svappZin_nbappZ	in_extappZ
new_configr   r   r   r   ‘   sº    +ÿÿÿÿ

þþþþþþþÿ
zCNotebookConfigShimMixin.shim_config_from_notebook_to_jupyter_server)	r%   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Ú__classcell__r   r   r"   r   r   l   s   r   N)Ú	functoolsr   Úcopyr   Z	traitletsr   Ztraitlets.config.loaderr   Zjupyter_core.applicationr   Zjupyter_server.serverappr   Z$jupyter_server.extension.applicationr   Ztraitsr
   r   r   r   r   r   r   r   r   r)   r   r   r   r   r   Ú<module>   s"   

