ó
Đ#Mc           @   s2  d  Z  d d l Z d d l Z d d l m Z m Z d   Z e   Z d d l m	 Z	 d d l
 m Z d d l m Z d d l m Z d	 e f d
     YZ d e f d     YZ d e f d     YZ d   Z y e j Z Wn e k
 rý d d  Z n Xd   Z d d  Z e Z d   Z d d g Z d S(   sL   
Plugin system for Twisted.

@author: Jp Calderone
@author: Glyph Lefkowitz
i˙˙˙˙N(   t	   Interfacet
   providedByc          C   s9   y d d l  }  |  SWn t k
 r4 d d l } | SXd S(   s5   
    Determine which 'pickle' API module to use.
    i˙˙˙˙N(   t   cPicklet   ImportErrort   pickle(   R   R   (    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   _determinePickleModule   s    (   t   getAdapterFactory(   t   namedAny(   t   log(   t	   getModulet   IPluginc           B   s   e  Z d  Z RS(   sý   
    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.
    (   t   __name__t
   __module__t   __doc__(    (    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyR
   &   s   t   CachedPluginc           B   s8   e  Z d    Z d   Z d   Z d d d  Z e Z RS(   c         C   s;   | |  _  | |  _ | |  _ | |  _ |  j  j j |   d  S(   N(   t   dropint   namet   descriptiont   providedt   pluginst   append(   t   selfR   R   R   R   (    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   __init__2   s
    				c         C   s<   d |  j  |  j j d j g  |  j D] } | j ^ q"  f S(   Ns"   <CachedPlugin %r/%r (provides %r)>s   , (   R   R   t
   moduleNamet   joinR   R   (   R   t   i(    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   __repr__9   s    c         C   s   t  |  j j d |  j  S(   Nt   .(   R   R   R   R   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   load>   s    c         C   s\   xU |  j  D]J } | j |  r) |  j   St | | d   d  k	 r
 | |  j   |  Sq
 W| S(   N(   R   t   isOrExtendsR   R   t   None(   R   t	   interfacet   registryt   defaultt   providedInterface(    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   __conform__A   s    
N(   R   R   R   R   R   R   R#   t   getComponent(    (    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyR   1   s
   				t   CachedDropinc           B   s   e  Z d  Z d   Z RS(   s  
    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 C{NoneType}
    @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         C   s   | |  _  | |  _ g  |  _ d  S(   N(   R   R   R   (   R   R   R   (    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyR   _   s    		(   R   R   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyR%   N   s   c         C   sy   t  |  j |  j  } x] |  j j   D]L \ } } t | d   } | d  k	 r% t | | | j t t	 |    q% q% W| S(   N(
   R%   R   R   t   __dict__t	   iteritemsR
   R   R   t   listR   (   t   providerR   t   kt   vt   plugin(    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   _generateCacheEntryf   s    	)c         C   s%   i  } x |  D] } | | | <q W| S(   N(    (   t   keyst   valuet   dR*   (    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   fromkeyss   s    c         C   s1  i  } t  |  j  } i  } xS | j   D]E } | j j   } | | k rV g  | | <n  | | } | j |  q( Wxš| j   D]Ť\ } } | j d  } y( | j   }	 t	 j
 | j d   }
 Wn i  }
 d }	 n Xt } i  } x | D] } | j j d  d } t | | <| |
 k s1| j j   |	 k rę t } y | j
   } Wn t j   qtXt |  } | |
 | <qę qę Wx0 |
 j   D]" } | | k r|
 | =t } qqW| ry | j t	 j |
   Wqt k
 r} t j d d d | j d	 | j  qt j d d
  qXn  | j |
  q~ W| S(   s;  
    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.
    s   dropin.cachet   ri    R   i˙˙˙˙t   formats@   Unable to write to plugin cache %(path)s: error number %(errno)dt   patht   errnos)   Unexpected error while writing cache fileN(   R	   R   t   iterModulest   filePatht   parentR   R'   t   childt   getModificationTimeR   R   t   opent   FalseR   t   splitt   TrueR   t   errR-   R.   t
   setContentt   dumpst   OSErrort   msgR4   R5   R   t   update(   t   modulet   allCachesCombinedt   modt   bucketst   plugmodt   fppt   buckett   pseudoPackagePatht
   dropinPatht
   lastCachedt   dropinDotCachet
   needsWritet   existingKeyst   pluginModulet	   pluginKeyR)   t   entryt   e(    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   getCache{   s\    



	c         c   s   | d k r d d l j } n  t |  } xc | j   D]U } xL | j D]A } y |  | d  } Wn t j   qG X| d k	 rG | VqG qG Wq7 Wd S(   sc  
    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.
    i˙˙˙˙N(   R   t   twisted.pluginsR   RV   t
   itervaluesR   R?   (   R   t   packaget
   allDropinsR   R,   t   adapted(    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt
   getPluginsĂ   s    c         C   sl   |  j  d  } g  t j D]O } t j j t j j | | d g   s t j j t j j | |   ^ q S(   s¨  
    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   s   __init__.py(   R=   t   sysR4   t   ost   existsR   t   abspath(   R   RY   t   x(    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   pluginPackagePathsá   s    R\   Rb   (   R   R^   R]   t   zope.interfaceR    R   R   R   t   twisted.python.componentsR   t   twisted.python.reflectR   t   twisted.pythonR   t   twisted.python.modulesR	   R
   t   objectR   R%   R-   t   dictR1   t   AttributeErrorR   RV   R\   t
   getPlugInsRb   t   __all__(    (    (    s2   /usr/lib/python2.7/dist-packages/twisted/plugin.pyt   <module>   s,   			
	H	