def test_read(self): f = BytesIO(b'\x03\x00' + u'가나다'.encode('utf-16le')) s = BSTR.read(f) self.assertEquals(u'가나다', s) pua = u'\ub098\ub78f\u302e\ub9d0\u302f\uebd4\ubbf8\u302e' pua_utf16le = pua.encode('utf-16le') f = BytesIO(chr(len(pua)) + b'\x00' + pua_utf16le) jamo = BSTR.read(f) expected = u'\ub098\ub78f\u302e\ub9d0\u302f\u110a\u119e\ubbf8\u302e' self.assertEquals(expected, jamo)
def test_read(self): from hwp5.dataio import BSTR from StringIO import StringIO f = StringIO('\x03\x00' + u'가나다'.encode('utf-16le')) s = BSTR.read(f) self.assertEquals(u'가나다', s) pua = u'\ub098\ub78f\u302e\ub9d0\u302f\uebd4\ubbf8\u302e' pua_utf16le = pua.encode('utf-16le') f = StringIO(chr(len(pua)) + '\x00' + pua_utf16le) jamo = BSTR.read(f) expected = u'\ub098\ub78f\u302e\ub9d0\u302f\u110a\u119e\ubbf8\u302e' self.assertEquals(expected, jamo)
def resolve_value_from_stream(item, stream): import struct from hwp5.dataio import readn from hwp5.dataio import BSTR from hwp5.binmodel import ParaTextChunks from hwp5.binmodel import CHID item_type = item['type'] if hasattr(item_type, 'binfmt'): binfmt = item_type.binfmt binsize = struct.calcsize(binfmt) bytes = readn(stream, binsize) unpacked = struct.unpack(binfmt, bytes) return unpacked[0] elif item_type is CHID: bytes = readn(stream, 4) return CHID.decode(bytes) elif item_type is BSTR: return BSTR.read(stream) elif item_type is ParaTextChunks: return ParaTextChunks.read(stream) elif hasattr(item_type, 'fixed_size'): bytes = readn(stream, item_type.fixed_size) if hasattr(item_type, 'decode'): return item_type.decode(bytes) return bytes else: assert hasattr(item_type, 'read') logger.warning('%s: item type relies on its read() to resolve a value', item_type.__name__) return item_type.read(stream)
def resolve_value_from_stream(item, stream): import struct from hwp5.dataio import readn from hwp5.dataio import BSTR from hwp5.binmodel import ParaTextChunks from hwp5.binmodel import CHID if 'bin_type' in item: item_type = item['bin_type'] else: item_type = item['type'] if hasattr(item_type, 'binfmt'): binfmt = item_type.binfmt binsize = struct.calcsize(binfmt) bytes = readn(stream, binsize) unpacked = struct.unpack(binfmt, bytes) return unpacked[0] elif item_type is CHID: bytes = readn(stream, 4) return CHID.decode(bytes) elif item_type is BSTR: return BSTR.read(stream) elif item_type is ParaTextChunks: return ParaTextChunks.read(stream) elif hasattr(item_type, 'fixed_size'): bytes = readn(stream, item_type.fixed_size) if hasattr(item_type, 'decode'): return item_type.decode(bytes) return bytes else: assert hasattr(item_type, 'read') logger.warning('%s: item type relies on its read() to resolve a value', item_type.__name__) return item_type.read(stream)
def test_read(self): f = BytesIO(b'\x03\x00' + u'가나다'.encode('utf-16le')) s = BSTR.read(f) self.assertEqual(u'가나다', s) pua = u'\ub098\ub78f\u302e\ub9d0\u302f\uebd4\ubbf8\u302e' pua_utf16le = pua.encode('utf-16le') if PY3: lengthbyte = bytes([len(pua)]) else: lengthbyte = chr(len(pua)) f = BytesIO(lengthbyte + b'\x00' + pua_utf16le) jamo = BSTR.read(f) expected = u'\ub098\ub78f\u302e\ub9d0\u302f\uebd4\ubbf8\u302e' self.assertEqual(expected, jamo)
def test_BSTR(self): assert type(BSTR(u'abc')) is unicode