a
    `z/                     @   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	Zddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ zddlmZ W n ey   ddlmZ Y n0 eeZg d	ZG d
d deZG dd deZdS )z.Utilities for plugins discovery and selection.    N)Dict)errors)
interfaces)	constants)os)Mapping)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-nginxc                   @   s   e Zd ZdZdZd*ddZdd Zedd	 Ze	d
d Z
e	dd Ze	dd Zejdd Ze	dd Zejdd Zdd Ze	dd Zd+ddZdd Ze	dd Zdd Ze	d d! Ze	d"d# Ze	d$d% Zd&d' Zd(d) ZdS ),PluginEntryPointzPlugin entry point.NFc                 C   s@   |  ||| _| | _|| _d | _d | _d | _d| _d | _	d S )NF)
entry_point_to_plugin_namenameload
plugin_clsentry_pointwarning_message_initialized	_prepared_hidden_long_description)selfr   with_prefix r   A/usr/lib/python3/dist-packages/certbot/_internal/plugins/disco.py__init__6   s    
zPluginEntryPoint.__init__c                 C   s$   || j kr | jrt| j dS dS )z(Check if the name refers to this plugin.TF)r   r   loggerwarningr   r   r   r   r   
check_name@   s
    
zPluginEntryPoint.check_namec                 C   s   |r|j jd |j S |jS )z)Unique plugin name for an ``entry_point``:)distkeyr   )clsr   r   r   r   r   r
   H   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!   O   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_nameT   s    z&PluginEntryPoint.description_with_namec                 C   s4   | j r| j S z
| jjW S  ty.   | j Y S 0 dS )zLong description of the plugin.N)r   r   long_descriptionAttributeErrorr!   r"   r   r   r   r%   Y   s    
z!PluginEntryPoint.long_descriptionc                 C   s
   || _ d S N)r   )r   r!   r   r   r   r%   c   s    c                 C   s   | j pt| jddS )z%Should this plugin be hidden from UI?hiddenF)r   getattrr   r"   r   r   r   r(   g   s    zPluginEntryPoint.hiddenc                 C   s
   || _ d S r'   )r   )r   Zhider   r   r   r(   l   s    c                    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>s   s   z4PluginEntryPoint.ifaces.<locals>.<genexpr>.<genexpr>N)all)r,   ifacesr"   r   r   r.   r   s   z*PluginEntryPoint.ifaces.<locals>.<genexpr>)anyr   ifaces_groupsr   r"   r   r0   p   s    zPluginEntryPoint.ifacesc                 C   s
   | j duS )z(Has the plugin been initialized already?N)r   r"   r   r   r   initializedw   s    zPluginEntryPoint.initializedc                 C   s&   | j s | j  | || j| _| jS )zMemoized plugin initialization.)r4   r   Zrequirer   r   r   r   configr   r   r   init|   s    
zPluginEntryPoint.initc                 C   s   | j s
J |D ]p}ztjj||   W q tjjjy| } z4|| j	rdt
jd| j	|j|dd W Y d}~ dS d}~0 0 qdS )z<Verify that the plugin conforms to the specified interfaces.z/%s implements %s but object does not verify: %sTexc_infoNF)r4   zope	interfaceverifyZverifyObjectr7   
exceptionsZBrokenImplementationr*   r   r   debug__name__)r   r0   r-   errorr   r   r   r<      s    
zPluginEntryPoint.verifyc                 C   s   | j std|  | jduS )z%Has the plugin been prepared already?z$.prepared called on uninitialized %rN)r4   r   r>   r   r"   r   r   r   prepared   s    zPluginEntryPoint.preparedc              
   C   s   | j s
J | jdu rz| j  W n tjy` } z$tjd| |dd || _W Y d}~nd}~0  tjy } z$tjd| |dd || _W Y d}~nLd}~0  tj	y } z$tjd| |dd || _W Y d}~nd}~0 0 d| _| jS )zMemoized plugin preparation.NzMisconfigured %r: %sTr8   zNo installation (%r): %szOther error:(%r): %s)
r4   r   r   preparer   MisconfigurationErrorr   r>   ZNoInstallationErrorZPluginError)r   r@   r   r   r   rB      s"    

zPluginEntryPoint.preparec                 C   s   t | jtjS )zIs plugin misconfigured?)
isinstancer   r   rC   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)rD   r   	Exceptionr"   r   r   r   problem   s    zPluginEntryPoint.problemc                 C   s   | j du p| jS )z4Is plugin available, i.e. prepared or misconfigured?T)r   rE   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'   )r?   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!   joinr:   r;   r*   r   r4   appendr7   rA   rB   )r   linesr   r   r   __str__   s    

	zPluginEntryPoint.__str__)F)N)r?   
__module____qualname____doc____hash__r   r   classmethodr
   propertyr!   r$   r%   setterr(   r0   r4   r7   r<   rA   rB   rE   rG   rH   rI   rN   r   r   r   r   r	   0   sD   





	








r	   c                   @   s   e Zd ZdZdd Ze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   s   i }t d}|r|dng }tj| |D ]}tj| q0t	
ttjttj}|D ]j}| j||dd}|jdusJ |jjtvrb| j||dd}d|_d|j|j}	|	|_d|	|j|_qb| |S )	z+Find plugins using setuptools entry points.ZCERTBOT_PLUGIN_PATHr   F)r   NTzRPlugin legacy name {0} may be removed in a future version. Please use {1} instead.z(WARNING: {0}) {1})r   getenvsplitsyspathextendpkg_resourcesZworking_setZ	add_entry	itertoolschainZiter_entry_pointsr   ZSETUPTOOLS_PLUGINS_ENTRY_POINTZ"OLD_SETUPTOOLS_PLUGINS_ENTRY_POINT_load_entry_pointr   r   PREFIX_FREE_DISTRIBUTIONSr(   r#   r   r   r%   )
r    r]   Zplugin_paths_stringZplugin_pathsZplugin_pathZentry_pointsr   	plugin_epZprefixed_plugin_epmessager   r   r   find_all   s:    
zPluginsRegistry.find_allc                 C   sh   t ||}|j|v r>||j }td|j|jjj|jjjtj	|j
rX|||j< ntd| |S )Nz+Duplicate plugin name {0} from {1} and {2}.z,%r does not provide IPluginFactory, skipping)r	   r   rF   r#   r   r   r   r   ZIPluginFactoryZ
providedByr   r   r   )r    r   r]   r   rh   Zother_epr   r   r   rf      s    


z!PluginsRegistry._load_entry_pointc                 C   s
   | j | S r'   )r\   r   r   r   r   __getitem__  s    zPluginsRegistry.__getitem__c                 C   s
   t | jS r'   )iterr\   r"   r   r   r   __iter__  s    zPluginsRegistry.__iter__c                 C   s
   t | jS r'   )lenr\   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   )r7   r,   rh   r6   r   r   
<listcomp>      z(PluginsRegistry.init.<locals>.<listcomp>rZ   
itervaluesr\   r5   r   rq   r   r7     s    

zPluginsRegistry.initc                    s"   t |  fddt| jD S )z"Filter plugins based on predicate.c                    s   i | ]\}} |r||qS r   r   )r,   r   rh   predr   r   
<dictcomp>  s   z*PluginsRegistry.filter.<locals>.<dictcomp>)typerZ   r[   r\   )r   rw   r   rv   r   filter  s    
zPluginsRegistry.filterc                 C   s   |  dd S )z#Filter plugins based on visibility.c                 S   s   | j  S r'   )r(   )rh   r   r   r   <lambda>   rs   z)PluginsRegistry.visible.<locals>.<lambda>rz   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'   r0   p_epr3   r   r   r{   $  rs   z(PluginsRegistry.ifaces.<locals>.<lambda>r|   r2   r   r   r   r0   "  s    zPluginsRegistry.ifacesc                    s   |   fddS )z%Filter plugins based on verification.c                    s
   |   S r'   )r<   r   r~   r   r   r{   (  rs   z(PluginsRegistry.verify.<locals>.<lambda>r|   )r   r0   r   r~   r   r<   &  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   )rB   rp   r   r   r   rr   ,  rs   z+PluginsRegistry.prepare.<locals>.<listcomp>rt   r"   r   r   r   rB   *  s    zPluginsRegistry.preparec                 C   s   |  dd S )z%Filter plugins based on availability.c                 S   s   | j S r'   )rH   r   r   r   r   r{   0  rs   z+PluginsRegistry.available.<locals>.<lambda>r|   r"   r   r   r   rH   .  s    zPluginsRegistry.availablec                    s:    fddt | jD }t|dks*J |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|  u r|qS r   )r4   r7   rp   pluginr   r   rr   B  s   z-PluginsRegistry.find_init.<locals>.<listcomp>   r   N)rZ   ru   r\   rn   )r   r   Z
candidatesr   r   r   	find_init3  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,   r   r   r   r   r.   K  s   z+PluginsRegistry.__repr__.<locals>.<genexpr>)r#   	__class__r?   rK   rZ   ru   r\   r"   r   r   r   rI   I  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.   Q  rs   z*PluginsRegistry.__str__.<locals>.<genexpr>)r\   rK   rZ   ru   r"   r   r   r   rN   N  s    zPluginsRegistry.__str__N)r?   rO   rP   rQ   r   rS   rj   rf   rk   rm   ro   r7   rz   r}   r0   r<   rB   rH   r   rI   rN   r   r   r   r   rV      s&   

rV   )rQ   rW   rd   Zloggingr`   rc   rZ   Zzope.interfacer:   Zzope.interface.verifyZacme.magic_typingr   r   r   r   Zcertbot._internalr   Zcertbot.compatr   collections.abcr   ImportErrorZ	getLoggerr?   r   rg   objectr	   rV   r   r   r   r   <module>   s,   
 #