U
    ÓZjØ'  ã                   @   sœ   d Z ddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZ ddlmZ e e¡ZG dd„ deƒZG d	d
„ d
ejƒZdS )z.Utilities for plugins discovery and selection.é    N)ÚDict)Ú	constants)Úerrors)Ú
interfacesc                   @   sì   e Zd ZdZdddddddd	d
ddddddddgZdZdd„ Zedd„ ƒZe	dd„ ƒZ
e	dd„ ƒZe	dd„ ƒZe	dd„ ƒZd d!„ Ze	d"d#„ ƒZd6d$d%„Zd&d'„ Ze	d(d)„ ƒZd*d+„ Ze	d,d-„ ƒZe	d.d/„ ƒZe	d0d1„ ƒZd2d3„ Zd4d5„ ZdS )7ÚPluginEntryPointzPlugin entry point.Úcertbotzcertbot-apachezcertbot-dns-cloudflarezcertbot-dns-cloudxnszcertbot-dns-digitaloceanzcertbot-dns-dnsimplezcertbot-dns-dnsmadeeasyzcertbot-dns-gehirnzcertbot-dns-googlezcertbot-dns-linodezcertbot-dns-luadnszcertbot-dns-nsonezcertbot-dns-ovhzcertbot-dns-rfc2136zcertbot-dns-route53zcertbot-dns-sakuracloudzcertbot-nginxNc                 C   s,   |   |¡| _| ¡ | _|| _d | _d | _d S ©N)Úentry_point_to_plugin_nameÚnameÚloadÚ
plugin_clsÚentry_pointÚ_initializedÚ	_prepared)Úselfr   © r   úX/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot/plugins/disco.pyÚ__init__0   s
    
zPluginEntryPoint.__init__c                 C   s&   |j j| jkr|jS |j jd |j S )z)Unique plugin name for an ``entry_point``ú:)ÚdistÚkeyÚPREFIX_FREE_DISTRIBUTIONSr
   )Úclsr   r   r   r   r	   7   s    z+PluginEntryPoint.entry_point_to_plugin_namec                 C   s   | j jS )zDescription of the plugin.)r   Údescription©r   r   r   r   r   >   s    zPluginEntryPoint.descriptionc                 C   s   d  | j| j¡S )z$Description with name. Handy for UI.z	{0} ({1}))Úformatr   r
   r   r   r   r   Údescription_with_nameC   s    z&PluginEntryPoint.description_with_namec                 C   s*   z
| j jW S  tk
r$   | j Y S X dS )zLong description of the plugin.N)r   Úlong_descriptionÚAttributeErrorr   r   r   r   r   r   H   s    
z!PluginEntryPoint.long_descriptionc                 C   s   t | jddƒS )z%Should this plugin be hidden from UI?ÚhiddenF)Úgetattrr   r   r   r   r   r   P   s    zPluginEntryPoint.hiddenc                    s   | pt ‡ fdd„|D ƒƒS )z2Does plugin implements specified interface groups?c                 3   s$   | ]}t ‡ fd d„|D ƒƒV  qdS )c                 3   s   | ]}|  ˆ j¡V  qd S r   )ÚimplementedByr   ©Ú.0Úifacer   r   r   Ú	<genexpr>X   s   ÿz4PluginEntryPoint.ifaces.<locals>.<genexpr>.<genexpr>N)Úall)r#   Úifacesr   r   r   r%   W   s   þÿz*PluginEntryPoint.ifaces.<locals>.<genexpr>)Úany©r   Úifaces_groupsr   r   r   r'   U   s    ýzPluginEntryPoint.ifacesc                 C   s
   | j dk	S )z(Has the plugin been initialized already?N)r   r   r   r   r   Úinitialized\   s    zPluginEntryPoint.initializedc                 C   s&   | j s | j ¡  |  || j¡| _| jS )zMemoized plugin initialization.)r+   r   Úrequirer   r
   r   ©r   Úconfigr   r   r   Úinita   s    
zPluginEntryPoint.initc                 C   s†   | j s
t‚|D ]r}ztjj ||  ¡ ¡ W q tjjjk
r~ } z0| 	| j
¡rftjd| j
|j|dd� W Y ¢ dS d}~X Y qX qdS )z<Verify that the plugin conforms to the specified interfaces.z/%s implements %s but object does not verify: %sT©Úexc_infoFN)r+   ÚAssertionErrorÚzopeÚ	interfaceÚverifyZverifyObjectr/   Ú
exceptionsZBrokenImplementationr!   r   ÚloggerÚdebugÚ__name__)r   r'   r$   Úerrorr   r   r   r5   h   s    
   þzPluginEntryPoint.verifyc                 C   s   | j st d| ¡ | jdk	S )z%Has the plugin been prepared already?z$.prepared called on uninitialized %rN)r+   r7   r8   r   r   r   r   r   Úpreparedv   s    zPluginEntryPoint.preparedc              
   C   sà   | j s
t‚| jdkrÚz| j ¡  W n° tjk
r^ } ztjd| |dd� || _W 5 d}~X Y n| tj	k
r˜ } ztjd| |dd� || _W 5 d}~X Y nB tj
k
rÒ } ztjd| |dd� || _W 5 d}~X Y nX d| _| jS )zMemoized plugin preparation.NzMisconfigured %r: %sTr0   zNo installation (%r): %szOther error:(%r): %s)r+   r2   r   r   Úpreparer   ÚMisconfigurationErrorr7   r8   ZNoInstallationErrorZPluginError)r   r:   r   r   r   r<   }   s(    

   ÿzPluginEntryPoint.preparec                 C   s   t | jtjƒS )zIs plugin misconfigured?)Ú
isinstancer   r   r=   r   r   r   r   Úmisconfigured‘   s    zPluginEntryPoint.misconfiguredc                 C   s   t | jtƒr| jS dS )zGReturn the Exception raised during plugin setup, or None if all is wellN)r>   r   Ú	Exceptionr   r   r   r   Úproblem–   s    zPluginEntryPoint.problemc                 C   s   | j dkp| jS )z4Is plugin available, i.e. prepared or misconfigured?T)r   r?   r   r   r   r   Ú	available�   s    zPluginEntryPoint.availablec                 C   s   d  | j¡S )NzPluginEntryPoint#{0})r   r
   r   r   r   r   Ú__repr__¢   s    zPluginEntryPoint.__repr__c              
   C   s„   d  | j¡d  | jj¡d  d dd„ tj | j¡D ƒ¡¡d  | j¡g}| j	rz| 
d  |  ¡ ¡¡ | jrz| 
d	  |  ¡ ¡¡ d
 |¡S )Nz* {0}zDescription: {0}zInterfaces: {0}z, c                 s   s   | ]}|j V  qd S r   )r9   r"   r   r   r   r%   ©   s    z+PluginEntryPoint.__str__.<locals>.<genexpr>zEntry point: {0}zInitialized: {0}z	Prep: {0}Ú
)r   r
   r   r   Újoinr3   r4   r!   r   r+   Úappendr/   r;   r<   )r   Úlinesr   r   r   Ú__str__¥   s    
ÿÿ
ú	zPluginEntryPoint.__str__)N)r9   Ú
__module__Ú__qualname__Ú__doc__r   Ú__hash__r   Úclassmethodr	   Úpropertyr   r   r   r   r'   r+   r/   r5   r;   r<   r?   rA   rB   rC   rH   r   r   r   r   r      s^   ï










r   c                   @   sŒ   e Zd ZdZdd„ Ze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dd„ Zdd„ Zd S )!ÚPluginsRegistryzPlugins registry.c                 C   s   t  tt |¡ƒ¡| _d S r   )ÚcollectionsÚOrderedDictÚsortedÚsixÚ	iteritemsÚ_plugins)r   Úpluginsr   r   r   r   º   s    	zPluginsRegistry.__init__c                 C   sr   i }t  t tj¡t tj¡¡}|D ]D}t|ƒ}|j|ksBt	dƒ‚t
j |j¡r\|||j< q$t d|¡ q$| |ƒS )z+Find plugins using setuptools entry points.z#PREFIX_FREE_DISTRIBUTIONS messed upz,%r does not provide IPluginFactory, skipping)Ú	itertoolsÚchainÚpkg_resourcesÚiter_entry_pointsr   ZSETUPTOOLS_PLUGINS_ENTRY_POINTZ"OLD_SETUPTOOLS_PLUGINS_ENTRY_POINTr   r
   r2   r   ZIPluginFactoryZ
providedByr   r7   Úwarning)r   rV   Úentry_pointsr   Ú	plugin_epr   r   r   Úfind_allÅ   s*    ÿÿýÿ ÿzPluginsRegistry.find_allc                 C   s
   | j | S r   )rU   )r   r
   r   r   r   Ú__getitem__Û   s    zPluginsRegistry.__getitem__c                 C   s
   t | jƒS r   )ÚiterrU   r   r   r   r   Ú__iter__Þ   s    zPluginsRegistry.__iter__c                 C   s
   t | jƒS r   )ÚlenrU   r   r   r   r   Ú__len__á   s    zPluginsRegistry.__len__c                    s   ‡ fdd„t  | j¡D ƒS )z'Initialize all plugins in the registry.c                    s   g | ]}|  ˆ ¡‘qS r   )r/   ©r#   r]   ©r.   r   r   Ú
<listcomp>æ   s     z(PluginsRegistry.init.<locals>.<listcomp>©rS   Ú
itervaluesrU   r-   r   re   r   r/   ä   s    

ÿzPluginsRegistry.initc                    s&   t | ƒt‡ fdd„t | j¡D ƒƒƒS )z"Filter plugins based on predicate.c                 3   s"   | ]\}}ˆ |ƒr||fV  qd S r   r   )r#   r
   r]   ©Úpredr   r   r%   ë   s    ÿz)PluginsRegistry.filter.<locals>.<genexpr>)ÚtypeÚdictrS   rT   rU   )r   rj   r   ri   r   Úfilteré   s    
ÿzPluginsRegistry.filterc                 C   s   |   dd„ ¡S )z#Filter plugins based on visibility.c                 S   s   | j  S r   )r   )r]   r   r   r   Ú<lambda>ð   ó    z)PluginsRegistry.visible.<locals>.<lambda>©rm   r   r   r   r   Úvisibleî   s    zPluginsRegistry.visiblec                    s   |   ‡ fdd„¡S )z#Filter plugins based on interfaces.c                    s
   | j ˆ Ž S r   ©r'   ©Úp_ep©r*   r   r   rn   ô   ro   z(PluginsRegistry.ifaces.<locals>.<lambda>rp   r)   r   ru   r   r'   ò   s    zPluginsRegistry.ifacesc                    s   |   ‡ fdd„¡S )z%Filter plugins based on verification.c                    s
   |   ˆ ¡S r   )r5   rs   rr   r   r   rn   ø   ro   z(PluginsRegistry.verify.<locals>.<lambda>rp   )r   r'   r   rr   r   r5   ö   s    zPluginsRegistry.verifyc                 C   s   dd„ t  | j¡D ƒS )z$Prepare all plugins in the registry.c                 S   s   g | ]}|  ¡ ‘qS r   )r<   rd   r   r   r   rf   ü   s     z+PluginsRegistry.prepare.<locals>.<listcomp>rg   r   r   r   r   r<   ú   s    zPluginsRegistry.preparec                 C   s   |   dd„ ¡S )z%Filter plugins based on availability.c                 S   s   | j S r   )rB   rs   r   r   r   rn      ro   z+PluginsRegistry.available.<locals>.<lambda>rp   r   r   r   r   rB   þ   s    zPluginsRegistry.availablec                    s:   ‡ fdd„t  | j¡D ƒ}t|ƒdks*t‚|r6|d S dS )aÚ  Find an initialized plugin.

        This is particularly useful for finding a name for the plugin
        (although `.IPluginFactory.__call__` takes ``name`` as one of
        the arguments, ``IPlugin.name`` is not part of the interface)::

          # plugin is an instance providing IPlugin, initialized
          # somewhere else in the code
          plugin_registry.find_init(plugin).name

        Returns ``None`` if ``plugin`` is not found in the registry.

        c                    s"   g | ]}|j r| ¡ ˆ kr|‘qS r   )r+   r/   rd   ©Úpluginr   r   rf     s     ÿz-PluginsRegistry.find_init.<locals>.<listcomp>é   r   N)rS   rh   rU   rb   r2   )r   rw   Ú
candidatesr   rv   r   Ú	find_init  s
    zPluginsRegistry.find_initc              	   C   s(   d  | jjd dd„ t | j¡D ƒ¡¡S )Nz{0}({1})ú,c                 s   s   | ]}t |ƒV  qd S r   )Úrepr©r#   rt   r   r   r   r%     s    z+PluginsRegistry.__repr__.<locals>.<genexpr>)r   Ú	__class__r9   rE   rS   rh   rU   r   r   r   r   rC     s     

ÿÿzPluginsRegistry.__repr__c                 C   s&   | j s
dS d dd„ t | j ¡D ƒ¡S )Nz
No pluginsz

c                 s   s   | ]}t |ƒV  qd S r   )Ústrr}   r   r   r   r%   !  s     z*PluginsRegistry.__str__.<locals>.<genexpr>)rU   rE   rS   rh   r   r   r   r   rH     s    zPluginsRegistry.__str__N)r9   rI   rJ   rK   r   rM   r^   r_   ra   rc   r/   rm   rq   r'   r5   r<   rB   rz   rC   rH   r   r   r   r   rO   ·   s"   
rO   )rK   rP   rW   ÚloggingrY   rS   Zzope.interfacer3   Zzope.interface.verifyZacme.magic_typingr   r   r   r   r   Ú	getLoggerr9   r7   Úobjectr   ÚMappingrO   r   r   r   r   Ú<module>   s   
 #