blob: da3bd51a5a795288890495320de999815b4a523b [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):
78 if (self.client_id):
79 return self.client_id
80 else:
81 self.client_id = base64.urlsafe_b64encode(os.urandom(12))
82 return self.client_id
Sapan Bhatia9182b322013-06-25 16:22:14 -040083
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040084
85 def run(self):
86 # This is our unique client id, to be used when firing and receiving events
87 # It needs to be generated once and placed in the config file
Sapan Bhatia66f4e612013-07-02 12:12:38 -040088
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040089 try:
90 user = Config().feefie_client_user
91 except:
92 user = 'pl'
Sapan Bhatiab9c59342013-09-03 10:21:13 -040093
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040094 try:
95 clid = Config().feefie_client_id
96 except:
97 clid = self.random_client_id()
98
99 f = Fofum(user=user)
100
101 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
102 listener_thread.start()