# Path: pyfive/core.py
# def _padded_size(size, padding_multipe=8):
#     """ Return the size of a field padded to be a multiple a give value. """
#     return int(ceil(size / padding_multipe) * padding_multipe)
# 
# def _structure_size(structure):
#     """ Return the size of a structure in bytes. """
#     fmt = '<' + ''.join(structure.values())
#     return struct.calcsize(fmt)
# 
# Path: pyfive/core.py
# def _unpack_struct_from(structure, buf, offset=0):
#     """ Unpack a structure into an OrderedDict from a buffer of bytes. """
#     fmt = '<' + ''.join(structure.values())
#     values = struct.unpack_from(fmt, buf, offset=offset)
#     return OrderedDict(zip(structure.keys(), values))
# 
# def _unpack_struct_from_file(structure, fh):
#     """ Unpack a structure into an OrderedDict from an open file. """
#     size = _structure_size(structure)
#     buf = fh.read(size)
#     return _unpack_struct_from(structure, buf)
# 
# Path: pyfive/core.py
# class InvalidHDF5File(Exception):
#     """ Exception raised when an invalid HDF5 file is detected. """
#     pass


import struct
from collections import OrderedDict
from .core import _padded_size, _structure_size
from .core import _unpack_struct_from, _unpack_struct_from_file
from .core import InvalidHDF5File
""" Misc low-level representation of HDF5 objects. """




class SuperBlock(object):
    """
    HDF5 Superblock.
    """

    def __init__(self, fh, offset):
        """ initalize. """

        fh.seek(offset)
        version_hint = struct.unpack_from('<B', fh.read(9), 8)[0]
        fh.seek(offset)
        if version_hint == 0:
            contents = _unpack_struct_from_file(SUPERBLOCK_V0, fh)
            self._end_of_sblock = offset + SUPERBLOCK_V0_SIZE
        elif version_hint == 2 or version_hint == 3:
            contents = _unpack_struct_from_file(SUPERBLOCK_V2_V3, fh)
            self._end_of_sblock = offset + SUPERBLOCK_V2_V3_SIZE
        else:
            raise NotImplementedError(
                "unsupported superblock version: %i" % (version_hint))

        # verify contents
        if contents['format_signature'] != FORMAT_SIGNATURE:

# Path: pyfive/core.py
# def _padded_size(size, padding_multipe=8):
#     """ Return the size of a field padded to be a multiple a give value. """
#     return int(ceil(size / padding_multipe) * padding_multipe)
# 
# def _structure_size(structure):
#     """ Return the size of a structure in bytes. """
#     fmt = '<' + ''.join(structure.values())
#     return struct.calcsize(fmt)
# 
# Path: pyfive/core.py
# def _unpack_struct_from(structure, buf, offset=0):
#     """ Unpack a structure into an OrderedDict from a buffer of bytes. """
#     fmt = '<' + ''.join(structure.values())
#     values = struct.unpack_from(fmt, buf, offset=offset)
#     return OrderedDict(zip(structure.keys(), values))
# 
# def _unpack_struct_from_file(structure, fh):
#     """ Unpack a structure into an OrderedDict from an open file. """
#     size = _structure_size(structure)
#     buf = fh.read(size)
#     return _unpack_struct_from(structure, buf)
# 
# Path: pyfive/core.py
# class InvalidHDF5File(Exception):
#     """ Exception raised when an invalid HDF5 file is detected. """
#     pass


# Path: pyfive/misc_low_level.py
import struct
from collections import OrderedDict
from .core import _padded_size, _structure_size
from .core import _unpack_struct_from, _unpack_struct_from_file
from .core import InvalidHDF5File

""" Misc low-level representation of HDF5 objects. """




class SuperBlock(object):
    """
    HDF5 Superblock.
    """

    def __init__(self, fh, offset):
        """ initalize. """

        fh.seek(offset)
        version_hint = struct.unpack_from('<B', fh.read(9), 8)[0]
        fh.seek(offset)
        if version_hint == 0:
            contents = _unpack_struct_from_file(SUPERBLOCK_V0, fh)
            self._end_of_sblock = offset + SUPERBLOCK_V0_SIZE
        elif version_hint == 2 or version_hint == 3:
            contents = _unpack_struct_from_file(SUPERBLOCK_V2_V3, fh)
            self._end_of_sblock = offset + SUPERBLOCK_V2_V3_SIZE
        else:
            raise NotImplementedError(
                "unsupported superblock version: %i" % (version_hint))

        # verify contents
        if contents['format_signature'] != FORMAT_SIGNATURE:

            raise InvalidHDF5File('Incorrect file signature')