U
    ]'                     @   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   7/usr/lib/python3/dist-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``:)Z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   Z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   r3   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*   r5   r6   r   r   r   r   r   preparedv   s    zPluginEntryPoint.preparedc              
   C   s   | j s
t| jdkrz| 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: %sTr.   zNo installation (%r): %szOther error:(%r): %s)r*   r0   r   r   preparer   MisconfigurationErrorr5   r6   ZNoInstallationErrorZPluginError)r   r8   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   )r7   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   joinr1   r2   r    r   r*   appendr-   r9   r:   )r   linesr   r   r   __str__   s    

	zPluginEntryPoint.__str__)N)r7   
__module____qualname____doc__r   __hash__r   classmethodr	   propertyr   r   r   r   r&   r*   r-   r3   r9   r:   r=   r?   r@   rA   rF   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_resourcesZiter_entry_pointsr   ZSETUPTOOLS_PLUGINS_ENTRY_POINTZ"OLD_SETUPTOOLS_PLUGINS_ENTRY_POINTr   r
   r0   r   ZIPluginFactoryZ
providedByr   r5   Zwarning)r   rT   Zentry_pointsr   	plugin_epr   r   r   find_all   s*     zPluginsRegistry.find_allc                 C   s
   | j | S r   )rS   )r   r
   r   r   r   __getitem__   s    zPluginsRegistry.__getitem__c                 C   s
   t | jS r   )iterrS   r   r   r   r   __iter__   s    zPluginsRegistry.__iter__c                 C   s
   t | jS r   )lenrS   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"   rX   r,   r   r   
<listcomp>   s     z(PluginsRegistry.init.<locals>.<listcomp>rQ   
itervaluesrS   r+   r   r`   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
   rX   predr   r   r$      s    z)PluginsRegistry.filter.<locals>.<genexpr>)typedictrQ   rR   rS   )r   re   r   rd   r   filter   s    
zPluginsRegistry.filterc                 C   s   |  dd S )z#Filter plugins based on visibility.c                 S   s   | j  S r   )r   )rX   r   r   r   <lambda>       z)PluginsRegistry.visible.<locals>.<lambda>rh   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   ri      rj   z(PluginsRegistry.ifaces.<locals>.<lambda>rk   r(   r   rp   r   r&      s    zPluginsRegistry.ifacesc                    s   |   fddS )z%Filter plugins based on verification.c                    s
   |   S r   )r3   rn   rm   r   r   ri      rj   z(PluginsRegistry.verify.<locals>.<lambda>rk   )r   r&   r   rm   r   r3      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:   r_   r   r   r   ra      s     z+PluginsRegistry.prepare.<locals>.<listcomp>rb   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   )r@   rn   r   r   r   ri      rj   z+PluginsRegistry.available.<locals>.<lambda>rk   r   r   r   r   r@      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-   r_   pluginr   r   ra     s     z-PluginsRegistry.find_init.<locals>.<listcomp>   r   N)rQ   rc   rS   r]   r0   )r   rr   Z
candidatesr   rq   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"   ro   r   r   r   r$     s    z+PluginsRegistry.__repr__.<locals>.<genexpr>)r   	__class__r7   rC   rQ   rc   rS   r   r   r   r   rA     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   )strrw   r   r   r   r$   !  s     z*PluginsRegistry.__str__.<locals>.<genexpr>)rS   rC   rQ   rc   r   r   r   r   rF     s    zPluginsRegistry.__str__N)r7   rG   rH   rI   r   rK   rY   rZ   r\   r^   r-   rh   rl   r&   r3   r:   r@   rt   rA   rF   r   r   r   r   rM      s"   
rM   )rI   rN   rU   ZloggingrW   rQ   Zzope.interfacer1   Zzope.interface.verifyZacme.magic_typingr   r   r   r   r   Z	getLoggerr7   r5   objectr   MappingrM   r   r   r   r   <module>   s   
 #