a
    d                     @   s   d dl mZmZ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 edZG dd deZG dd	 d	ZG d
d dee Zdd ZdS )    )AnyDictListOptionalGenericTypeVarcast)TracebackType)entry_points)curry
PluginTypec                   @   s   e Zd Zdd Zdd ZdS )NoSuchEntryPointc                 C   s   || _ || _d S N)groupname)selfr   r    r   e/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/altair/utils/plugin_registry.py__init__   s    zNoSuchEntryPoint.__init__c                 C   s   d| j d| jS )NzNo z entry point found in group )r   r   r   r   r   r   __str__   s    zNoSuchEntryPoint.__str__N)__name__
__module____qualname__r   r   r   r   r   r   r      s   r   c                   @   sP   e Zd ZdZdedddZd dddZeee	d	d
ddZ
edddZd	S )PluginEnablera  Context manager for enabling plugins

    This object lets you use enable() as a context manager to
    temporarily enable a given plugin::

        with plugins.enable('name'):
            do_something()  # 'name' plugin temporarily enabled
        # plugins back to original state
    PluginRegistry)registryr   c                 K   s4   || _ || _|| _| | _| j j|fi | d S r   )r   r   options
_get_stateoriginal_state_enable)r   r   r   r   r   r   r   r       s
    
zPluginEnabler.__init__returnc                 C   s   | S r   r   r   r   r   r   	__enter__'   s    zPluginEnabler.__enter__N)typvalue	tracebackr"   c                 C   s   | j | j d S r   )r   
_set_stater   )r   r$   r%   r&   r   r   r   __exit__*   s    zPluginEnabler.__exit__c                 C   s   d | jjj| jS )Nz{}.enable({!r}))formatr   	__class__r   r   r   r   r   r   __repr__-   s    zPluginEnabler.__repr__)r   r   r   __doc__strr   r#   type	Exceptionr	   r(   r+   r   r   r   r   r      s
   
r   c                   @   s   e Zd ZdZi Zi ZdefeedddZ	ee
e e
e dddZee d	d
dZeeef d	ddZeeef ddddZeddddZde
e edddZeed	ddZeeeef d	ddZe
e d	ddZed	ddZdS ) r   a  A registry for plugins.

    This is a plugin registry that allows plugins to be loaded/registered
    in two ways:

    1. Through an explicit call to ``.register(name, value)``.
    2. By looking for other Python packages that are installed and provide
       a setuptools entry point group.

    When you create an instance of this class, provide the name of the
    entry point group to use::

        reg = PluginRegister('my_entrypoint_group')

     )entry_point_groupplugin_typec                 C   s6   || _ || _d| _d| _i | _i | _| jj | _dS )aJ  Create a PluginRegistry for a named entry point group.

        Parameters
        ==========
        entry_point_group: str
            The name of the entry point group.
        plugin_type: object
            A type that will optionally be used for runtime type checking of
            loaded plugins using isinstance.
        Nr0   )	r1   r2   _active_active_name_plugins_optionsr*   _global_settingscopy)r   r1   r2   r   r   r   r   J   s    zPluginRegistry.__init__)r   r%   r"   c                 C   s8   |du r| j |dS t|| js&J || j |< |S dS )a  Register a plugin by name and value.

        This method is used for explicit registration of a plugin and shouldn't be
        used to manage entry point managed plugins, which are auto-loaded.

        Parameters
        ==========
        name: str
            The name of the plugin.
        value: PluginType or None
            The actual plugin object to register or None to unregister that plugin.

        Returns
        =======
        plugin: PluginType or None
            The plugin that was registered or unregistered.
        N)r5   pop
isinstancer2   )r   r   r%   r   r   r   register]   s
    
zPluginRegistry.registerr!   c                 C   s<   t | j }t| j}dd |D }|| tt|S )z:List the names of the registered and entry points plugins.c                 S   s   g | ]
}|j qS r   r   .0epr   r   r   
<listcomp>z       z(PluginRegistry.names.<locals>.<listcomp>)listr5   keysimportlib_metadata_getr1   extendsortedset)r   extsZe_pointsZ	more_extsr   r   r   namesv   s
    

zPluginRegistry.namesc                 C   s&   | j | j| j | j | j dS )zBReturn a dictionary representing the current state of the registry)r3   r4   r5   r6   r7   )r3   r4   r5   r8   r6   r7   r   r   r   r   r   ~   s    zPluginRegistry._get_stateN)stater"   c                 C   s:   t | h dksJ | D ]\}}t| || q dS )zReset the state of the registry>   r6   r7   r3   r5   r4   N)rG   rC   itemssetattr)r   rJ   keyvalr   r   r   r'      s    zPluginRegistry._set_state)r   r"   c              
      s    | j vrz fddt| jD \}W nN tyv } z6 | jv rTt| j  |nt| j |W Y d }~n
d }~0 0 tt| }| 	 |  | _
| j   | _t| t| j @ D ]}||| j|< q|| _d S )Nc                    s   g | ]}|j  kr|qS r   r<   r=   r<   r   r   r@      s   
z*PluginRegistry._enable.<locals>.<listcomp>)r5   rD   r1   
ValueErrorentrypoint_err_messagesr   r   r   loadr;   r4   r3   rG   rC   r7   r9   r6   )r   r   r   r?   errr%   rM   r   r<   r   r       s     


$zPluginRegistry._enablec                 K   s    |du r| j }t| |fi |S )a  Enable a plugin by name.

        This can be either called directly, or used as a context manager.

        Parameters
        ----------
        name : string (optional)
            The name of the plugin to enable. If not specified, then use the
            current active name.
        **options :
            Any additional parameters will be passed to the plugin as keyword
            arguments

        Returns
        -------
        PluginEnabler:
            An object that allows enable() to be used as a context manager
        N)activer   )r   r   r   r   r   r   enable   s    zPluginRegistry.enablec                 C   s   | j S )z.Return the name of the currently active plugin)r4   r   r   r   r   rS      s    zPluginRegistry.activec                 C   s   | j S )z%Return the current options dictionary)r6   r   r   r   r   r      s    zPluginRegistry.optionsc                 C   s$   | j rt| jfi | j S | jS dS )z#Return the currently active plugin.N)r6   r   r3   r   r   r   r   get   s    zPluginRegistry.getc                 C   s   d | jj| jt|  S )Nz {}(active={!r}, registered={!r}))r)   r*   r   r4   rB   rI   r   r   r   r   r+      s    zPluginRegistry.__repr__)N)r   r   r   r,   rP   r7   objectr-   r.   r   r   r   r;   r   rI   r   r   r   r'   r    r   rT   propertyrS   r   rU   r+   r   r   r   r   r   1   s    
r   c                 C   s,   t  }t|dr|j| dS || g S d S )Nselect)r   )r
   hasattrrX   rU   )r   r?   r   r   r   rD      s    
rD   N)typingr   r   r   r   r   r   r   typesr	   importlib.metadatar
   Ztoolzr   r   r/   r   r   r   rD   r   r   r   r   <module>   s   $	 '