def read(cls, fp): """Read the element from a file-like object. :param fp: file-like object """ name = read_unicode_string(fp) classID = read_length_and_key(fp) value = read_unicode_string(fp) return cls(name, classID, value)
def read(cls, fp, **kwargs): kind = LinkedLayerType(read_fmt('4s', fp)[0]) version = read_fmt('I', fp)[0] assert 1 <= version and version <= 7, 'Invalid version %d' % (version) uuid = read_pascal_string(fp, 'macroman', padding=1) filename = read_unicode_string(fp) filetype, creator, datasize, open_file = read_fmt('4s4sQB', fp) if open_file: open_file = DescriptorBlock.read(fp, padding=1) else: open_file = None linked_file = None timestamp = None data = None filesize = None child_id = None mod_time = None lock_state = None if kind == LinkedLayerType.EXTERNAL: linked_file = DescriptorBlock.read(fp, padding=1) if version > 3: timestamp = read_fmt('I4Bd', fp) filesize = read_fmt('Q', fp)[0] # External file size. if version > 2: data = fp.read(datasize) elif kind == LinkedLayerType.ALIAS: read_fmt('8x', fp) if kind == LinkedLayerType.DATA: data = fp.read(datasize) assert len(data) == datasize, '(%d vs %d)' % (len(data), datasize) # The followings are not well documented... if version >= 5: child_id = read_unicode_string(fp) if version >= 6: mod_time = read_fmt('d', fp)[0] if version >= 7: lock_state = read_fmt('B', fp)[0] if kind == LinkedLayerType.EXTERNAL and version == 2: data = fp.read(datasize) return cls(kind, version, uuid, filename, filetype, creator, filesize, open_file, linked_file, timestamp, data, child_id, mod_time, lock_state)
def test_unicode_string_wr(fixture, padding): with io.BytesIO() as f: write_unicode_string(f, fixture, padding=padding) data = f.getvalue() with io.BytesIO(data) as f: output = read_unicode_string(f, padding=padding) assert fixture == output
def read(cls, fp): """Read the element from a file-like object. :param fp: file-like object """ name = read_unicode_string(fp) classID = read_length_and_key(fp) offset = read_fmt('I', fp)[0] return cls(name, classID, offset)
def test_unicode_stringrw(fixture, padding): with io.BytesIO(fixture) as f: data = read_unicode_string(f, padding=padding) print(len(fixture), f.tell()) print('%d %r' % (len(data), data)) with io.BytesIO() as f: write_unicode_string(f, data, padding=padding) output = f.getvalue() assert fixture == output
def read(cls, fp): slice_id, group_id, origin = read_fmt('3I', fp) associated_id = read_fmt('I', fp)[0] if origin == 1 else None name = read_unicode_string(fp) slice_type = read_fmt('I', fp)[0] bbox = read_fmt('4I', fp) url = read_unicode_string(fp) target = read_unicode_string(fp) message = read_unicode_string(fp) alt_tag = read_unicode_string(fp) cell_is_html = read_fmt('?', fp)[0] cell_text = read_unicode_string(fp) horizontal_align, vertical_align = read_fmt('2I', fp) alpha, red, green, blue = read_fmt('4B', fp) data = None if is_readable(fp, 4): # There is no easy distinction between descriptor block and # next slice v6 item here... current_position = fp.tell() version = read_fmt('I', fp)[0] fp.seek(-4, 1) if version == 16: try: data = DescriptorBlock.read(fp) except ValueError: logger.debug('Failed to read DescriptorBlock') fp.seek(current_position) return cls(slice_id, group_id, origin, associated_id, name, slice_type, bbox, url, target, message, alt_tag, cell_is_html, cell_text, horizontal_align, vertical_align, alpha, red, green, blue, data)
def _read_body(cls, fp): name = read_unicode_string(fp, padding=1) classID = read_length_and_key(fp) items = [] count = read_fmt('I', fp)[0] for _ in range(count): key = read_length_and_key(fp) ostype = OSType(fp.read(4)) kls = TYPES.get(ostype) value = kls.read(fp) items.append((key, value)) return dict(name=name, classID=classID, items=items)
def read(cls, fp, **kwargs): version = read_fmt('I', fp)[0] assert version == 1, 'Invalid version %d' % (version) image_mode = ColorMode(read_fmt('I', fp)[0]) point = read_fmt('2h', fp) name = read_unicode_string(fp) pattern_id = read_pascal_string(fp, encoding='ascii', padding=1) color_table = None if image_mode == ColorMode.INDEXED: color_table = [read_fmt("3B", fp) for i in range(256)] read_fmt('4x', fp) data = VirtualMemoryArrayList.read(fp) return cls(version, image_mode, point, name, pattern_id, color_table, data)
def read(cls, fp, **kwargs): version, is_reversed, is_dithered = read_fmt('H2B', fp) assert version == 1, 'Invalid version %s' % (version) name = read_unicode_string(fp) count = read_fmt('H', fp)[0] color_stops = [ColorStop.read(fp) for _ in range(count)] count = read_fmt('H', fp)[0] transparency_stops = [TransparencyStop.read(fp) for _ in range(count)] expansion, interpolation, length, mode = read_fmt('4H', fp) assert expansion == 2, 'Invalid expansion %d' % (expansion) random_seed, show_transparency, use_vector_color = read_fmt('I2H', fp) roughness, color_model = read_fmt('IH', fp) minimum_color = read_fmt('4H', fp) maximum_color = read_fmt('4H', fp) read_fmt('2x', fp) # Dummy? return cls(version, is_reversed, is_dithered, name, color_stops, transparency_stops, expansion, interpolation, length, mode, random_seed, show_transparency, use_vector_color, roughness, color_model, minimum_color, maximum_color)
def read(cls, fp, padding=1, **kwargs): return cls(read_unicode_string(fp, padding=padding))
def read(cls, fp, **kwargs): version, has_composite = read_fmt('I?', fp) writer = read_unicode_string(fp) reader = read_unicode_string(fp) file_version = read_fmt('I', fp)[0] return cls(version, has_composite, writer, reader, file_version)
def read(cls, fp): number, id = read_fmt('2I', fp) name = read_unicode_string(fp) return cls(number, id, name)
def read(cls, fp): bbox = read_fmt('4I', fp) name = read_unicode_string(fp) count = read_fmt('I', fp)[0] items = [SliceV6.read(fp) for _ in range(count)] return cls(bbox, name, items)
def read(cls, fp, **kwargs): items = [] while is_readable(fp): items.append(read_unicode_string(fp)) return cls(items)