blob: a885fb5d52dde1889509a63c2b1d2c52f26e00db [file] [log] [blame]
Tony Mack79a49c82013-06-15 23:51:57 -04001import threading
2import requests, json
Sapan Bhatia66f4e612013-07-02 12:12:38 -04003
Sapan Bhatia66f4e612013-07-02 12:12:38 -04004from planetstack.config import Config
Sapan Bhatiadbaf1932013-09-03 11:28:52 -04005from observer.deleters import deleters
Tony Mack79a49c82013-06-15 23:51:57 -04006
Sapan Bhatia9182b322013-06-25 16:22:14 -04007import os
8import base64
Tony Mack5c0c4552013-07-03 09:36:51 -04009from fofum import Fofum
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040010import json
Sapan Bhatia9182b322013-06-25 16:22:14 -040011
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040012# decorator that marks dispatachable event methods
Tony Mack79a49c82013-06-15 23:51:57 -040013def event(func):
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040014 setattr(func, 'event', func.__name__)
15 return func
Tony Mack79a49c82013-06-15 23:51:57 -040016
17class EventHandler:
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040018 # This code is currently not in use.
19 def __init__(self):
20 pass
Tony Mack79a49c82013-06-15 23:51:57 -040021
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040022 @staticmethod
23 def get_events():
24 events = []
25 for name in dir(EventHandler):
26 attribute = getattr(EventHandler, name)
27 if hasattr(attribute, 'event'):
28 events.append(getattr(attribute, 'event'))
29 return events
Tony Mack79a49c82013-06-15 23:51:57 -040030
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040031 def dispatch(self, event, *args, **kwds):
32 if hasattr(self, event):
33 return getattr(self, event)(*args, **kwds)
34
35
Sapan Bhatia66f4e612013-07-02 12:12:38 -040036class EventSender:
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040037 def __init__(self,user=None,clientid=None):
38 try:
39 user = Config().feefie_client_user
40 except:
41 user = 'pl'
Sapan Bhatia66f4e612013-07-02 12:12:38 -040042
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040043 try:
44 clid = Config().feefie_client_id
45 except:
46 clid = self.random_client_id()
47
Sapan Bhatia66f4e612013-07-02 12:12:38 -040048
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040049 self.fofum = Fofum(user=user)
50 self.fofum.make(clid)
51
52 def fire(self,**args):
53 self.fofum.fire(json.dumps(args))
Tony Mack79a49c82013-06-15 23:51:57 -040054
55class EventListener:
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040056 def __init__(self,wake_up=None):
57 self.handler = EventHandler()
58 self.wake_up = wake_up
Tony Mack79a49c82013-06-15 23:51:57 -040059
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040060 def handle_event(self, payload):
61 payload_dict = json.loads(payload)
Sapan Bhatia43c3a772013-07-03 11:19:07 -040062
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040063 try:
64 deletion = payload_dict['deletion_flag']
65 if (deletion):
66 model = payload_dict['model']
67 pk = payload_dict['pk']
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040068
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040069 for deleter in deleters[model]:
70 deleter(pk)
71 except:
72 deletion = False
Sapan Bhatia66f4e612013-07-02 12:12:38 -040073
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040074 if (not deletion and self.wake_up):
75 self.wake_up()
76
77 def random_client_id(self):
Sapan Bhatia012ecff2013-10-09 10:56:06 -040078 try:
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040079 return self.client_id
Sapan Bhatia012ecff2013-10-09 10:56:06 -040080 except AttributeError:
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040081 self.client_id = base64.urlsafe_b64encode(os.urandom(12))
82 return self.client_id
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040083
84 def run(self):
85 # This is our unique client id, to be used when firing and receiving events
86 # It needs to be generated once and placed in the config file
Sapan Bhatia66f4e612013-07-02 12:12:38 -040087
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040088 try:
89 user = Config().feefie_client_user
90 except:
91 user = 'pl'
Sapan Bhatiab9c59342013-09-03 10:21:13 -040092
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040093 try:
94 clid = Config().feefie_client_id
95 except:
96 clid = self.random_client_id()
97
98 f = Fofum(user=user)
99
100 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
101 listener_thread.start()