o
    h'                     @  sT  d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZ d dlmZ ejdkr8d dlmZ nd dlmZ ejd	krJd d
lmZ nd d
lmZ erXd dlmZ edZedZede
def efdZedee	 dZe
egeee	  f Zd!ddZG dd deZG dd deeef ZG dd deeef Z dd  Z!dS )"    )annotationsN)partial)entry_points)TYPE_CHECKINGAnyCallableGenericTypeVarcast)deprecated_warn)      )TypeIs)r      )TypeAliasType)TracebackTypeTRPlugin.)type_paramsPluginT)boundtptype[T]return#Callable[[object], TypeIs[type[T]]]c                  s   d fdd}|S )	zq
    Converts a type to guard function.

    Added for compatibility with original `PluginRegistry` default.
    objobjectr   TypeIs[type[T]]c                  s
   t |  S N)
isinstance)r   r    P/var/www/vscode/kcb/lib/python3.10/site-packages/altair/utils/plugin_registry.pyfunc$   s   
z_is_type.<locals>.funcN)r   r   r   r   r"   )r   r$   r"   r!   r#   _is_type   s   r%   c                   @  s   e Zd Zdd Zdd ZdS )NoSuchEntryPointc                 C  s   || _ || _d S r   )groupname)selfr'   r(   r"   r"   r#   __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                   @  s8   e Zd ZdZdd
dZdddZdddZ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
    registryPluginRegistry[PluginT, R]r(   stroptionsr   r   Nonec                 K  s4   || _ || _|| _| | _| j j|fi | d S r   )r1   r(   r4   
_get_stateoriginal_state_enable)r)   r1   r(   r4   r"   r"   r#   r*   ?   s
   
zPluginEnabler.__init__PluginEnabler[PluginT, R]c                 C  s   | S r   r"   r+   r"   r"   r#   	__enter__H   s   zPluginEnabler.__enter__typtypevalue	Exception	tracebackr   c                 C  s   | j | j d S r   )r1   
_set_stater7   )r)   r;   r=   r?   r"   r"   r#   __exit__K   s   zPluginEnabler.__exit__c                 C  s   t | jj d| jdS )Nz.enable())r<   r1   r-   r(   r+   r"   r"   r#   __repr__N   s   zPluginEnabler.__repr__N)r1   r2   r(   r3   r4   r   r   r5   )r   r9   )r;   r<   r=   r>   r?   r   r   r5   r   r3   )r-   r.   r/   __doc__r*   r:   rA   rC   r"   r"   r"   r#   r0   3   s    

	
r0   c                   @  s   e Zd ZU dZi Zded< i Zded< defd.ddZd/ddZ	d0ddZ
d1ddZd2ddZd3ddZ	d4d5d#d$Zed6d%d&Zed1d'd(Zd7d*d+Zd6d,d-ZdS )8PluginRegistrya  
    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")

    zdict[str, str]entrypoint_err_messagesdict[str, Any]_global_settings entry_point_groupr3   plugin_typeIsPluginr   r5   c                 C  s|   || _ |  |tur&t|tr&dt| jd}t|dd ttt|| _	n|| _	d| _
d| _i | _i | _| jj | _dS )a  
        Create a PluginRegistry for a named entry point group.

        Parameters
        ----------
        entry_point_group: str
            The name of the entry point group.
        plugin_type
            A type narrowing function that will optionally be used for runtime
            type checking loaded plugins.

        References
        ----------
        https://typing.readthedocs.io/en/latest/spec/narrowing.html
        z<Pass a callable `TypeIs` function to `plugin_type` instead.
z~(plugin_type)

See also:
https://typing.readthedocs.io/en/latest/spec/narrowing.html
https://docs.astral.sh/ruff/rules/assert/z5.4.0)versionNrJ   )rK   callabler    r<   r-   r   r
   rM   r%   rL   _active_active_name_plugins_options	__class__rI   copy)r)   rK   rL   msgr"   r"   r#   r*   l   s   zPluginRegistry.__init__r(   r=   PluginT | Nonec                 C  sP   |du r| j |dS | |r|| j |< |S t|jdt| j}t|)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.
        Nz is not compatible with )rR   poprL   r<   r-   	TypeError)r)   r(   r=   rV   r"   r"   r#   register   s   

zPluginRegistry.register	list[str]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>   s    z(PluginRegistry.names.<locals>.<listcomp>)listrR   keysimportlib_metadata_getrK   extendsortedset)r)   extse_points	more_extsr"   r"   r#   names   s
   

zPluginRegistry.namesc                 C  s&   | j | j| j | j | j dS )zCReturn a dictionary representing the current state of the registry.)rP   rQ   rR   rS   rI   )rP   rQ   rR   rU   rS   rI   r+   r"   r"   r#   r6      s   zPluginRegistry._get_statestatec                 C  s:   t | h dksJ | D ]
\}}t| || qdS )z Reset the state of the registry.>   rP   rS   rR   rQ   rI   N)rf   rb   itemssetattr)r)   rk   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                 3  s    | ]
}|j  kr|V  qd S r   r\   r]   r\   r"   r#   	<genexpr>   s    
z)PluginRegistry._enable.<locals>.<genexpr>)rR   rc   rK   
ValueErrorrG   r&   r
   r   loadrZ   rQ   rP   rf   rb   rI   rX   rS   )r)   r(   r4   r_   errr=   rn   r"   r\   r#   r8      s$   



zPluginRegistry._enableN
str | Noner4   r   r9   c                 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)activer0   )r)   r(   r4   r"   r"   r#   enable   s   zPluginRegistry.enablec                 C     | j S )z/Return the name of the currently active plugin.)rQ   r+   r"   r"   r#   ru         zPluginRegistry.activec                 C  rw   )z&Return the current options dictionary.)rS   r+   r"   r"   r#   r4      rx   zPluginRegistry.optionspartial[R] | Plugin[R] | Nonec                 C  sl   | j  }r| |r| jrt|fi | jS |S | j dur0t| jdt| j jd}t|tr4tdS )z#Return the currently active plugin.Nz2 requires all plugins to be callable objects, but z is not callable.)	rP   rL   rS   r   r<   r-   rY   r   NotImplementedError)r)   r$   rV   r"   r"   r#   get  s   

zPluginRegistry.getc                 C  s"   t | j d| jd|  dS )Nz(active=z, registered=rB   )r<   r-   ru   rj   r+   r"   r"   r#   rC     s   "zPluginRegistry.__repr__)rK   r3   rL   rM   r   r5   )r(   r3   r=   rW   r   rW   )r   r[   )r   rH   )rk   rH   r   r5   )r(   r3   r   r5   r   )r(   rt   r4   r   r   r9   rD   )r   ry   )r-   r.   r/   rE   rG   __annotations__rI   rO   r*   rZ   rj   r6   r@   r8   rv   propertyru   r4   r{   rC   r"   r"   r"   r#   rF   R   s&   
 
&





rF   c                 C  s(   t  }t|dr|j| dS || g S )Nselect)r'   )r   hasattrr~   r{   )r'   r_   r"   r"   r#   rc     s   
rc   )r   r   r   r   )"
__future__r   sys	functoolsr   importlib.metadatar   typingr   r   r   r   r	   r
   altair.utils.deprecationr   version_infor   typing_extensionsr   typesr   r   r   r   r   r   rM   r%   r>   r&   r0   rF   rc   r"   r"   r"   r#   <module>   s2     


	 H