def _wavelet_from_json(self, json, pending_ops): """Construct a wavelet from the passed json. The json should either contain a wavelet and a blips record that define those respective object. The returned wavelet will be constructed using the passed pending_ops OperationQueue. Alternatively the json can be the result of a previous wavelet.serialize() call. In that case the blips will be contaned in the wavelet record. """ if isinstance(json, basestring): json = simplejson.loads(json) # Create blips dict so we can pass into BlipThread objects blips = {} # Setup threads first, as the Blips and Wavelet need to know about them threads = {} # In case of blind_wavelet or new_wave, we may not have threads indo threads_data = json.get('threads', {}) # Create remaining thread objects for thread_id, raw_thread_data in threads_data.items(): threads[thread_id] = wavelet.BlipThread(thread_id, raw_thread_data.get('location'), raw_thread_data.get('blipIds', []), blips, pending_ops) # If being called from blind_wavelet, wavelet is top level info if 'wavelet' in json: raw_wavelet_data = json['wavelet'] elif 'waveletData' in json: raw_wavelet_data = json['waveletData'] else: raw_wavelet_data = json root_thread_data = raw_wavelet_data.get('rootThread') root_thread = wavelet.BlipThread('', root_thread_data.get('location'), root_thread_data.get('blipIds', []), blips, pending_ops) threads[''] = root_thread # Setup the blips, pass in reply threads for blip_id, raw_blip_data in json['blips'].items(): reply_threads = [threads[id] for id in raw_blip_data.get('replyThreadIds', [])] thread = threads.get(raw_blip_data.get('threadId')) blips[blip_id] = blip.Blip(raw_blip_data, blips, pending_ops, thread=thread, reply_threads=reply_threads) result = wavelet.Wavelet(raw_wavelet_data, blips, root_thread, pending_ops, raw_deltas=json.get('rawDeltas')) robot_address = json.get('robotAddress') if robot_address: result.robot_address = robot_address return result
def _wavelet_from_json(self, json, pending_ops): """Construct a wavelet from the passed json. The json should either contain a wavelet and a blips record that define those respective object. The returned wavelet will be constructed using the passed pending_ops OperationQueue. Alternatively the json can be the result of a previous wavelet.serialize() call. In that case the blips will be contaned in the wavelet record. """ if isinstance(json, basestring): json = simplejson.loads(json) # Create blips dict so we can pass into BlipThread objects blips = {} # Setup threads first, as the Blips and Wavelet need to know about them threads = {} threads_data = json.get('threads', {}) # Create remaining thread objects for thread_id, raw_thread_data in threads_data.items(): threads[thread_id] = wavelet.BlipThread( thread_id, raw_thread_data.get('location'), raw_thread_data.get('blipIds', []), blips, pending_ops) # Setup the blips, pass in reply threads for blip_id, raw_blip_data in json['blips'].items(): reply_threads = [] reply_thread_ids = raw_blip_data.get('replyThreadIds', []) for reply_thread_id in reply_thread_ids: reply_threads.append(threads[reply_thread_id]) blips[blip_id] = blip.Blip(raw_blip_data, blips, pending_ops, reply_threads=reply_threads) if 'wavelet' in json: raw_wavelet_data = json['wavelet'] elif 'waveletData' in json: raw_wavelet_data = json['waveletData'] else: raw_wavelet_data = json wavelet_blips = {} wavelet_id = raw_wavelet_data['waveletId'] wave_id = raw_wavelet_data['waveId'] for blip_id, instance in blips.items(): if instance.wavelet_id == wavelet_id and instance.wave_id == wave_id: wavelet_blips[blip_id] = instance result = wavelet.Wavelet(raw_wavelet_data, wavelet_blips, pending_ops) robot_address = json.get('robotAddress') if robot_address: result.robot_address = robot_address return result
def setUp(self): self.operation_queue = ops.OperationQueue() self.all_blips = {} self.blip = blip.Blip(TEST_BLIP_DATA, self.all_blips, self.operation_queue) self.all_blips[self.blip.blip_id] = self.blip root_thread_data = TEST_WAVELET_DATA.get('rootThread') root_thread = wavelet.BlipThread('', root_thread_data.get('location'), root_thread_data.get('blipIds', []), self.all_blips, self.operation_queue) self.wavelet = wavelet.Wavelet(TEST_WAVELET_DATA, self.all_blips, root_thread, self.operation_queue) self.wavelet.robot_address = ROBOT_NAME
def new_wave(self, domain, participants=None, message='', proxy_for_id=None, submit=False): """Create a new wave with the initial participants on it. A new wave is returned with its own operation queue. It the responsibility of the caller to make sure this wave gets submitted to the server, either by calling robot.submit() or by calling .submit_with() on the returned wave. Args: domain: the domain to create the wavelet on. This should in general correspond to the domain of the incoming wavelet. (wavelet.domain). Exceptions are situations where the robot is calling new_wave outside of an event or when the server is handling multiple domains. participants: initial participants on the wave. The robot as the creator of the wave is always added. message: a string that will be passed back to the robot when the WAVELET_CREATOR event is fired. This is a lightweight way to pass around state. submit: if true, use the active gateway to make a round trip to the server. This will return immediately an actual waveid/waveletid and blipId for the root blip. """ util.check_is_valid_proxy_for_id(proxy_for_id) operation_queue = ops.OperationQueue(proxy_for_id) if not isinstance(message, basestring): message = simplejson.dumps(message) # Create temporary wavelet data blip_data, wavelet_data = operation_queue.robot_create_wavelet( domain=domain, participants=participants, message=message) # Create temporary blips dictionary blips = {} root_blip = blip.Blip(blip_data, blips, operation_queue) blips[root_blip.blip_id] = root_blip if submit: # Submit operation to server and return actual wave/blip IDs temp_wavelet = wavelet.Wavelet(wavelet_data, blips=blips, root_thread=None, operation_queue=operation_queue) result = self._first_rpc_result(self.submit(temp_wavelet)) if isinstance(result, list): result = result[0] if 'blipId' in result: blip_data['blipId'] = result['blipId'] wavelet_data['rootBlipId'] = result['blipId'] for field in 'waveId', 'waveletId': if field in result: wavelet_data[field] = result[field] blip_data[field] = result[field] blips = {} root_blip = blip.Blip(blip_data, blips, operation_queue) blips[root_blip.blip_id] = root_blip root_thread = wavelet.BlipThread('', -1, [root_blip.blip_id], blips, operation_queue) new_wavelet = wavelet.Wavelet(wavelet_data, blips=blips, root_thread=root_thread, operation_queue=operation_queue) return new_wavelet