Example #1
0
class JournalNavigator(object):
  """Iterates over journal JSON."""

  def __init__(self):
    """Constructor"""
    self.__input_stream = None
    self.__decoder = json.JSONDecoder()

  def __iter__(self):
    """Iterate over the contents of the journal."""
    self.__check_open()
    return self

  def open(self, path):
    """Open the journal to be able to iterate over its contents.

    Args:
      path: [string] The path to load the journal from.
    """
    if self.__input_stream != None:
      raise ValueError('Navigator is already open.')
    self.__input_stream = RecordInputStream(open(path, 'r'))

  def close(self):
    """Close the journal."""
    self.__check_open()
    self.__input_stream.close()
    self.__input_stream = None

  def next(self):
    """Return the next item in the journal.

    Raises:
      StopIteration when there are no more elements.
    """
    self.__check_open()
    json_str = self.__input_stream.next()

    try:
      return self.__decoder.decode(json_str)

    except ValueError:
      print 'Invalid json record:\n{0}'.format(json_str)
      raise

  def __check_open(self):
    """Verify that the navigator is open (and thus valid to iterate)."""
    if self.__input_stream is None:
      raise ValueError('Navigator is not open.')
Example #2
0
    def test_write_message_with_metadata(self):
        """Verify the journal messages contain the metadata we add."""
        output = StringIO()
        journal = TestJournal(output)
        offset = len(output.getvalue())

        journal.write_message('My message.', str='ABC', num=123)
        metadata = {'str': 'ABC', 'num': 123}
        message_json_text = self.expect_message_text(journal.clock,
                                                     'My message.', metadata)

        input_stream = RecordInputStream(StringIO(output.getvalue()[offset:]))
        decoder = json.JSONDecoder(encoding='ASCII')
        expect_obj = decoder.decode(message_json_text)
        got_obj = decoder.decode(input_stream.next())
        self.assertItemsEqual(expect_obj, got_obj)
Example #3
0
  def test_write_message_with_metadata(self):
    """Verify the journal messages contain the metadata we add."""
    output = StringIO()
    journal = TestJournal(output)
    offset = len(output.getvalue())

    journal.write_message('My message.', str='ABC', num=123)
    metadata = {'str': 'ABC', 'num': 123}
    message_json_text = self.expect_message_text(
        journal.clock, 'My message.', metadata)

    input_stream = RecordInputStream(StringIO(output.getvalue()[offset:]))
    decoder = json.JSONDecoder(encoding='ASCII')
    expect_obj = decoder.decode(message_json_text)
    got_obj = decoder.decode(input_stream.next())
    self.assertItemsEqual(expect_obj, got_obj)
Example #4
0
  def test_context_logging(self):
      offset = len(_journal_file.getvalue())
      logger = JournalLogger('test_journal_logger')
      logger.addHandler(JournalLogHandler(path=None))
      citest_extra = {'foo':'bar'}
      start_time = _journal_clock.last_time

      JournalLogger.execute_in_context(
        'The Test Context',
        lambda: {logger.debug('Test Log Message')},
        **citest_extra)

      expect_sequence = [
        {
          '_title': 'The Test Context',
          '_type': 'JournalContextControl',
          '_timestamp': start_time + 1,
          '_thread': thread.get_ident(),
          'control': 'BEGIN',
          'foo': 'bar',
        },
        {
          '_value': 'Test Log Message',
          '_type': 'JournalMessage',
          '_level': logging.DEBUG,
          '_timestamp': start_time + 2,
          '_thread': thread.get_ident(),
          'format': 'pre'
        },
        {
          '_type': 'JournalContextControl',
          '_timestamp': start_time + 3,
          '_thread': thread.get_ident(),
          'control': 'END'
        }
      ]

      entry_str = _journal_file.getvalue()[offset:]
      input_stream = RecordInputStream(StringIO(entry_str))
      for expect in expect_sequence:
        json_str = input_stream.next()
        json_dict = json_module.JSONDecoder(encoding='utf-8').decode(json_str)
        self.assertEqual(expect, json_dict)
Example #5
0
    def test_store(self):
        """Verify we store objects as JSON snapshots."""
        data = TestData('NAME', 1234, TestDetails())
        decoder = json.JSONDecoder(encoding='ASCII')
        snapshot = JsonSnapshot()
        snapshot.add_object(data)

        time_function = lambda: 1.23
        journal = Journal(time_function)
        output = StringIO()
        journal.open_with_file(output)
        offset = len(output.getvalue())

        journal.store(data)
        contents = output.getvalue()
        got_stream = RecordInputStream(StringIO(contents[offset:]))
        got_json_str = got_stream.next()
        got = decoder.decode(got_json_str)
        json_object = snapshot.to_json_object()
        json_object['_timestamp'] = time_function()
        json_object['_thread'] = threading.current_thread().ident
        self.assertItemsEqual(json_object, got)
    def test_context_logging(self):
        offset = len(_journal_file.getvalue())
        logger = JournalLogger('test_journal_logger')
        logger.addHandler(JournalLogHandler(path=None))
        citest_extra = {'foo': 'bar'}
        start_time = _journal_clock.last_time

        JournalLogger.execute_in_context(
            'The Test Context', lambda: {logger.debug('Test Log Message')},
            **citest_extra)

        expect_sequence = [{
            '_title': 'The Test Context',
            '_type': 'JournalContextControl',
            '_timestamp': start_time + 1,
            '_thread': thread.get_ident(),
            'control': 'BEGIN',
            'foo': 'bar',
        }, {
            '_value': 'Test Log Message',
            '_type': 'JournalMessage',
            '_level': logging.DEBUG,
            '_timestamp': start_time + 2,
            '_thread': thread.get_ident(),
            'format': 'pre'
        }, {
            '_type': 'JournalContextControl',
            '_timestamp': start_time + 3,
            '_thread': thread.get_ident(),
            'control': 'END'
        }]

        entry_str = _journal_file.getvalue()[offset:]
        input_stream = RecordInputStream(StringIO(entry_str))
        for expect in expect_sequence:
            json_str = input_stream.next()
            json_dict = json_module.JSONDecoder(
                encoding='utf-8').decode(json_str)
            self.assertEqual(expect, json_dict)
Example #7
0
  def test_store(self):
    """Verify we store objects as JSON snapshots."""
    data = TestData('NAME', 1234, TestDetails())
    decoder = json.JSONDecoder(encoding='ASCII')
    snapshot = JsonSnapshot()
    snapshot.add_object(data)

    time_function = lambda: 1.23
    journal = Journal(time_function)
    output = StringIO()
    journal.open_with_file(output)
    offset = len(output.getvalue())

    journal.store(data)
    contents = output.getvalue()
    got_stream = RecordInputStream(StringIO(contents[offset:]))
    got_json_str = got_stream.next()
    got = decoder.decode(got_json_str)
    json_object = snapshot.to_json_object()
    json_object['_timestamp'] = time_function()
    json_object['_thread'] = threading.current_thread().ident
    self.assertItemsEqual(json_object, got)