def load_binary(cls, f, validate=True):
"""
Loads Jubatus binary model file from binary stream ``f``.
When ``validate`` is ``True``, the model file format is strictly validated.
"""
m = cls()
checksum = 0
# Load header
h = cls.Header.load(f)
m.header = h
if validate:
checksum = crc32(h.dumps(False), checksum)
# Load system_data
buf = f.read(h.system_data_size)
m.system = cls.SystemContainer.loads(buf)
if validate:
if h.system_data_size != len(buf):
raise InvalidModelFormatError(
'EOF detected while reading system_data: ' +
'expected {0} bytes, got {1} bytes'.format(h.system_data_size, len(buf)))
checksum = crc32(buf, checksum)
# Load user_data
buf = f.read(h.user_data_size)
m.user = cls.UserContainer.loads(buf)
m._user_raw = buf
if validate:
if h.user_data_size != len(buf):
raise InvalidModelFormatError(
'EOF detected while reading user_data: ' +
'expected {0} bytes, got {1} bytes'.format(h.user_data_size, len(buf)))
checksum = crc32(buf, checksum)
if validate:
# Convert the checksum into 32-bit unsigned integer (for Python 2/3 compatibility)
checksum = checksum & 0xffffffff
# Check CRC
if checksum != h.crc32:
raise InvalidModelFormatError(
'CRC32 mismatch: expected {0}, got {1}'.format(checksum, h.crc32))
return m
评论列表
文章目录