class RPC(pb.Referenceable, resource.Resource): isLeaf = True def __init__(self): ibid.rpc[self.features[0]] = self self.form = templates.get_template('plugin_form.html') self.list = templates.get_template('plugin_functions.html') def get_function(self, request): method = request.postpath[0] if hasattr(self, 'remote_%s' % method): return getattr(self, 'remote_%s' % method) return None def render_POST(self, request): args = [] for arg in request.postpath[1:]: try: arg = json.loads(arg) except ValueError, e: pass args.append(arg) kwargs = {} for key, value in request.args.items(): try: value = json.loads(value[0]) except ValueError, e: value = value[0] kwargs[key] = value
def _event(self, data): "Handle a JSON stream event, data is the JSON" d = json.loads(data) if d['user_id'] == self.my_id: return type = d['type'] if type.endswith('Message'): type = type[:-7] if type == 'Enter': self.get_room_info(d['room_id']) if hasattr(self, 'handle_' + type): params = {} params['room_id'] = d['room_id'] params['room_name'] = self._rooms[d['room_id']]['name'] if d.get('user_id') is not None: if d['user_id'] not in self._users: # User list not loaded yet, stick it on a queue: self._room_info_queue.append((d['room_id'], data)) return params['user_id'] = d['user_id'] params['user_name'] = self._users[d['user_id']]['name'] if d.get('body', None) is not None: params['body'] = d['body'] getattr(self, 'handle_' + type)(**params)
def json_webservice(url, params={}, headers={}): "Request data from a JSON webservice, and deserialise" data = generic_webservice(url, params, headers) try: return json.loads(data) except ValueError, e: raise JSONException(e)
def render_POST(self, request): args = [] for arg in request.postpath[1:]: try: arg = json.loads(arg) except ValueError, e: pass args.append(arg)
def _do_room_list(self, data): log.debug(u'Parsing room list') roommeta = json.loads(data)['rooms'] for room in roommeta: # We want this present before we get to room metadata self._rooms[room['id']] = {'name': room['name']} if room['name'] in self.rooms: logging.debug(u'Connecting to: %s', room['name']) self.join_room(room['id'])
def _do_room_info(self, data, join): d = json.loads(data)['room'] r = self._rooms[d['id']] for k, v in d.iteritems(): if k != 'users': r[k] = v r['users'] = set() for user in d['users']: self._users[user['id']] = u = user r['users'].add(user['id']) if join: self.joined_room(r) queue, self._room_info_queue = self._room_info_queue, [] for room_id, item in queue: if room_id == d['id']: self._event(item) else: self._room_info_queue.append((room_id, item))
def _do_get_id(self, data): log.debug(u'Parsing my info') meta = json.loads(data)['user'] self.my_id = meta['id'] self.my_name = meta['name'] self.get_room_list()