def test_example_protos_from_path_get_all_in_file(self):
     cns_path = os.path.join(tf.test.get_temp_dir(), 'dummy_example')
     example = test_utils.make_fake_example()
     test_utils.write_out_examples([example], cns_path)
     dummy_examples = oss_utils.example_protos_from_path(cns_path)
     self.assertEqual(1, len(dummy_examples))
     self.assertEqual(example, dummy_examples[0])
    def _examples_from_path_handler(self, request):
        """Returns JSON of the specified examples.

    Args:
      request: A request that should contain 'examples_path' and 'max_examples'.

    Returns:
      JSON of up to max_examlpes of the tf.train.Examples in the path.
    """
        examples_count = int(request.args.get('max_examples'))
        examples_path = request.args.get('examples_path')
        try:
            oss_utils.throw_if_file_access_not_allowed(examples_path,
                                                       self._logdir,
                                                       self._has_auth_group)
            example_strings = oss_utils.example_protos_from_path(
                examples_path, examples_count, parse_examples=False)
            self.examples = [
                tf.train.Example.FromString(ex) for ex in example_strings
            ]
            self.generate_sprite(example_strings)
            json_examples = [
                json_format.MessageToJson(example) for example in self.examples
            ]
            self.updated_example_indices = set(range(len(json_examples)))
            return http_util.Respond(
                request, {
                    'examples': json_examples,
                    'sprite': True if self.sprite else False
                }, 'application/json')
        except common_utils.InvalidUserInputError as e:
            return http_util.Respond(request, {'error': e.message},
                                     'application/json',
                                     code=400)
 def test_example_protos_from_path_get_two(self):
     cns_path = os.path.join(tf.test.get_temp_dir(), 'dummy_example')
     example_one = test_utils.make_fake_example(1)
     example_two = test_utils.make_fake_example(2)
     example_three = test_utils.make_fake_example(3)
     test_utils.write_out_examples(
         [example_one, example_two, example_three], cns_path)
     dummy_examples = oss_utils.example_protos_from_path(cns_path, 2)
     self.assertEqual(2, len(dummy_examples))
     self.assertEqual(example_one, dummy_examples[0])
     self.assertEqual(example_two, dummy_examples[1])
    def test_example_protos_from_path_use_wildcard(self):
        cns_path = os.path.join(tf.test.get_temp_dir(), 'wildcard_example1')
        example1 = test_utils.make_fake_example(1)
        test_utils.write_out_examples([example1], cns_path)
        cns_path = os.path.join(tf.test.get_temp_dir(), 'wildcard_example2')
        example2 = test_utils.make_fake_example(2)
        test_utils.write_out_examples([example2], cns_path)

        wildcard_path = os.path.join(tf.test.get_temp_dir(),
                                     'wildcard_example*')
        dummy_examples = oss_utils.example_protos_from_path(wildcard_path)
        self.assertEqual(2, len(dummy_examples))
 def test_example_proto_from_path_if_does_not_exist(self):
     cns_path = os.path.join(tf.test.get_temp_dir(), 'does_not_exist')
     with self.assertRaises(common_utils.InvalidUserInputError):
         oss_utils.example_protos_from_path(cns_path)