o
    e                     @   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   LD:\Projects\ConvertPro\env\Lib\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__      zNoSuchEntryPoint.__str__N)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   sT   e Zd ZdZdddefddZddd	Zd
edede	ddfddZ
def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
    registryPluginRegistryr   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__typvalue	tracebackNc                 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__-   r   zPluginEnabler.__repr__)r"   r   )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dedefddZ	dede
e d	e
e fd
dZd	ee fddZd	eeef fddZdeeef d	dfddZded	dfddZd de
e d	efddZed	efddZed	eeef fddZd	e
e fddZd	ef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   s4   |du r| j |dS t|| jsJ || j |< |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.registerc                 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   s    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_statestateNc                 C   s:   t | h dksJ | D ]
\}}t| || qdS )zReset the state of the registry>   r7   r3   r5   r4   r6   N)rF   rB   itemssetattr)r   rI   keyvalr   r   r   r'      s   zPluginRegistry._set_statec              
      s    | j vrBz fddt| jD \}W n  ty4 } z | jv r)t| j  |t| j |d }~ww tt| }| 	 |  | _
| j   | _t| t| j @ D ]
}||| j|< qX|| _d S )Nc                    s   g | ]	}|j  kr|qS r   r<   r=   r<   r   r   r@      s
    
z*PluginRegistry._enable.<locals>.<listcomp>)r5   rC   r1   
ValueErrorentrypoint_err_messagesr   r   r   loadr;   r4   r3   rF   rB   r7   r9   r6   )r   r   r   r?   errr%   rL   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      | j S )z.Return the name of the currently active plugin)r4   r   r   r   r   rR         zPluginRegistry.activec                 C   rT   )z%Return the current options dictionary)r6   r   r   r   r   r      rU   zPluginRegistry.optionsc                 C   s    | j rt| jfi | j S | jS )z#Return the currently active plugin.)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   rA   rH   r   r   r   r   r+      s   zPluginRegistry.__repr__r   )r   r   r   r,   rO   r7   objectr-   r.   r   r   r   r;   r   rH   r   r   r   r'   r!   r   rS   propertyrR   r   rV   r+   r   r   r   r   r   1   s"    
r   c                 C   s(   t  }t|dr|j| dS || g S )Nselect)r   )r
   hasattrrY   rV   )r   r?   r   r   r   rC      s   
rC   N)typingr   r   r   r   r   r   r   typesr	   importlib.metadatar
   Ztoolzr   r   r/   r   r   r   rC   r   r   r   r   <module>   s   $ 	 '