mς
tX&Gc           @   sΘ   d  k  Z  d  k Z d  k Z d  k Z d  k Z d  k Z d e i   Z d d d d d g Z d  d  d  d  Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z e d j o e   n d  S(   Ns   %s/drinksfile.pckt
   drinkslistt   adddrinkt   deldrinkt   popdrinkt   drinkc         C   sα   t  i d  } t  i d |  } t i | | |   } |  d j o t
   } n{ |  d j o t | |  } n[ |  d j o t | |  } n; |  d j o t |  } n |  d j o t |  } n t i | | |  d  S(   Nt   commonR    R   R   R   R   (   t   impt   find_modulet   module_locationt   load_modulet   moduleR   t   NormalizeMessaget   msgt   irct   keywordt	   GetDrinkst   resultt   AddDrinkt   DelDrinkt   PopDrinkt
   ServeDrinkt   DeliverMessaget   channel(   R   R   R   R   R   R
   R   (    (    t@   /home/avleen/devel/python/googlebot2/modules/googlebot_drinks.pyt   Gather   s    c          C   s   y t  t d  } Wn> t j
 o2 d t GHd g }  t |   t  t d  } n Xy t i |  }  Wn/ t j
 o# d t GHd g }  t |   n X|  S(   sj   List drinks in the database
  
  Args:
    None
    
  Returns:
    drinkslist: ('drink', 'drink', ...)
  t   rs   Drinks file %s does not exists   natural spring waters   Drinks file %s is emptyN(	   t   opent
   DRINKSFILEt   drinkspicklet   IOErrorR    t
   SaveDrinkst   picklet   loadt   EOFError(   R    R   (    (    R   R      s     		
		c         C   s-   t  t d  } t i |  |  | i   d S(   s   Saves the drinks to the drinks file.

  Args:
    drinkslist: ['drink', 'drink'...]

  Returns:
    str: Success or failure message
  t   ws   Drinks list updated!N(   R   R   R   R   t   dumpR    t   close(   R    R   (    (    R   R   8   s
     
c         C   sO   t  |   d j o d } | Sn t   } | i |   t |  d |  } | S(   sh   Add drinks to the database
  
  Args:
    msg: string

  Returns:
    str: Success or failure message
  i@   s.   Hmmm that drink name is rather long, isn't it?s   Added %s to the databaseN(   t   lenR   R   R   R    t   appendR   (   R   R    R   (    (    R   R   H   s     	

c          C   sG   t    }  y |  i   t |   d } Wn t j
 o d } n X| S(   st   Pop the database, to remove the latest entry
  
  Args:
    None
    
  Returns:
    str: Success or failure messages   Popped the databases   Unable to pop empty databaseN(   R   R    t   popR   R   t
   IndexError(   R    R   (    (    R   R   [   s     	


c         C   sR   t    } y | i |   d |  } Wn t j
 o d |  } n Xt |  | S(   s   Remove drinks from the database

  Args:
    channel: string
    msg: string

  Returns:
    str: Success or failure message
  s   Removed %s from the databases   %s is not in the databaseN(   R   R    t   removeR   R   t
   ValueErrorR   (   R   R    R   (    (    R   R   n   s    	 	
c         C   s=   t    } t i |  } |  p
 d }  n d |  | f } | S(   sX   Serve the drinks!

  Args:
    msg: string

  Returns:
    str: Message serving drink
  t   yous   pours %s one %sN(   R   R    t   randomt   choiceR   t   nickR   (   R.   R    R   R   (    (    R   R      s     	
c          C   s   y t  i d }  Wn  t j
 o t  i d  n Xy t  i d } Wn t j
 o d  } n X|  t j o t |  |  n d  S(   Ni   i   (	   t   syst   argvR   R(   t   exitt   argumentt   Nonet   module_triggersR   (   R   R2   (    (    R   t   main   s    t   __main__(   R   R   R   R,   R/   t   ost   getcwdR   R4   R3   R   R   R   R   R   R   R   R5   t   __name__(   R   R   R   R   R   R7   R,   R   R/   R   R   R   R5   R   R   R4   (    (    R   t   ?   s"   													
