
δΎT;  c               @   so   d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l Z d a
 Gd d	   d	  Z d S(
   u   
The Command handler
i   (   u   command(   u   get_namei    (   u   reload(   u   import_moduleNc             B   sn   |  Ee  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z	 d d   Z
 d S(   u   CommandHandleru:    The command handler object - handles triggering commands c             C   sξ   | a  g  |  _ |  _ d d   | D |  _ t j |  |  _ x |  j D] } | |  qH Wt j |  |  _ d d   |  j D |  _ |  j |  j  |  _	 d d   |  j D |  _
 |  j g  k rΣ t d   n  t d t |  j   d  S(   Nc             S   s   g  |  ] } | j   q S(    (   u   __name__(   u   .0u   p(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu
   <listcomp>   s   	 u+   CommandHandler.__init__.<locals>.<listcomp>c             S   s   g  |  ] } | j   q S(    (   u	   main_hook(   u   .0u   cmd(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu
   <listcomp>   s   	 c             S   s   g  |  ] } | j  r |  q S(    (   u   regex(   u   .0u   i(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu
   <listcomp>   s   	 u   No commands found!u   %d commands initialized.(   u   BOTu   initializersu   commandsu   loaded_pluginsu   commandu   plugins_to_initializersu   plugins_to_commandsu   command_namesu   organize_commandsu   command_helpu   commands_with_reu   RuntimeErroru   printu   len(   u   selfu   botu   pluginsu   function(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu   __init__   s    u   CommandHandler.__init__c             C   sσ   | t  j j   k r$ t |  } n t t  j |  } | |  j k rY |  j j |  n  x< |  j D]1 } t | j	  j
 |  rc |  j j |  qc qc W|  j t j |  7_ t j |  } x | D] } | t  qΖ W|  j |  j  |  _ | S(   N(   u   sysu   modulesu   keysu   impu   reloadu   loaded_pluginsu   removeu   commandsu   get_nameu   functionu
   startswithu   commandu   plugins_to_commandsu   plugins_to_initializersu   BOTu   organize_commandsu   command_help(   u   selfu   pluginu   reloaded_pluginu   cmdu   initializersu   i(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu   reload_plugin#   s    u   CommandHandler.reload_pluginc             C   s   | t  j j   k r d  St |  } |  j j | j  |  j t	 j
 |  7_ t	 j |  } x | D] } | t  qf W|  j |  j  |  _ | S(   N(   u   sysu   modulesu   keysu   Noneu   impu   loaded_pluginsu   appendu   __name__u   commandsu   commandu   plugins_to_commandsu   plugins_to_initializersu   BOTu   organize_commandsu   command_help(   u   selfu   nameu   pluginu   initializersu   i(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu   load_plugin4   s    u   CommandHandler.load_pluginc             C   sv   | t  j j   k r d  Sx< |  j D]1 } t | j  j |  r# |  j j |  q# q# Wt  j | =|  j	 j |  d S(   NT(   u   sysu   modulesu   keysu   Noneu   commandsu   get_nameu   functionu
   startswithu   removeu   loaded_pluginsu   True(   u   selfu   nameu   cmd(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu   unload_pluginA   s    
u   CommandHandler.unload_pluginc             C   sΨ   t    } i  } | j d  x6 | D]. } t | j d  r# | j | j j  q# q# Wx% | D] } | j i t    | 6 q\ WxQ | D]I } t | j d  rΉ | | j j j | j  q | d j | j  q W~ | S(   Nu   unknownu	   _category(   u   setu   addu   hasattru   functionu	   _categoryu   updateu	   main_hook(   u   selfu   commandsu
   categoriesu   command_helpu   commandu   category(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu   organize_commandsM   s    	u    CommandHandler.organize_commandsc             C   s+   x$ |  j  D] } | j |  r
 | Sq
 Wd S(   u.    Try to trigger a command with hook <trigger> N(   u   commandsu   is_triggered_byu   None(   u   selfu   triggeru   command_(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu   trigger_   s    u   CommandHandler.triggerc             C   s1   x* |  j  D] } | j | d d r
 | Sq
 Wd S(   u3    Try to trigger a command with shorthook <trigger> u   shortTN(   u   commandsu   is_triggered_byu   Trueu   None(   u   selfu   triggeru   command_(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu   trigger_shortg   s    u   CommandHandler.trigger_shortN(   u   __name__u
   __module__u   __qualname__u   __doc__u   __init__u   reload_pluginu   load_pluginu   unload_pluginu   organize_commandsu   triggeru   trigger_short(   u
   __locals__(    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu   CommandHandler   s   u   CommandHandler(   u   __doc__u    u   commandu   get_nameu   impu   reloadu	   importlibu   import_moduleu   sysu   Noneu   BOTu   CommandHandler(    (    (    u.   /home/sam/Code/Infobot/utils/commandhandler.pyu   <module>   s   