
    Ϫf!                        d Z ddlZddlZddlZddlZddlmZmZmZm	Z	 ddl
mZmZ ddlmZ ddlmZ ddlmZ ddlmZ  G d	 d
e      Z G d d      Z G d d      Zd Z	 ej4                  Zd Zdej:                  fdZ e	de      Z	 ddee   deej:                     dee   fdZ e Z!d Z"ddgZ#y# e$ r ddZY Qw xY w)zL
Plugin system for Twisted.

@author: Jp Calderone
@author: Glyph Lefkowitz
    N)IterableOptionalTypeTypeVar)	Interface
providedBy)log)getAdapterFactory)	getModule)namedAnyc                       e Zd ZdZy)IPluginz
    Interface that must be implemented by all plugins.

    Only objects which implement this interface will be considered for return
    by C{getPlugins}.  To be useful, plugins should also implement some other
    application-specific interface.
    N)__name__
__module____qualname____doc__     0/usr/lib/python3/dist-packages/twisted/plugin.pyr   r      s    r   r   c                   0    e Zd Zd ZdefdZd ZddZeZy)CachedPluginc                     || _         || _        || _        || _        | j                   j                  j                  |        y N)dropinnamedescriptionprovidedpluginsappend)selfr   r   r   r   s        r   __init__zCachedPlugin.__init__'   s7    	& ""4(r   returnc           
          dj                  | j                  | j                  j                  dj	                  | j
                  D cg c]  }|j                   c}            S c c}w )Nz(<CachedPlugin {!r}/{!r} (provides {!r})>z, )formatr   r   
moduleNamejoinr   r   )r    is     r   __repr__zCachedPlugin.__repr__.   sM    9@@IIKK""II4==9aqzz9:
 	
 :s   A#c                 `    t        | j                  j                  dz   | j                  z         S )N.)r   r   r%   r   )r    s    r   loadzCachedPlugin.load5   s%    ..4tyy@AAr   Nc                     | j                   D ]K  }|j                  |      r| j                         c S t        ||d       4 || j                         |      c S  |S r   )r   isOrExtendsr+   r
   )r    	interfaceregistrydefaultprovidedInterfaces        r   __conform__zCachedPlugin.__conform__8   sZ    !% 	7 ,,Y7yy{" !2ItDP g66		7
 r   )NN)	r   r   r   r!   strr(   r+   r2   getComponentr   r   r   r   r   &   s%    )
# 
B Lr   r   c                       e Zd ZdZd Zy)CachedDropina  
    A collection of L{CachedPlugin} instances from a particular module in a
    plugin package.

    @type moduleName: C{str}
    @ivar moduleName: The fully qualified name of the plugin module this
        represents.

    @type description: C{str} or L{None}
    @ivar description: A brief explanation of this collection of plugins
        (probably the plugin module's docstring).

    @type plugins: C{list}
    @ivar plugins: The L{CachedPlugin} instances which were loaded from this
        dropin.
    c                 .    || _         || _        g | _        y r   )r%   r   r   )r    r%   r   s      r   r!   zCachedDropin.__init__V   s    $&r   N)r   r   r   r   r!   r   r   r   r6   r6   D   s    "r   r6   c                     t        | j                  | j                        }| j                  j	                         D ]>  \  }}t        |d       }|t        |||j                  t        t        |                   @ |S r   )	r6   r   r   __dict__itemsr   r   listr   )providerr   kvplugins        r   _generateCacheEntryr@   \   ss    (++X-=-=>F!!'') I1D!AIItJv4F/GH	I
 Mr   c                 "    i }| D ]  }|||<   	 |S r   r   )keysvaluedr=   s       r   fromkeysrE   j   s$     	AAaD	r   c                    i }t        | j                        }i }|j                         D ];  }|j                  j	                         }||vrg ||<   ||   }|j                  |       = |j                         D ]*  \  }}|j                  d      }	 |j                         }	|j                  d      5 }
t        j                  |
      }ddd       d}i }|D ]j  }|j                  j                  d      d   }d||<   |vs|j                  j                         |	k\  sHd}	 |j                         }t        |      }|||<   l t%        j'                               D ]  }||vs||= d} |r%	 |j)                  t        j*                  |             |j5                  |       - |S # 1 sw Y   xY w# t        $ r i }d}	Y w xY w# t        $ r t!        j"                          Y w xY w# t,        $ r6}t!        j.                  d	|j0                  |j2                  
       Y d}~d}~wt        $ r t!        j"                  dd       Y w xY w)a;  
    Compute all the possible loadable plugins, while loading as few as
    possible and hitting the filesystem as little as possible.

    @param module: a Python module object.  This represents a package to search
    for plugins.

    @return: a dictionary mapping module names to L{CachedDropin} instances.
    zdropin.cacherNr   Fr*   Tz@Unable to write to plugin cache %(path)s: error number %(errno)d)r$   patherrnoz)Unexpected error while writing cache file)r   r   iterModulesfilePathparentr   r:   childgetModificationTimeopenpickler+   BaseExceptionr   splitr@   r	   errr;   rB   
setContentdumpsOSErrormsgrI   rJ   update)moduleallCachesCombinedmodbucketsplugmodfppbucketpseudoPackagePath
dropinPath
lastCachedfdropinDotCache
needsWriteexistingKeyspluginModule	pluginKeyr<   entryes                      r   getCacherl   q   s`    
FOO
$CG ??$ %%'gGCLg &-]]_ .1!6&,,^<
	#779J% 0!'Q0 
" 	6L$))//4R8I&*L#/%%99;zI!
6+002H
 09E05N9-	6  n1134 	"I,"9-!
	" K%%fll>&BC 	  0].1^ W0 0 	NJ	 % GGI  + $''  ! KIJKsZ   !F42F(F4!G.$G+(F1	-F44GGG('G(+	I
4,H%%"I
	I
r"   c                      dd l m}  | S )Nr   )twisted.pluginsr   )packages    r   _pluginsPackagerp      s
    %Nr   _TInterface)boundr.   ro   c              #      K   |
t               }t        |      }|j                         D ]'  \  }}|j                  D ]  }	  | |d      }||  ) y# t        $ r t        j                          Y 8w xY ww)ac  
    Retrieve all plugins implementing the given interface beneath the given module.

    @param interface: An interface class.  Only plugins which implement this
    interface will be returned.

    @param package: A package beneath which plugins are installed.  For
    most uses, the default value is correct.

    @return: An iterator of plugins.
    N)rp   rl   r:   r   rR   r	   rT   )r.   ro   
allDropinskeyr   r?   adapteds          r   
getPluginsrw      s      !#'"J!'') "Vnn 	"F"#FD1 &!M	"" ! 	s/   =A8 	A	A8	A8A52A84A55A8c           
      b   | j                  d      }t        j                  D cg c]  }t        j                  j	                  t        j                  j
                  |g|dgz          s=t        j                  j                  t        j                  j
                  |g|        c}S c c}w )a  
    Return a list of additional directories which should be searched for
    modules to be included as part of the named plugin package.

    @type name: C{str}
    @param name: The fully-qualified Python name of a plugin package, eg
        C{'twisted.plugins'}.

    @rtype: C{list} of C{str}
    @return: The absolute paths to other directories which may contain plugin
        modules for the named plugin package.
    r*   z__init__.py)rS   sysrI   osexistsr&   abspath)r   ro   xs      r   pluginPackagePathsr~      s     jjoG ww~~bggll1Iw-/HIJ 	Q112  s   BB,rw   r~   r   )$r   rz   rQ   ry   typestypingr   r   r   r   zope.interfacer   r   twisted.pythonr	   twisted.python.componentsr
   twisted.python.modulesr   twisted.python.reflectr   r   r   r6   r@   dictrE   AttributeErrorrl   
ModuleTyperp   rq   rw   
getPlugInsr~   __all__r   r   r   <module>r      s    
  
  4 4 0  7 , +i  < 0}}HIX))  m95 IM"K "+3E4D4D+E"k": 
< -
.y  s   "B4 4	C ?C 