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 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -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): |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -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: |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 18 | # This code is currently not in use. |
| 19 | def __init__(self): |
Tony Mack | 387a73f | 2013-09-18 07:59:14 -0400 | [diff] [blame] | 20 | pass |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 21 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -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 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -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: |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 37 | def __init__(self,user=None,clientid=None): |
| 38 | try: |
| 39 | clid = Config().feefie_client_id |
| 40 | user = Config().feefie_client_user |
| 41 | except: |
| 42 | clid = 'planetstack_core_team' |
| 43 | user = 'pl' |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 44 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 45 | self.fofum = Fofum(user=user) |
| 46 | self.fofum.make(clid) |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 47 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 48 | def fire(self,**args): |
| 49 | self.fofum.fire(json.dumps(args)) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 50 | |
| 51 | class EventListener: |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 52 | def __init__(self,wake_up=None): |
| 53 | self.handler = EventHandler() |
| 54 | self.wake_up = wake_up |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 55 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 56 | def handle_event(self, payload): |
| 57 | payload_dict = json.loads(payload) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 58 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 59 | try: |
| 60 | deletion = payload_dict['deletion_flag'] |
| 61 | if (deletion): |
| 62 | model = payload_dict['model'] |
| 63 | pk = payload_dict['pk'] |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 64 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 65 | for deleter in deleters[model]: |
| 66 | deleter(pk) |
| 67 | except: |
| 68 | deletion = False |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 69 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 70 | if (not deletion and self.wake_up): |
| 71 | self.wake_up() |
| 72 | |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 73 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 74 | def run(self): |
| 75 | # This is our unique client id, to be used when firing and receiving events |
| 76 | # It needs to be generated once and placed in the config file |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 77 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 78 | try: |
| 79 | clid = Config().feefie_client_id |
| 80 | user = Config().feefie_client_user |
| 81 | except: |
| 82 | clid = 'planetstack_core_team' |
| 83 | user = 'pl' |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 84 | |
Tony Mack | ce79de0 | 2013-09-24 10:12:33 -0400 | [diff] [blame] | 85 | f = Fofum(user=user) |
| 86 | |
| 87 | listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event)) |
| 88 | listener_thread.start() |