
ä¾T²  ã               @   sq   d  Z  d d l Z d d l Z d d l Z d d l Z e j Z Gd d „  d e ƒ Z d d „  Z	 d d „  Z
 d S)	zp
command class definition - do not confuse this with
the individual plugins in plugins/
by Sam van Kampen, 2013
é    Nc               @   s   e  Z d  Z d Z d d d d „ Z d d „  Z d d „  Z d	 d
 „  Z d d „  Z d d „  Z	 d d „  Z
 d d d „ Z d S)ÚCommandzECommand(Object function, List<str> hooks=[], List<str> shorthooks=[])Nc             C   s†   | |  _  d  |  _ | s g  } | s* g  } t | ƒ |  _ t | ƒ |  _ | r^ | d |  _ n	 d  |  _ t | d ƒ r‚ | j |  _ d  S)Nr   Ú_regex)ÚfunctionÚregexÚsetÚhooksÚshort_hooksZ	main_hookÚhasattrr   )Úselfr   r   Z
shorthooks© r   ú'/home/sam/code/Infobot/utils/command.pyÚ__init__   s    			zCommand.__init__c             C   s=   |  j  d  k r# d |  j |  j  f Sd |  j |  j |  j f S)NzCommand(%r, regex=%r)z$Command(%r, hooks=%r, shorthooks=%r))r   r   r   r   )r
   r   r   r   Ú__repr__    s    zCommand.__repr__c             G   s   |  j  | Œ  S)z+ Call the function embedded in the command )r   )r
   Úargsr   r   r   Ú__call__%   s    zCommand.__call__c             C   s   |  j  S)z-Get the function associated with this command)r   )r
   r   r   r   Úget_function)   s    zCommand.get_functionc             C   s   | |  _  d S)z Set the hooks. N)r   )r
   r   r   r   r   Ú	set_hooks-   s    zCommand.set_hooksc             C   s   | |  _  d S)z Set the shorthook(s) N)r   )r
   r   r   r   r   Úset_shorthooks1   s    zCommand.set_shorthooksc             C   s   t  j |  j | d t  j ƒS)NÚflags)ÚreÚmatchr   ÚI)r
   Úmsgr   r   r   Úis_re_triggered_by5   s    zCommand.is_re_triggered_byFc             C   sV   |  j  r |  j |  j Br d S| r; | |  j k r7 d Sd S| |  j k rN d Sd Sd S)z4Check whether this command is triggered by <trigger>FTN)r   r   r   )r
   ZtriggerZshortr   r   r   Úis_triggered_by8   s
    zCommand.is_triggered_by)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   r   r   r      s   r   c             C   sø   g  } d } t  |  ƒ t  t j t ƒ k r4 |  g }  y¡ xš |  D]’ } x‰ d d „  | j j ƒ  Dƒ D]k } t | d ƒ rŽ d } t | | j ƒ } t | d ƒ r³ | r³ | j	 | j
 ƒ | ra | j | ƒ d } qa Wq> WWn t k
 ró t j ƒ  Yn X| S)z.Turn a list of plugins into a list of commandsFc             S   s(   g  |  ] } t  | ƒ t k r | ‘ q Sr   )ÚtypeÚftype)Ú.0Úfuncr   r   r   ú
<listcomp>J   s   	 z'plugins_to_commands.<locals>.<listcomp>ÚhookTÚ	shorthook)r   ÚsysÚmodulesr   Ú__dict__Úvaluesr	   r   r$   r   r%   ÚappendÚBaseExceptionÚ	tracebackÚ	print_exc)ÚpluginsZcommandsZhas_hookÚpluginr   Znew_commandr   r   r   Úplugins_to_commandsB   s$    	#r0   c             C   s¥   g  } t  |  ƒ t  t j t ƒ k r. |  g }  yT xM |  D]E } x< t t | j j ƒ  ƒ D]" } t | d ƒ rW | j	 | ƒ qW Wq8 WWn t
 k
 r  t j ƒ  Yn X| S)z2Turn a list of plugins into a list of initializersZ_is_plugin_initializer)r   r&   r'   r   ÚfilterÚcallabler(   r)   r	   r*   r+   r,   r-   )r.   Zinitializersr/   r   r   r   r   Úplugins_to_initializersY   s    	r3   )r   r&   r,   r   ÚtypesÚFunctionTyper    Úobjectr   r0   r3   r   r   r   r   Ú<module>   s   	4