from google.protobuf import json_format
import os
import json
import sys

from absl import logging


class Reader:
    def __init__(self, data_file_path):
        with open(data_file_path) as reader:
            data_file_conf = json.load(reader)

        data_file_base_path = os.path.dirname(data_file_path)

        index_file_path = os.path.join(data_file_base_path, data_file_conf["index"][0])
        binary_file_path = os.path.join(data_file_base_path, data_file_conf["data"][0])

        with open(index_file_path, "r") as object_index_file:
            self.object_index = json.load(object_index_file)

        self.binary_file_path = binary_file_path

    def each(self):
        with open(self.binary_file_path, "rb") as object_pb_file:
            for obj_idx in self.object_index["index"]:
                offset = obj_idx[1]
                size = obj_idx[2]

                object_pb_file.seek(offset)
                bts = object_pb_file.read(size)

                logging.debug("data size %d", size)

                yield bts
