示例#1
0
 def new_blip(self, **args):
     """Create a blip for testing."""
     data = TEST_BLIP_DATA.copy()
     data.update(args)
     res = blip.Blip(data, self.all_blips, self.operation_queue)
     self.all_blips[res.blip_id] = res
     return res
示例#2
0
    def __init__(self):
        self._running = True
        self._display_surf = None
        self.active_display = None
        self.clock = pygame.time.Clock()
        self.now = 0

        pygame.init()
        pygame.mouse.set_visible(False)

        if sys.platform.startswith("win32"):
            ctypes.windll.user32.SetProcessDPIAware()

        width, height = pygame.display.Info().current_w, pygame.display.Info(
        ).current_h
        self._display_surf = pygame.display.set_mode(
            (width, height),
            pygame.NOFRAME | pygame.HWSURFACE | pygame.DOUBLEBUF)
        self._running = True

        self.blip = blip.Blip(width, height)

        self.starfield_scanner = starfield_scanner.StarfieldScanner(
            width, height, self._display_surf)
        self.radar = radar.Radar(width, height, self._display_surf, self.blip)
        self.forward_sweep = forward_sweep.ForwardSweep(
            width, height, self._display_surf)
        self.active_display = self.starfield_scanner
示例#3
0
  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
示例#4
0
 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
     self.wavelet = wavelet.Wavelet(TEST_WAVELET_DATA, self.all_blips, None,
                                    self.operation_queue)
     self.wavelet.robot_address = ROBOT_NAME
示例#5
0
    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
示例#6
0
    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
示例#7
0
 def testBlipSerialize(self):
     root = self.new_blip(blipId=ROOT_BLIP_ID, childBlipIds=[CHILD_BLIP_ID])
     serialized = root.serialize()
     unserialized = blip.Blip(serialized, self.all_blips,
                              self.operation_queue)
     self.assertEquals(root.blip_id, unserialized.blip_id)
     self.assertEquals(root.child_blip_ids, unserialized.child_blip_ids)
     self.assertEquals(root.contributors, unserialized.contributors)
     self.assertEquals(root.creator, unserialized.creator)
     self.assertEquals(root.text, unserialized.text)
     self.assertEquals(root.last_modified_time,
                       unserialized.last_modified_time)
     self.assertEquals(root.parent_blip_id, unserialized.parent_blip_id)
     self.assertEquals(root.wave_id, unserialized.wave_id)
     self.assertEquals(root.wavelet_id, unserialized.wavelet_id)
     self.assertTrue(unserialized.is_root())
    def reply(self, initial_content=None):
        """Replies to the conversation in this wavelet.

    Args:
      initial_content: If set, start with this (string) content.

    Returns:
      A transient version of the blip that contains the reply.
    """
        if not initial_content:
            initial_content = u'\n'
        initial_content = util.force_unicode(initial_content)
        blip_data = self._operation_queue.wavelet_append_blip(
            self.wave_id, self.wavelet_id, initial_content)

        instance = blip.Blip(blip_data, self._blips, self._operation_queue)
        self._blips._add(instance)
        return instance
    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)

        blips = {}
        for blip_id, raw_blip_data in json['blips'].items():
            blips[blip_id] = blip.Blip(raw_blip_data, blips, pending_ops)

        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, self,
                                 pending_ops)
        robot_address = json.get('robotAddress')
        if robot_address:
            result.robot_address = robot_address
        return result
示例#10
0
  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
示例#11
0
    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.

    """
        operation_queue = ops.OperationQueue(proxy_for_id)
        if not isinstance(message, basestring):
            message = simplejson.dumps(message)

        blip_data, wavelet_data = operation_queue.robot_create_wavelet(
            domain=domain, participants=participants, message=message)

        blips = {}
        root_blip = blip.Blip(blip_data, blips, operation_queue)
        blips[root_blip.blip_id] = root_blip
        created = wavelet.Wavelet(wavelet_data,
                                  blips=blips,
                                  robot=self,
                                  operation_queue=operation_queue)
        if submit:
            result = self._first_rpc_result(self.submit(created))
            if type(result) == list:
                result = result[0]
            # Currently, data is sometimes wrapped in an outer 'data'
            # Remove these 2 lines when that is no longer an issue.
            if 'data' in result and len(result) == 2:
                result = result['data']
            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
            created = wavelet.Wavelet(wavelet_data,
                                      blips=blips,
                                      robot=self,
                                      operation_queue=operation_queue)

        return created