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 | |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame^] | 7 | import uuid |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 8 | import os |
| 9 | import base64 |
Tony Mack | 5c0c455 | 2013-07-03 09:36:51 -0400 | [diff] [blame] | 10 | from fofum import Fofum |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 11 | import json |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 12 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 13 | # decorator that marks dispatachable event methods |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 14 | def event(func): |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 15 | setattr(func, 'event', func.__name__) |
| 16 | return func |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 17 | |
| 18 | class EventHandler: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 19 | # This code is currently not in use. |
| 20 | def __init__(self): |
| 21 | pass |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 22 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 23 | @staticmethod |
| 24 | def get_events(): |
| 25 | events = [] |
| 26 | for name in dir(EventHandler): |
| 27 | attribute = getattr(EventHandler, name) |
| 28 | if hasattr(attribute, 'event'): |
| 29 | events.append(getattr(attribute, 'event')) |
| 30 | return events |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 31 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 32 | def dispatch(self, event, *args, **kwds): |
| 33 | if hasattr(self, event): |
| 34 | return getattr(self, event)(*args, **kwds) |
| 35 | |
| 36 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 37 | class EventSender: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 38 | def __init__(self,user=None,clientid=None): |
| 39 | try: |
| 40 | user = Config().feefie_client_user |
| 41 | except: |
| 42 | user = 'pl' |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 43 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 44 | try: |
| 45 | clid = Config().feefie_client_id |
| 46 | except: |
| 47 | clid = self.random_client_id() |
| 48 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 49 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 50 | self.fofum = Fofum(user=user) |
| 51 | self.fofum.make(clid) |
| 52 | |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame^] | 53 | def fire(self,**kwargs): |
| 54 | kwargs["uuid"] = str(uuid.uuid1()) |
| 55 | print "YYY fire", kwargs |
| 56 | self.fofum.fire(json.dumps(kwargs)) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 57 | |
| 58 | class EventListener: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 59 | def __init__(self,wake_up=None): |
| 60 | self.handler = EventHandler() |
| 61 | self.wake_up = wake_up |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 62 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 63 | def handle_event(self, payload): |
| 64 | payload_dict = json.loads(payload) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 65 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 66 | try: |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame^] | 67 | deletion = payload_dict.get('delete_flag', False) |
| 68 | print "XXX", payload_dict, deletion |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 69 | if (deletion): |
| 70 | model = payload_dict['model'] |
| 71 | pk = payload_dict['pk'] |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 72 | |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame^] | 73 | print "XXX", model, pk, deleters |
| 74 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 75 | for deleter in deleters[model]: |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame^] | 76 | print "ZZZ executing deleter" |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 77 | deleter(pk) |
| 78 | except: |
| 79 | deletion = False |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 80 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 81 | if (not deletion and self.wake_up): |
| 82 | self.wake_up() |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame^] | 83 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 84 | def random_client_id(self): |
Sapan Bhatia | 012ecff | 2013-10-09 10:56:06 -0400 | [diff] [blame] | 85 | try: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 86 | return self.client_id |
Sapan Bhatia | 012ecff | 2013-10-09 10:56:06 -0400 | [diff] [blame] | 87 | except AttributeError: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 88 | self.client_id = base64.urlsafe_b64encode(os.urandom(12)) |
| 89 | return self.client_id |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame^] | 90 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 91 | def run(self): |
| 92 | # This is our unique client id, to be used when firing and receiving events |
| 93 | # It needs to be generated once and placed in the config file |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 94 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 95 | try: |
| 96 | user = Config().feefie_client_user |
| 97 | except: |
| 98 | user = 'pl' |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 99 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 100 | try: |
| 101 | clid = Config().feefie_client_id |
| 102 | except: |
| 103 | clid = self.random_client_id() |
| 104 | |
| 105 | f = Fofum(user=user) |
| 106 | |
| 107 | listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event)) |
| 108 | listener_thread.start() |