Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 1 | import threading |
| 2 | import requests, json |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 3 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 5 | from observer.deleters import deleters |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 6 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 7 | import os |
| 8 | import base64 |
Tony Mack | 5c0c455 | 2013-07-03 09:36:51 -0400 | [diff] [blame] | 9 | from fofum import Fofum |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 10 | import json |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 11 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 12 | # decorator that marks dispatachable event methods |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 13 | def event(func): |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 14 | setattr(func, 'event', func.__name__) |
| 15 | return func |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 16 | |
| 17 | class EventHandler: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 18 | # This code is currently not in use. |
| 19 | def __init__(self): |
| 20 | pass |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 21 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 22 | @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 Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 30 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 31 | def dispatch(self, event, *args, **kwds): |
| 32 | if hasattr(self, event): |
| 33 | return getattr(self, event)(*args, **kwds) |
| 34 | |
| 35 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 36 | class EventSender: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 37 | def __init__(self,user=None,clientid=None): |
| 38 | try: |
| 39 | user = Config().feefie_client_user |
| 40 | except: |
| 41 | user = 'pl' |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 42 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 43 | try: |
| 44 | clid = Config().feefie_client_id |
| 45 | except: |
| 46 | clid = self.random_client_id() |
| 47 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 48 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 49 | self.fofum = Fofum(user=user) |
| 50 | self.fofum.make(clid) |
| 51 | |
| 52 | def fire(self,**args): |
| 53 | self.fofum.fire(json.dumps(args)) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 54 | |
| 55 | class EventListener: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 56 | def __init__(self,wake_up=None): |
| 57 | self.handler = EventHandler() |
| 58 | self.wake_up = wake_up |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 59 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 60 | def handle_event(self, payload): |
| 61 | payload_dict = json.loads(payload) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 62 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 63 | try: |
| 64 | deletion = payload_dict['deletion_flag'] |
| 65 | if (deletion): |
| 66 | model = payload_dict['model'] |
| 67 | pk = payload_dict['pk'] |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 68 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 69 | for deleter in deleters[model]: |
| 70 | deleter(pk) |
| 71 | except: |
| 72 | deletion = False |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 73 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 74 | if (not deletion and self.wake_up): |
| 75 | self.wake_up() |
| 76 | |
| 77 | def random_client_id(self): |
Sapan Bhatia | 012ecff | 2013-10-09 10:56:06 -0400 | [diff] [blame] | 78 | try: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 79 | return self.client_id |
Sapan Bhatia | 012ecff | 2013-10-09 10:56:06 -0400 | [diff] [blame] | 80 | except AttributeError: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 81 | self.client_id = base64.urlsafe_b64encode(os.urandom(12)) |
| 82 | return self.client_id |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 83 | |
| 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 Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 87 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 88 | try: |
| 89 | user = Config().feefie_client_user |
| 90 | except: |
| 91 | user = 'pl' |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 92 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 93 | 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() |