def _set_key(self): self._key = None for key in self.tagged_blocks: if (TaggedBlock.is_adjustment_key(key) or TaggedBlock.is_fill_key(key)): self._key = key return logger.error("Unknown adjustment layer: {}".format(self))
def parse_tagged_block(block): """ Replaces "data" attribute of a block with parsed data structure if it is known how to parse it. """ if not TaggedBlock.is_known(block.key): warnings.warn("Unknown tagged block (%s)" % block.key) decoder = _tagged_block_decoders.get(block.key, lambda data: data) return Block(block.key, decoder(block.data))
def _repr_pretty_(self, p, cycle): if cycle: p.text(repr(self)) else: with p.group(0, 'Block(', ')'): p.text("%s %s, " % (self.key, TaggedBlock.name_of(self.key))) if isinstance(self.data, bytes): p.text(trimmed_repr(self.data)) else: p.pretty(self.data)
def _repr_pretty_(self, p, cycle): if cycle: p.text('Block(...)') else: with p.group(1, 'Block(', ')'): p.breakable() p.text("%s %s," % (self.key, TaggedBlock.name_of(self.key))) p.breakable() if isinstance(self.data, bytes): p.text(trimmed_repr(self.data)) else: p.pretty(self.data)
def _read_additional_layer_info_block(fp, padding): """ Reads a tagged block with additional layer information. """ sig = fp.read(4) if sig not in (b'8BIM', b'8B64'): raise Error("Invalid signature of tagged block (%r)" % sig) key = fp.read(4) length = read_fmt("I", fp)[0] if not length: logger.debug( "Found tagged block with no data (%s %s). Dropping..." % ( key, TaggedBlock.name_of(key) )) return None if padding > 0: length = pad(length, padding) data = fp.read(length) return Block(key, data)
def adjustment_type(self): """Type of adjustment.""" return TaggedBlock.human_name_of(self._key).replace("-setting", "")
def build(self, decoded_data): """Build the tree structure.""" self.decoded_data = decoded_data layer_records = decoded_data.layer_and_mask_data.layers.layer_records group_stack = [self] clip_stack = [] for index, record in reversed(list(enumerate(layer_records))): current_group = group_stack[-1] blocks = dict(record.tagged_blocks) divider = blocks.get( TaggedBlock.SECTION_DIVIDER_SETTING, blocks.get(TaggedBlock.NESTED_SECTION_DIVIDER_SETTING), ) if divider: if divider.type in (SectionDivider.CLOSED_FOLDER, SectionDivider.OPEN_FOLDER): layer = Group(current_group, index) group_stack.append(layer) elif divider.type == SectionDivider.BOUNDING_SECTION_DIVIDER: if len(group_stack) == 1: # This means that there is a BOUNDING_SECTION_DIVIDER # without an OPEN_FOLDER before it. Create a new group # and move layers to this new group in this case. # Assume the first layer is a group # and convert it to a group: layers = group_stack[0].layers[0] group = Group(current_group, layers[0]._index) group._layers = layers[1:] # replace moved layers with newly created group: group_stack[0].layers = [group] else: assert group_stack.pop() is not self continue else: logger.warning("Invalid state") elif TaggedBlock.TYPE_TOOL_OBJECT_SETTING in blocks: layer = TypeLayer(current_group, index) elif ((TaggedBlock.VECTOR_ORIGINATION_DATA in blocks or TaggedBlock.VECTOR_MASK_SETTING1 in blocks or TaggedBlock.VECTOR_MASK_SETTING2 in blocks or TaggedBlock.VECTOR_STROKE_DATA in blocks or TaggedBlock.VECTOR_STROKE_CONTENT_DATA in blocks) and record.flags.pixel_data_irrelevant): layer = ShapeLayer(current_group, index) elif (TaggedBlock.SMART_OBJECT_PLACED_LAYER_DATA in blocks or TaggedBlock.PLACED_LAYER_OBSOLETE2 in blocks or TaggedBlock.PLACED_LAYER_DATA in blocks): layer = SmartObjectLayer(current_group, index) elif any([ TaggedBlock.is_adjustment_key(key) or TaggedBlock.is_fill_key(key) for key in blocks.keys() ]): layer = AdjustmentLayer(current_group, index) else: layer = PixelLayer(current_group, index) if record.clipping: clip_stack.append(layer) else: layer._clip_layers = clip_stack clip_stack = [] current_group._layers.append(layer)
def __repr__(self): return "Block(%s %s, %s)" % (self.key, TaggedBlock.name_of(self.key), trimmed_repr(self.data))
def __repr__(self): return "Block(%s %s, %s)" % (self.key, TaggedBlock.name_of( self.key), trimmed_repr(self.data))