Package emma :: Package interface
[hide private]
[frames] | no frames]

Source Code for Package emma.interface

 1  """ 
 2  interfaces are where the comunication to the internet happends 
 3   
 4  Interfaces communicate with L{modules<module>} through L{events}. modules 
 5  will do the functionallity of the bot, using interfaces for communicate with 
 6  mailing lists, irc, ... 
 7   
 8  Interface inheritate from L{complement} from where you can 
 9  L{log<emma.complement.Complement.log>}, L{use locks<emma.complement.use_lock>}, 
10  ... Everything in an interface happens in parallel on several threads, so for 
11  class variables L{locks<emma.complement.use_lock>} should be use. 
12   
13  @copyright: (c) 2011 hackmeeting U{http://sindominio.net/hackmeeting} 
14  @author: Ruben Pollan 
15  @organization: hackmeeting U{http://sindominio.net/hackmeeting} 
16  @contact: meskio@sindominio.net 
17  @license: 
18    This program is free software; you can redistribute it and/or 
19    modify it under the terms of the Do What The Fuck You Want To 
20    Public License, Version 2, as published by Sam Hocevar. See 
21    U{http://sam.zoy.org/projects/COPYING.WTFPL} for more details. 
22  """ 
23   
24  import re 
25   
26  from emma.complement import Complement 
27  from emma.events import Event, subscribe 
28   
29   
30 -class Interface(Complement):
31 - def __init__(self, *args):
32 Complement.__init__(self, *args) 33 name = self.__module__.split(".")[-1] 34 event = Event(event="db", interface=name, identifier=self.identifier) 35 subscribe(event, self.db_handler)
36
37 - def db_handler(self, event, data):
38 """ 39 Default DB handler 40 41 Get a database search for the collection of the interface and return 42 it's results. 43 44 @type event: l{event} 45 @param event: event to be triggered, it must have all the elements 46 @type data: dict 47 @param data: a db search, like {"type": "foo", "body": True} 48 @returns: results of the db search 49 """ 50 search = {} 51 for k, v in data.items(): 52 if '/' == v[0] == v[-1]: 53 search[k] = re.compile(v[1:-2]) 54 else: 55 search[k] = v 56 57 try: 58 res = self.db.find(search) 59 except Exception: 60 self.log(_("db request error.")) 61 res = [] 62 return [i for i in res]
63