ó
Qº’Nc           @   s„   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z e j ƒ  Z d  d l Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d S(   iÿÿÿÿNt   Pluginc           B   sS   e  Z d  Z d „  Z d „  Z e e e ƒ Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   s‡   Base class for plugins.
    
    These plugins only do one thing: identify cruft. See the 'get_cruft'
    method for details.
    
    c         C   s   t  |  d ƒ r |  j Sg  Sd  S(   Nt
   _condition(   t   hasattrR   (   t   self(    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   get_condition#   s    c         C   s   | |  _  d  S(   N(   R   (   R   t	   condition(    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   set_condition)   s    c         C   s   | |  _  d S(   s¥  Set the Application instance this plugin belongs to.
        
        This is used by the plugin manager when creating the plugin
        instance. In a perfect world, this would be done via the
        __init__ method, but since I took a wrong left turn, I ended
        up in an imperfect world, and therefore giving the Application
        instance to __init__ would mandate that all sub-classes would
        have to deal with that explicitly. That is a lot of unnecessary
        make-work, which we should avoid. Therefore, we do it via this
        method.
        
        The class may access the Application instance via the
        'app' attribute.
        
        N(   t   app(   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   set_application.   s    c         C   s/   x |  j  ƒ  D] } | j ƒ  q W|  j ƒ  d S(   sF   Find cruft and clean it up.

        This is a helper method.
        N(   t	   get_cruftt   cleanupt   post_cleanup(   R   t   cruft(    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   do_cleanup_cruftA   s    c         C   s   t  j |  j ƒ ‚ d S(   s„  Find some cruft in the system.
        
        This method MUST return an iterator (see 'yield' statement).
        This interface design allows cruft to be collected piecemeal,
        which makes it easier to show progress in the user interface.
        
        The base class default implementation of this raises an
        exception. Subclasses MUST override this method.

        N(   t   computerjanitort   UnimplementedMethodR	   (   R   (    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyR	   K   s    c         C   s   d S(   sú   Does plugin wide cleanup after the individual cleanup
           was performed.
           
           This is useful for stuff that needs to be proccessed
           in batches (e.g. for performance reasons) like package
           removal.
        N(    (   R   (    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyR   Y   s    (   t   __name__t
   __module__t   __doc__R   R   t   propertyR   R   R   R	   R   (    (    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyR       s   				
	t   PluginManagerc           B   sA   e  Z d  Z d „  Z d „  Z d „  Z d „  Z g  d d „ Z RS(   s›   Class to find and load plugins.
    
    Plugins are stored in files named '*_plugin.py' in the list of
    directories given to the initializer.
    
    c         C   s   | |  _  | |  _ d  |  _ d  S(   N(   t   _appt   _plugin_dirst   Nonet   _plugins(   R   R   t   plugin_dirs(    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   __init__m   s    		c         C   s³   g  } x¦ |  j  D]› } t j j | ƒ s. q n  g  t j | ƒ D] } | j d ƒ r> | ^ q> } t j d | d j | ƒ f ƒ | g  | D] } t j j | | ƒ ^ q‰ 7} q W| S(   s4   Return all filenames in which plugins may be stored.s
   _plugin.pys   Plugin modules in %s: %st    (	   R   t   ost   patht   existst   listdirt   endswitht   loggingt   debugt   join(   R   t   namest   dirnamet   xt	   basenames(    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   get_plugin_filesr   s    	0c         C   s˜   g  } xK t  j | ƒ D]: \ } } t  j | ƒ r t | t ƒ r | j | ƒ q q Wt j d | d j d „  | Dƒ ƒ f ƒ g  | D] } | ƒ  ^ q… S(   s-   Find and instantiate all plugins in a module.s   Plugins in %s: %sR   c         s   s   |  ] } t  | ƒ Vq d  S(   N(   t   str(   t   .0R&   (    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pys	   <genexpr>Š   s    (	   t   inspectt
   getmemberst   isclasst
   issubclassR    t   appendR!   R"   R#   (   R   t   modulet   pluginst   dummyt   membert   plugin(    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   _find_pluginsƒ   s    	!c         C   s¨   t  j d | ƒ t j j t j j | ƒ ƒ \ } } t | d ƒ } y( t j | | | d d t j	 f ƒ } Wn+ t
 k
 r™ } t  j d | | f ƒ d SX| j ƒ  | S(   s   Load a module from a filename.s   Loading module %st   rs   .pys   Failed to load plugin '%s' (%s)N(   R!   R"   R   R   t   splitextt   basenamet   filet   impt   load_modulet	   PY_SOURCEt	   Exceptiont   warningR   t   close(   R   t   filenamet   module_nameR2   t   fR0   t   e(    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   _load_module   s    $	
c   	      C   s  |  j  d k r´ g  |  _  |  j ƒ  } x t t | ƒ ƒ D]v } | r` | | | | t | ƒ ƒ n  |  j | | ƒ } x7 |  j | ƒ D]& } | j |  j ƒ |  j  j	 | ƒ qƒ Wq7 Wn  g  |  j  D]6 } | j
 | k sî | | j
 k sî | d k r¾ | ^ q¾ } t j d | | f ƒ | S(   s´  Return all plugins that have been found.
        
        If callback is specified, it is called after each plugin has
        been found, with the following arguments: filename, index of
        filename in list of files to be examined (starting with 0), and
        total number of files to be examined. The purpose of this is to
        allow the callback to inform the user in case things take a long
        time.
        
        t   *s#   plugins for condition '%s' are '%s'N(   R   R   R(   t   ranget   lenRD   R5   R   R   R/   R   R!   R"   (	   R   R   t   callbackt	   filenamest   iR0   R4   t   pR1   (    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   get_pluginsœ   s"    		N(	   R   R   R   R   R(   R5   RD   R   RL   (    (    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyR   d   s   			
	(
   R:   R+   R!   R   R   t   setup_gettextt   _t   objectR    R   (    (    (    s:   /usr/lib/python2.7/dist-packages/computerjanitor/plugin.pyt   <module>   s   J