
    M/e,                        d 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 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 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 ddlmZ ej0                  dk\  rddlmZ nddlZ ej8                  e      Zej>                  ej@                  ejB                  gZ"	  G d d      Z# G d de
      Z$y)z.Utilities for plugins discovery and selection.    N)Callable)cast)Dict)Iterable)Iterator)List)Mapping)Optional)Type)Union)configuration)errors)
interfaces)	constants)os)Error)   
   c                      e Zd ZdZdZdej                  ddfdZdee	   de
fdZedej                  de	fd       Zede	fd	       Zede	fd
       Zede	fd       Zede
fd       Zdee   de
fdZede
fd       Zddeej0                     dej4                  fdZede
fd       Zdee
ef   fdZede
fd       Z edee!   fd       Z"ede
fd       Z#de	fdZ$de	fdZ%y)PluginEntryPointzPlugin entry point.Nentry_pointreturnc                     | j                  |      | _        |j                         | _        || _        d | _        d | _        d | _        y N)entry_point_to_plugin_namenameload
plugin_clsr   warning_message_initialized	_prepared)selfr   s     A/usr/lib/python3/dist-packages/certbot/_internal/plugins/disco.py__init__zPluginEntryPoint.__init__(   sC    33K@	3>3C3C3E&.29=7;    r   c                 $    || j                   k(  ryy)z(Check if the name refers to this plugin.TFr   r"   r   s     r#   
check_namezPluginEntryPoint.check_name0   s    499r%   c                     |j                   S )z)Unique plugin name for an ``entry_point``r'   )clsr   s     r#   r   z+PluginEntryPoint.entry_point_to_plugin_name6   s     r%   c                 .    | j                   j                  S )zDescription of the plugin.)r   descriptionr"   s    r#   r-   zPluginEntryPoint.description;   s     ***r%   c                 N    dj                  | j                  | j                        S )z$Description with name. Handy for UI.z	{0} ({1}))formatr-   r   r.   s    r#   description_with_namez&PluginEntryPoint.description_with_name@   s!     !!$"2"2DII>>r%   c                 D    t        | j                  d| j                        S )zLong description of the plugin.long_description)getattrr   r-   r.   s    r#   r3   z!PluginEntryPoint.long_descriptionE   s     t(:D<L<LMMr%   c                 0    t        | j                  dd      S )z%Should this plugin be hidden from UI?hiddenF)r4   r   r.   s    r#   r6   zPluginEntryPoint.hiddenJ   s     t%88r%   ifaces_groupsc                 6     | xs t         fd|D              S )z1Does plugin implement specified interface groups?c              3   F   K   | ]  }t        fd |D                yw)c              3   J   K   | ]  }t        j                  |        y wr   )
issubclassr   .0ifacer"   s     r#   	<genexpr>z4PluginEntryPoint.ifaces.<locals>.<genexpr>.<genexpr>R   s$      % 4??E2 %s    #N)all)r=   ifacesr"   s     r#   r?   z*PluginEntryPoint.ifaces.<locals>.<genexpr>Q   s-      ()   %#% %()s   !)anyr"   r7   s   ` r#   rA   zPluginEntryPoint.ifacesO   s)       )C () (() %) 	)r%   c                     | j                   duS )z(Has the plugin been initialized already?N)r    r.   s    r#   initializedzPluginEntryPoint.initializedV   s       ,,r%   configc                 t    | j                   s!| j                  || j                        | _         | j                   S )zMemoized plugin initialization.)r    r   r   )r"   rF   s     r#   initzPluginEntryPoint.init[   s0       !%		 BD   r%   c                 b    | j                   st        j                  d|        | j                  duS )z%Has the plugin been prepared already?z$.prepared called on uninitialized %rN)rE   loggerdebugr!   r.   s    r#   preparedzPluginEntryPoint.preparedc   s+     LL?F~~T))r%   c                 F   | j                   t        d      | j                  "	 | j                   j                          d| _        t        t        t        t        f   | j                        S # t        j
                  $ r*}t        j                  d| |d       || _        Y d}~_d}~wt        j                  $ r*}t        j                  d| |d       || _        Y d}~d}~wt        j                  $ r*}t        j                  d| |d       || _        Y d}~d}~ww xY w)zMemoized plugin preparation.NzPlugin is not initialized.TzMisconfigured %r: %s)exc_infozNo installation (%r): %szOther error:(%r): %s)r    
ValueErrorr!   preparer   MisconfigurationErrorrJ   rK   NoInstallationErrorPluginErrorr   r   boolr   )r"   errors     r#   rP   zPluginEntryPoint.preparej   s    $9::>>!&!!))+ "&E$+&77 // '3T54P!&-- '.ed  L!&%% '3T54P!&'s/   A- -D   B%%D ; C  D 6 DD c                 J    t        | j                  t        j                        S )zIs plugin misconfigured?)
isinstancer!   r   rQ   r.   s    r#   misconfiguredzPluginEntryPoint.misconfigured   s     $..&*F*FGGr%   c                 P    t        | j                  t              r| j                  S y)zGReturn the Exception raised during plugin setup, or None if all is wellN)rW   r!   	Exceptionr.   s    r#   problemzPluginEntryPoint.problem   s     dnni0>>!r%   c                 :    | j                   du xs | j                  S )z4Is plugin available, i.e. prepared or misconfigured?T)r!   rX   r.   s    r#   	availablezPluginEntryPoint.available   s     ~~%;););;r%   c                 8    dj                  | j                        S )NzPluginEntryPoint#{0})r0   r   r.   s    r#   __repr__zPluginEntryPoint.__repr__   s    %,,TYY77r%   c                     dj                   j                        dj                   j                  j                        dj                  dj	                   fdt
        D                    dj                   j                        g} j                  rh|j                  dj                   j                                       j                  r.|j                  dj                   j                                      d	j	                  |      S )
Nz* {0}zDescription: {0}zInterfaces: {0}z, c              3   b   K   | ]&  }t        j                  |      r|j                   ( y wr   )r;   r   __name__r<   s     r#   r?   z+PluginEntryPoint.__str__.<locals>.<genexpr>   s+      /#(doou5 /s   ,/zEntry point: {0}zInitialized: {0}z	Prep: {0}
)r0   r   r   r-   joinPLUGIN_INTERFACESr   rE   appendrH   rL   rP   )r"   liness   ` r#   __str__zPluginEntryPoint.__str__   s    NN499%%%doo&A&AB$$TYY /,=/ &  %%d&6&67
 LL+22499;?@}}[//?@yyr%   r   )&rb   
__module____qualname____doc____hash__importlib_metadata
EntryPointr$   r
   strrT   r)   classmethodr   propertyr-   r1   r3   r6   r   r   rA   rE   r   NamespaceConfigr   PluginrH   rL   r   r   rP   rX   rZ   r[   r]   r_   rh    r%   r#   r   r   "   s    H<$6$A$A <d <x}    5G5R5R  WZ     +S + + ?s ? ? N# N N 9 9 9)Xd^ ) ) -T - -!8M$A$AB !jN_N_ ! *$ * *8tU{+ 8, Ht H H ),   <4 < <8# 8   r%   r   c                      e Zd ZdZdeeef   ddfdZedd       Z	ede
j                  deeef   ddfd       Zd	edefd
Zdee   fdZdefdZdej(                  deej.                     fdZdeegef   dd fdZddZdee   dd fdZdee ee!f      fdZ"ddZ#dej.                  de$e   fdZ%defdZ&defdZ'y)PluginsRegistryzPlugins registry.pluginsr   Nc                 R    t        t        |j                                     | _        y r   )dictsorteditems_plugins)r"   rw   s     r#   r$   zPluginsRegistry.__init__   s    
 VGMMO45r%   c           	         i }t        j                  d      }|r|j                  d      ng }t        j                  j                  |       t        t        j                  t        j                              }t        t        j                  t        j                              }||z   D ]  }	 | j                  ||         | |      S # t        $ r,}t        j                  d|j                    d| d      |d}~ww xY w)z+Find plugins using setuptools entry points.CERTBOT_PLUGIN_PATH:)groupzThe 'z ' plugin errored while loading: z. You may need to remove or update this plugin. The Certbot log will contain the full error details and this should be reported to the plugin developer.N)r   getenvsplitsyspathextendlistrm   entry_pointsr   SETUPTOOLS_PLUGINS_ENTRY_POINT"OLD_SETUPTOOLS_PLUGINS_ENTRY_POINT_load_entry_pointrZ   r   rS   module)r+   rw   plugin_paths_stringplugin_pathsr   old_entry_pointsr   es           r#   find_allzPluginsRegistry.find_all   s    02 ii(=>9L*005RT%.;;::< = 2 ? ?>>!@ A'*:: 	0K0%%k7;	0 7|  0((K..//OPQs S( () /0	00s   /C	D 'C;;D r   c                    t        |      }|j                  |v r||j                     }|j                  j                  }|j                  j                  }|r|j                  j	                         nd}|r|j                  j	                         nd}t        dj                  |j                  ||            t        |j                  t        j                        r|||j                  <   y t        j                  d|       y )Nunknownz+Duplicate plugin name {0} from {1} and {2}.z)%r does not inherit from Plugin, skipping)r   r   r   distlowerrZ   r0   r;   r   r   rs   rJ   warning)	r+   r   rw   	plugin_epother_epplugin1_distplugin2_distplugin1plugin2s	            r#   r   z!PluginsRegistry._load_entry_point   s     %[1	>>W$y~~.H$0055L#//44L3?l''--/YG3?l''--/YGIPP2 3 3i**J,=,=>&/GINN#NN;YHr%   r   c                      | j                   |   S r   )r|   r(   s     r#   __getitem__zPluginsRegistry.__getitem__   s    }}T""r%   c                 ,    t        | j                        S r   )iterr|   r.   s    r#   __iter__zPluginsRegistry.__iter__   s    DMM""r%   c                 ,    t        | j                        S r   )lenr|   r.   s    r#   __len__zPluginsRegistry.__len__   s    4==!!r%   rF   c                 z    | j                   j                         D cg c]  }|j                  |       c}S c c}w )z'Initialize all plugins in the registry.)r|   valuesrH   )r"   rF   r   s      r#   rH   zPluginsRegistry.init   s6     =='')+9	v& + 	+ +s   8predc           	           t        |       | j                  j                         D ci c]  \  }} ||      r|| c}}      S c c}}w )z"Filter plugins based on predicate.)typer|   r{   )r"   r   r   r   s       r#   filterzPluginsRegistry.filter   sP    tDz"mm113HtY7;I  ? H I 	I Hs   A
c                 &    | j                  d       S )z#Filter plugins based on visibility.c                     | j                    S r   )r6   )r   s    r#   <lambda>z)PluginsRegistry.visible.<locals>.<lambda>   s    1A1A-A r%   r   r.   s    r#   visiblezPluginsRegistry.visible   s    {{ABBr%   r7   c                 ,    | j                  fd      S )z#Filter plugins based on interfaces.c                 "     | j                    S r   )rA   )p_epr7   s    r#   r   z(PluginsRegistry.ifaces.<locals>.<lambda>   s    ](C r%   r   rC   s    `r#   rA   zPluginsRegistry.ifaces   s    {{CDDr%   c                 x    | j                   j                         D cg c]  }|j                          c}S c c}w )z$Prepare all plugins in the registry.)r|   r   rP   )r"   r   s     r#   rP   zPluginsRegistry.prepare   s,    59]]5I5I5KL		!!#LLLs   7c                 &    | j                  d       S )z%Filter plugins based on availability.c                     | j                   S r   )r]   )r   s    r#   r   z+PluginsRegistry.available.<locals>.<lambda>   s
     r%   r   r.   s    r#   r]   zPluginsRegistry.available   s    {{677r%   pluginc                     | j                   j                         D cg c]"  }|j                  r|j                         |u r|$ }}t	        |      dk  sJ |r|d   S yc c}w )aM  Find an initialized plugin.

        This is particularly useful for finding a name for the plugin::

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

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

           r   N)r|   r   rE   rH   r   )r"   r   r   
candidatess       r#   	find_initzPluginsRegistry.find_init   so     261E1E1G OI"..9>>3Cv3M   O
 O:!###a= Os   'Ac                     dj                  | j                  j                  dj                  d | j                  j                         D                    S )Nz{0}({1}),c              3   2   K   | ]  }t        |        y wr   )reprr=   r   s     r#   r?   z+PluginsRegistry.__repr__.<locals>.<genexpr>  s      .?#T
.?   )r0   	__class__rb   rd   r|   r   r.   s    r#   r_   zPluginsRegistry.__repr__  sI      NN##SXX .?'+}}';';'=.? &?@ 	@r%   c                 |    | j                   sydj                  d | j                   j                         D              S )Nz
No pluginsz

c              3   2   K   | ]  }t        |        y wr   )ro   r   s     r#   r?   z*PluginsRegistry.__str__.<locals>.<genexpr>  s     H3t9Hr   )r|   rd   r   r.   s    r#   rh   zPluginsRegistry.__str__  s.    }}{{H1E1E1GHHHr%   )r   rv   )(rb   ri   rj   rk   r	   ro   r   r$   rp   r   rm   rn   r   r   r   r   r   intr   r   rr   r   r   rs   rH   r   rT   r   r   r   r   rA   r   r   rP   r]   r
   r   r_   rh   rt   r%   r#   rv   rv      so   6-=(= > 64 6  , H,>,I,I H#'-=(=#>HCGH H$# #(8 ##(3- #" "+=88 +T*BSBS=T +
I8%5$6$<= IBS I
CEXd^ E8I EMeD%K01 M8

 1 1 h?O6P (@# @
I Ir%   rv   )%rk   loggingr   typingr   r   r   r   r   r   r	   r
   r   r   certbotr   r   r   certbot._internalr   certbot.compatr   certbot.errorsr   version_infoimportlib.metadatametadatarm   	getLoggerrb   rJ   Authenticator	Installerrs   re   r   rv   rt   r%   r#   <module>r      s    4  
           !   '   w3			8	$  --z/C/CZEVEVW  BB  B JtIg tIr%   