def test_linked_layers_wr(): linked_layers = LinkedLayers([ LinkedLayer(), LinkedLayer(kind=LinkedLayerType.DATA, data=b'\x01\x02\x03\x04'), LinkedLayer(kind=LinkedLayerType.ALIAS), LinkedLayer(kind=LinkedLayerType.EXTERNAL, data=None, filesize=4, linked_file=DescriptorBlock(), version=1), LinkedLayer(kind=LinkedLayerType.EXTERNAL, data=b'\x01\x02\x03\x04', filesize=4, linked_file=DescriptorBlock(), version=2), LinkedLayer(kind=LinkedLayerType.EXTERNAL, data=b'\x01\x02\x03\x04', filesize=4, linked_file=DescriptorBlock(), version=3), LinkedLayer(kind=LinkedLayerType.EXTERNAL, data=b'\x01\x02\x03\x04', filesize=4, linked_file=DescriptorBlock(), version=7, timestamp=(2000, 1, 1, 0, 0, 0.0), child_id='', mod_time=200000.1, lock_state=1, open_file=DescriptorBlock()), ]) check_write_read(linked_layers)
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(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 read(cls, fp, **kwargs): version = read_fmt('H', fp)[0] transform = read_fmt('6d', fp) text_version = read_fmt('H', fp)[0] text_data = DescriptorBlock.read(fp) # Engine data. if b'EngineData' in text_data: try: engine_data = text_data[b'EngineData'].value engine_data = EngineData.frombytes(engine_data) text_data[b'EngineData'].value = engine_data except: logger.warning('Failed to read engine data') warp_version = read_fmt('H', fp)[0] warp = DescriptorBlock.read(fp) left, top, right, bottom = read_fmt("4i", fp) return cls(version, transform, text_version, text_data, warp_version, warp, left, top, right, bottom)
def read(cls, fp, **kwargs): signature = read_fmt('4s', fp)[0] assert signature == b'8BIM', 'Invalid signature %r' % signature key, copy_on_sheet = read_fmt("4s?3x", fp) data = read_length_block(fp) if key == b'mdyn': with io.BytesIO(data) as f: data = read_fmt('I', f)[0] elif key in (b'cust', b'cmls', b'extn'): data = DescriptorBlock.frombytes(data, padding=4) else: message = 'Unknown metadata key %r' % (key) logger.warning(message) warn(message) data = data return cls(signature, key, copy_on_sheet, data)
def read(cls, fp, **kwargs): version = read_fmt('I', fp)[0] assert version in (6, 7, 8), 'Invalid version %d' % (version) if version == 6: return cls(version=version, data=SlicesV6.read(fp)) return cls(version=version, data=DescriptorBlock.read(fp))
from psd_tools2.psd.descriptor import DescriptorBlock from psd_tools2.psd.linked_layer import LinkedLayer, LinkedLayers from ..utils import check_write_read logger = logging.getLogger(__name__) @pytest.mark.parametrize('kwargs', [ dict(), dict(kind=LinkedLayerType.DATA, data=b'\x01\x02\x03\x04'), dict(kind=LinkedLayerType.ALIAS), dict(kind=LinkedLayerType.EXTERNAL, data=None, filesize=4, linked_file=DescriptorBlock(), version=1), dict(kind=LinkedLayerType.EXTERNAL, data=b'\x01\x02\x03\x04', filesize=4, linked_file=DescriptorBlock(), version=2), dict(kind=LinkedLayerType.EXTERNAL, data=b'\x01\x02\x03\x04', filesize=4, linked_file=DescriptorBlock(), version=3), dict(kind=LinkedLayerType.EXTERNAL, data=b'\x01\x02\x03\x04', filesize=4, linked_file=DescriptorBlock(),
def read(cls, fp, **kwargs): kind, version = read_fmt('4sI', fp) data = DescriptorBlock.read(fp) return cls(kind, version, data)