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 |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 5 | from observer.deleter import Deleter |
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 |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 9 | import imp |
| 10 | import inspect |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 11 | import base64 |
Tony Mack | 5c0c455 | 2013-07-03 09:36:51 -0400 | [diff] [blame] | 12 | from fofum import Fofum |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 13 | import json |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 14 | import traceback |
Sapan Bhatia | 9182b32 | 2013-06-25 16:22:14 -0400 | [diff] [blame] | 15 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 16 | # decorator that marks dispatachable event methods |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 17 | def event(func): |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 18 | setattr(func, 'event', func.__name__) |
| 19 | return func |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 20 | |
| 21 | class EventHandler: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 22 | # This code is currently not in use. |
| 23 | def __init__(self): |
| 24 | pass |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 25 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 26 | @staticmethod |
| 27 | def get_events(): |
| 28 | events = [] |
| 29 | for name in dir(EventHandler): |
| 30 | attribute = getattr(EventHandler, name) |
| 31 | if hasattr(attribute, 'event'): |
| 32 | events.append(getattr(attribute, 'event')) |
| 33 | return events |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 34 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 35 | def dispatch(self, event, *args, **kwds): |
| 36 | if hasattr(self, event): |
| 37 | return getattr(self, event)(*args, **kwds) |
| 38 | |
| 39 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 40 | class EventSender: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 41 | def __init__(self,user=None,clientid=None): |
| 42 | try: |
| 43 | user = Config().feefie_client_user |
| 44 | except: |
| 45 | user = 'pl' |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 46 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 47 | try: |
| 48 | clid = Config().feefie_client_id |
| 49 | except: |
| 50 | clid = self.random_client_id() |
| 51 | |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 52 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 53 | self.fofum = Fofum(user=user) |
| 54 | self.fofum.make(clid) |
| 55 | |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 56 | def fire(self,**kwargs): |
| 57 | kwargs["uuid"] = str(uuid.uuid1()) |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 58 | self.fofum.fire(json.dumps(kwargs)) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 59 | |
| 60 | class EventListener: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 61 | def __init__(self,wake_up=None): |
| 62 | self.handler = EventHandler() |
| 63 | self.wake_up = wake_up |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 64 | self.deleters = {} |
| 65 | self.load_deleter_modules() |
| 66 | |
| 67 | def load_deleter_modules(self, deleter_dir=None): |
| 68 | if deleter_dir is None: |
| 69 | if hasattr(Config(), "observer_deleters_dir"): |
| 70 | deleter_dir = Config().observer_deleters_dir |
| 71 | else: |
| 72 | deleter_dir = "/opt/planetstack/observer/deleters" |
| 73 | |
| 74 | for fn in os.listdir(deleter_dir): |
| 75 | pathname = os.path.join(deleter_dir,fn) |
| 76 | if os.path.isfile(pathname) and fn.endswith(".py") and (fn!="__init__.py"): |
| 77 | module = imp.load_source(fn[:-3],pathname) |
| 78 | for classname in dir(module): |
| 79 | c = getattr(module, classname, None) |
| 80 | |
| 81 | # make sure 'c' is a descendent of Deleter and has a |
| 82 | # provides field (this eliminates the abstract base classes |
| 83 | # since they don't have a provides) |
| 84 | |
| 85 | if inspect.isclass(c) and issubclass(c, Deleter) and hasattr(c,"model") and c.model!=None: |
| 86 | modelName = c.model |
| 87 | if not modelName in self.deleters: |
| 88 | self.deleters[modelName] = [] |
| 89 | if not (c in self.deleters[modelName]): |
| 90 | self.deleters[modelName].append(c) |
| 91 | print 'loaded deleters: %s' % ",".join(self.deleters.keys()) |
| 92 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 93 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 94 | def handle_event(self, payload): |
| 95 | payload_dict = json.loads(payload) |
Sapan Bhatia | 43c3a77 | 2013-07-03 11:19:07 -0400 | [diff] [blame] | 96 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 97 | try: |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 98 | deletion = payload_dict.get('delete_flag', False) |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 99 | if (deletion): |
| 100 | model = payload_dict['model'] |
| 101 | pk = payload_dict['pk'] |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 102 | model_dict = payload_dict['model_dict'] |
Sapan Bhatia | dbaf193 | 2013-09-03 11:28:52 -0400 | [diff] [blame] | 103 | |
Scott Baker | 6ecd426 | 2014-01-21 23:15:21 -0800 | [diff] [blame] | 104 | for deleter in self.deleters[model]: |
| 105 | try: |
| 106 | deleter()(pk, model_dict) |
| 107 | except: |
| 108 | # something is silently eating these |
| 109 | # exceptions... |
| 110 | traceback.print_exc() |
| 111 | raise |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 112 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 113 | except: |
| 114 | deletion = False |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 115 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 116 | if (not deletion and self.wake_up): |
| 117 | self.wake_up() |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 118 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 119 | def random_client_id(self): |
Sapan Bhatia | 012ecff | 2013-10-09 10:56:06 -0400 | [diff] [blame] | 120 | try: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 121 | return self.client_id |
Sapan Bhatia | 012ecff | 2013-10-09 10:56:06 -0400 | [diff] [blame] | 122 | except AttributeError: |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 123 | self.client_id = base64.urlsafe_b64encode(os.urandom(12)) |
| 124 | return self.client_id |
Scott Baker | c1c45f8 | 2014-01-21 16:23:51 -0800 | [diff] [blame] | 125 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 126 | def run(self): |
| 127 | # This is our unique client id, to be used when firing and receiving events |
| 128 | # It needs to be generated once and placed in the config file |
Sapan Bhatia | 66f4e61 | 2013-07-02 12:12:38 -0400 | [diff] [blame] | 129 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 130 | try: |
| 131 | user = Config().feefie_client_user |
| 132 | except: |
| 133 | user = 'pl' |
Sapan Bhatia | b9c5934 | 2013-09-03 10:21:13 -0400 | [diff] [blame] | 134 | |
Sapan Bhatia | 9faf7b0 | 2013-10-09 10:27:14 -0400 | [diff] [blame] | 135 | try: |
| 136 | clid = Config().feefie_client_id |
| 137 | except: |
| 138 | clid = self.random_client_id() |
| 139 | |
| 140 | f = Fofum(user=user) |
| 141 | |
| 142 | listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event)) |
| 143 | listener_thread.start() |