a
    /§»a 9  ã                   @   s¬   d dl Z d dlZd dlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZ ejdkrdd dlmZ nd dlZdd	„ ZG d
d„ deƒZG dd„ dƒZG dd„ dƒZdd„ ZdS )é    Né   )Ú_tracing)Ú_ResultÚ
_multicall)ÚHookImplÚ
_HookRelayÚ_HookCallerÚnormalize_hookimpl_opts)é   é   ©Úmetadatac                 C   s"   t j| t| ƒ|jj|jjd� d S )N)ÚlinenoÚfilename)ÚwarningsÚwarn_explicitÚtypeÚ__code__Úco_firstlinenoÚco_filename)ÚwarningÚfunction© r   úI/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/pluggy/_manager.pyÚ_warn_for_function   s    ür   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚPluginValidationErrorz�plugin failed validation.

    :param object plugin: the plugin which failed validation,
        may be a module or an arbitrary object.
    c                    s   || _ tt| ƒ |¡ d S ©N)ÚpluginÚsuperÚ	ExceptionÚ__init__)Úselfr   Úmessage©Ú	__class__r   r   r       s    zPluginValidationError.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    Ú__classcell__r   r   r#   r   r      s   r   c                   @   s6   e Zd ZdZdd„ Zedd„ ƒZddd„Zd	d
„ ZdS )Ú
DistFacadez$Emulate a pkg_resources Distributionc                 C   s
   || _ d S r   )Ú_dist)r!   Údistr   r   r   r    '   s    zDistFacade.__init__c                 C   s
   | j d S )NÚnamer   ©r!   r   r   r   Úproject_name*   s    zDistFacade.project_nameNc                 C   s   t | j||ƒS r   )Úgetattrr+   )r!   ÚattrÚdefaultr   r   r   Ú__getattr__.   s    zDistFacade.__getattr__c                 C   s   t t| jƒddg ƒS )Nr+   r/   )ÚsortedÚdirr+   r.   r   r   r   Ú__dir__1   s    zDistFacade.__dir__)N)	r%   r&   r'   r(   r    Úpropertyr/   r3   r6   r   r   r   r   r*   $   s   

r*   c                   @   sÖ   e Zd ZdZdd„ Zdd„ Zd3dd„Zd	d
„ Zd4dd„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d„ Zdd „ Zd!d"„ Zd#d$„ Zd5d%d&„Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ ZdS )6ÚPluginManagera•  Core :py:class:`.PluginManager` class which manages registration
    of plugin objects and 1:N hook calling.

    You can register new hooks by calling :py:meth:`add_hookspecs(module_or_class)
    <.PluginManager.add_hookspecs>`.
    You can register plugin objects (which contain hooks) by calling
    :py:meth:`register(plugin) <.PluginManager.register>`.  The :py:class:`.PluginManager`
    is initialized with a prefix that is searched for in the names of the dict
    of registered plugin objects.

    For debugging purposes you can call :py:meth:`.PluginManager.enable_tracing`
    which will subsequently send debug information to the trace helper.
    c                 C   s:   || _ i | _i | _g | _t ¡  d¡| _tƒ | _	t
| _d S )NZpluginmanage)r/   Ú_name2pluginÚ_plugin2hookcallersÚ_plugin_distinfor   Z	TagTracerÚgetÚtracer   Úhookr   Ú_inner_hookexec)r!   r/   r   r   r   r    D   s    zPluginManager.__init__c                 C   s   |   ||||¡S r   ©r?   )r!   Ú	hook_nameÚmethodsÚkwargsÚfirstresultr   r   r   Ú	_hookexecM   s    zPluginManager._hookexecNc           	      C   s  |p|   |¡}|| jv s"|| jv rL| j |d¡du r8dS td||| jf ƒ‚|| j|< g  | j|< }t|ƒD ]¬}|  ||¡}|durlt|ƒ t||ƒ}t	||||ƒ}| d¡p°|}t| j
|dƒ}|du rät|| jƒ}t| j
||ƒ n | ¡ �r|  ||¡ | |¡ | |¡ | |¡ ql|S )z¹Register a plugin and return its canonical name or ``None`` if the name
        is blocked from registering.  Raise a :py:class:`ValueError` if the plugin
        is already registered.éÿÿÿÿNz#Plugin already registered: %s=%s
%sZspecname)Úget_canonical_namer9   r:   r<   Ú
ValueErrorr5   Úparse_hookimpl_optsr	   r0   r   r>   r   rE   ÚsetattrÚhas_specÚ_verify_hookZ_maybe_apply_historyÚ_add_hookimplÚappend)	r!   r   r-   Úplugin_nameZhookcallersZhookimpl_optsÚmethodÚhookimplr>   r   r   r   ÚregisterR   s8    
ÿÿ




zPluginManager.registerc                 C   s`   t ||ƒ}t |¡sd S zt || jd d ƒ}W n tyD   i }Y n0 |d ur\t|tƒs\d }|S )NÚ_impl)r0   ÚinspectÚ	isroutiner/   r   Ú
isinstanceÚdict)r!   r   r-   rP   Úresr   r   r   rI   x   s    


z!PluginManager.parse_hookimpl_optsc                 C   sj   |du r"|dusJ dƒ‚|   |¡}|du r4|  |¡}| j |¡rH| j|= | j |g ¡D ]}| |¡ qV|S )zlunregister a plugin object and all its contained hook implementations
        from internal data structures.Nz+one of name or plugin needs to be specified)Úget_nameÚ
get_pluginr9   r<   r:   ÚpopZ_remove_plugin)r!   r   r-   Z
hookcallerr   r   r   Ú
unregister…   s    

zPluginManager.unregisterc                 C   s   | j |d� d| j|< dS )zHblock registrations of the given name, unregister if already registered.©r-   N)r\   r9   ©r!   r-   r   r   r   Úset_blocked˜   s    zPluginManager.set_blockedc                 C   s   || j v o| j | du S )z4return ``True`` if the given plugin name is blocked.N)r9   r^   r   r   r   Ú
is_blocked�   s    zPluginManager.is_blockedc                 C   sª   g }t |ƒD ]~}|  ||¡}|durt| j|dƒ}|du rZt|| j||ƒ}t| j||ƒ n&| ||¡ | ¡ D ]}|  	||¡ qn| 
|¡ q|s¦td| j›d|›�ƒ‚dS )z�add new hook specifications defined in the given ``module_or_class``.
        Functions are recognized if they have been decorated accordingly.Nzdid not find any z
 hooks in )r5   Úparse_hookspec_optsr0   r>   r   rE   rJ   Zset_specificationÚget_hookimplsrL   rN   rH   r/   )r!   Úmodule_or_classÚnamesr-   Z	spec_optsÚhcZhookfunctionr   r   r   Úadd_hookspecs¡   s     ÿzPluginManager.add_hookspecsc                 C   s   t ||ƒ}t || jd d ƒS )NÚ_spec)r0   r/   )r!   rc   r-   rP   r   r   r   ra   ¸   s    
z!PluginManager.parse_hookspec_optsc                 C   s
   t | jƒS )z%return the set of registered plugins.)Úsetr:   r.   r   r   r   Úget_plugins¼   s    zPluginManager.get_pluginsc                 C   s
   || j v S )z4Return ``True`` if the plugin is already registered.)r:   ©r!   r   r   r   r   Úis_registeredÀ   s    zPluginManager.is_registeredc                 C   s   t |ddƒptt|ƒƒS )aV  Return canonical name for a plugin object. Note that a plugin
        may be registered under a different name which was specified
        by the caller of :py:meth:`register(plugin, name) <.PluginManager.register>`.
        To obtain the name of an registered plugin use :py:meth:`get_name(plugin)
        <.PluginManager.get_name>` instead.r%   N)r0   ÚstrÚidrj   r   r   r   rG   Ä   s    z PluginManager.get_canonical_namec                 C   s   | j  |¡S )z/Return a plugin or ``None`` for the given name.)r9   r<   r^   r   r   r   rZ   Ì   s    zPluginManager.get_pluginc                 C   s   |   |¡duS )z>Return ``True`` if a plugin with the given name is registered.N)rZ   r^   r   r   r   Ú
has_pluginÐ   s    zPluginManager.has_pluginc                 C   s(   | j  ¡ D ]\}}||kr
|  S q
dS )z@Return name for registered plugin or ``None`` if not registered.N)r9   Úitems)r!   r   r-   Úvalr   r   r   rY   Ô   s    zPluginManager.get_namec                 C   s°   |  ¡ r&|jr&t|jd|j|jf ƒ‚|jjr>t|jj|j	ƒ t
|jƒt
|jjƒ }|rzt|jd|j|jt|j	ƒ|f ƒ‚|jr¬t |j	¡s¬t|jd|j|jt|j	ƒf ƒ‚d S )Nz6Plugin %r
hook %r
historic incompatible to hookwrapperz~Plugin %r for hook %r
hookimpl definition: %s
Argument(s) %s are declared in the hookimpl but can not be found in the hookspeczsPlugin %r for hook %r
hookimpl definition: %s
Declared as hookwrapper=True but function is not a generator function)Zis_historicZhookwrapperr   r   rO   r-   ÚspecZwarn_on_implr   r   rh   ÚargnamesÚ
_formatdefrT   Úisgeneratorfunction)r!   r>   rQ   Z	notinspecr   r   r   rL   Ú   s8    
ÿþüýþþþzPluginManager._verify_hookc                 C   s\   | j jD ]N}|d dkrt| j |ƒ}| ¡ s| ¡ D ] }|js4t|jd||jf ƒ‚q4qdS )z™Verify that all hooks which have not been verified against
        a hook specification are optional, otherwise raise :py:class:`.PluginValidationError`.r   Ú_zunknown hook %r in plugin %rN)r>   Ú__dict__r0   rK   rb   Zoptionalhookr   r   )r!   r-   r>   rQ   r   r   r   Úcheck_pendingý   s    ÿþzPluginManager.check_pendingc                 C   s�   d}t t ¡ ƒD ]z}|jD ]n}|j|ks|dur:|j|ks|  |j¡s|  |j¡rTq| ¡ }| j	||jd� | j
 |t|ƒf¡ |d7 }qq|S )a*  Load modules from querying the specified setuptools ``group``.

        :param str group: entry point group to load plugins
        :param str name: if given, loads only plugins with the given ``name``.
        :rtype: int
        :return: return the number of loaded plugins by this call.
        r   Nr]   r   )ÚlistÚimportlib_metadataÚdistributionsZentry_pointsÚgroupr-   rZ   r`   ÚloadrR   r;   rN   r*   )r!   r{   r-   Úcountr,   Úepr   r   r   r   Úload_setuptools_entrypoints  s&    
ÿþþ
ü
ûz)PluginManager.load_setuptools_entrypointsc                 C   s
   t | jƒS )zTreturn list of distinfo/plugin tuples for all setuptools registered
        plugins.)rx   r;   r.   r   r   r   Úlist_plugin_distinfo%  s    z"PluginManager.list_plugin_distinfoc                 C   s   t | j ¡ ƒS )z!return list of name/plugin pairs.)rx   r9   ro   r.   r   r   r   Úlist_name_plugin*  s    zPluginManager.list_name_pluginc                 C   s   | j  |¡S )z.get all hook callers for the specified plugin.)r:   r<   rj   r   r   r   Úget_hookcallers.  s    zPluginManager.get_hookcallersc                    s.   ˆj ‰‡ ‡‡fdd„}|ˆ_ ‡‡fdd„}|S )aP  add before/after tracing functions for all hooks
        and return an undo function which, when called,
        will remove the added tracers.

        ``before(hook_name, hook_impls, kwargs)`` will be called ahead
        of all hook calls and receive a hookcaller instance, a list
        of HookImpl instances and the keyword arguments for the hook call.

        ``after(outcome, hook_name, hook_impls, kwargs)`` receives the
        same arguments as ``before`` but also a :py:class:`pluggy._callers._Result` object
        which represents the result of the overall hook call.
        c                    s<   ˆˆˆˆƒ t  ‡ ‡‡‡‡fdd„¡}ˆ|ˆˆˆƒ | ¡ S )Nc                      s   ˆˆˆˆˆ ƒS r   r   r   )rD   Ú
hook_implsrA   rC   Úoldcallr   r   Ú<lambda>D  ó    zPPluginManager.add_hookcall_monitoring.<locals>.traced_hookexec.<locals>.<lambda>)r   Z	from_callÚ
get_result)rA   rƒ   rC   rD   Úoutcome)ÚafterÚbeforer„   )rD   rƒ   rA   rC   r   Útraced_hookexecA  s    ÿz>PluginManager.add_hookcall_monitoring.<locals>.traced_hookexecc                      s
   ˆ ˆ_ d S r   r@   r   )r„   r!   r   r   ÚundoK  s    z3PluginManager.add_hookcall_monitoring.<locals>.undor@   )r!   rŠ   r‰   r‹   rŒ   r   )r‰   rŠ   r„   r!   r   Úadd_hookcall_monitoring2  s
    z%PluginManager.add_hookcall_monitoringc                    s2   | j j d¡‰ ‡ fdd„}‡ fdd„}|  ||¡S )z9enable tracing of hook calls and return an undo function.r>   c                    s   ˆ j  jd7  _ˆ | |ƒ d S )Nr   )ÚrootÚindent)rA   rB   rC   ©Z	hooktracer   r   rŠ   T  s    z,PluginManager.enable_tracing.<locals>.beforec                    s0   | j d u rˆ d|d|  ¡ ƒ ˆ j jd8  _d S )NÚfinishz-->r   )Úexcinfor‡   rŽ   r�   )rˆ   rA   rB   rC   r�   r   r   r‰   X  s    
z+PluginManager.enable_tracing.<locals>.after)r=   rŽ   r<   r�   )r!   rŠ   r‰   r   r�   r   Úenable_tracingP  s    zPluginManager.enable_tracingc                    s~   t | jˆ ƒ}‡ fdd„|D ƒ}|rzt|j|j|jj|jjƒ}| ¡ D ]0}|j	}||vrD| 
|¡ | j |g ¡ |¡ qD|S |S )z­Return a new :py:class:`._hooks._HookCaller` instance for the named method
        which manages calls to all registered plugins except the
        ones from remove_plugins.c                    s   g | ]}t |ˆ ƒr|‘qS r   )Úhasattr)Ú.0Zplugr]   r   r   Ú
<listcomp>d  r†   z4PluginManager.subset_hook_caller.<locals>.<listcomp>)r0   r>   r   r-   rE   rq   Ú	namespaceÚoptsrb   r   rM   r:   Ú
setdefaultrN   )r!   r-   Zremove_pluginsÚorigZplugins_to_removere   rQ   r   r   r]   r   Úsubset_hook_caller_  s    ÿ
z PluginManager.subset_hook_caller)N)NN)N)r%   r&   r'   r(   r    rE   rR   rI   r\   r_   r`   rf   ra   ri   rk   rG   rZ   rn   rY   rL   rw   r   r€   r�   r‚   r�   r“   r›   r   r   r   r   r8   5   s2   	
&
#
r8   c                 C   s   | j › t | ¡› �S r   )r%   rT   Ú	signature)Úfuncr   r   r   rs   t  s    rs   )rT   Úsysr   Ú r   Z_callersr   r   Z_hooksr   r   r   r	   Úversion_infoÚ	importlibr   ry   r   r   r   r*   r8   rs   r   r   r   r   Ú<module>   s   
	  A