def write(self, fp, padding=1, **kwargs): written = write_fmt(fp, '4sI', self.kind.value, self.version) written += write_pascal_string(fp, self.uuid, 'macroman', padding=1) written += write_unicode_string(fp, self.filename) written += write_fmt(fp, '4s4sQB', self.filetype, self.creator, len(self.data) if self.data is not None else 0, self.open_file is not None) if self.open_file is not None: written += self.open_file.write(fp, padding=1) if self.kind == LinkedLayerType.EXTERNAL: written += self.linked_file.write(fp, padding=1) if self.version > 3: written += write_fmt(fp, 'I4Bd', *self.timestamp) written += write_fmt(fp, 'Q', self.filesize) if self.version > 2: written += write_bytes(fp, self.data) elif self.kind == LinkedLayerType.ALIAS: written += write_fmt(fp, '8x') if self.kind == LinkedLayerType.DATA: written += write_bytes(fp, self.data) if self.child_id is not None: written += write_unicode_string(fp, self.child_id) if self.mod_time is not None: written += write_fmt(fp, 'd', self.mod_time) if self.lock_state is not None: written += write_fmt(fp, 'B', self.lock_state) if self.kind == LinkedLayerType.EXTERNAL and self.version == 2: written += write_bytes(fp, self.data) written += write_padding(fp, written, padding) return written
def write(self, fp, indent=0, write_container=True): inner_indent = indent if indent is None else indent + 1 written = 0 if write_container: if indent == 0: written += self._write_newline(fp, indent) written += self._write_newline(fp, indent) written += self._write_indent(fp, indent) written += write_bytes(fp, b'<<') written += self._write_newline(fp, indent) for key in self: written += self._write_indent(fp, inner_indent) written += key.write(fp) value = self[key] if isinstance(value, Dict): written += value.write(fp, indent=inner_indent) else: written += write_bytes(fp, b' ') if isinstance(value, List): if len(value) > 0 and isinstance(value[0], Dict): written += value.write(fp, indent=inner_indent) else: written += value.write(fp, indent=None) else: written += value.write(fp) written += self._write_newline(fp, indent) if write_container: written += self._write_indent(fp, indent) written += write_bytes(fp, b'>>') return written
def write_length_and_key(fp, value): """ Helper to write descriptor classID and key. """ if isinstance(value, DescriptorClassID): length = (len(value.value) != 4) * len(value.value) written = write_fmt(fp, 'I', length) written += write_bytes(fp, value.value) elif value in _UNKNOWN_CLASS_ID: written = write_fmt(fp, 'I', 0) written += write_bytes(fp, value) else: written = write_fmt(fp, 'I', len(value)) written += write_bytes(fp, value) return written
def write(self, fp): value = b'%.8f' % (self.value) value = value.rstrip(b'0') value = value + b'0' if value.endswith(b'.') else value if 0.0 < abs(self.value) and abs(self.value) < 1.0: value = value.replace(b'0.', b'.') return write_bytes(fp, value)
def write(self, fp, **kwargs): written = write_fmt( fp, '6I2H', self.fmt, self.width, self.height, self.row, self.total_size, len(self.data), self.bits, self.planes ) written += write_bytes(fp, self.data) return written
def write(self, fp): written = write_fmt( fp, '3I', self.slice_id, self.group_id, self.origin ) if self.origin == 1 and self.associated_id is not None: written += write_fmt(fp, 'I', self.associated_id) written += write_unicode_string(fp, self.name, padding=1) written += write_fmt(fp, 'I', self.slice_type) written += write_fmt(fp, '4I', *self.bbox) written += write_unicode_string(fp, self.url, padding=1) written += write_unicode_string(fp, self.target, padding=1) written += write_unicode_string(fp, self.message, padding=1) written += write_unicode_string(fp, self.alt_tag, padding=1) written += write_fmt(fp, '?', self.cell_is_html) written += write_unicode_string(fp, self.cell_text, padding=1) written += write_fmt( fp, '2I', self.horizontal_align, self.vertical_align ) written += write_fmt( fp, '4B', self.alpha, self.red, self.green, self.blue ) if self.data is not None: if hasattr(self.data, 'write'): written += self.data.write(fp, padding=1) elif self.data: written += write_bytes(fp, self.data) return written
def _write_body(self, fp): written = write_fmt(fp, 'I', self.depth) written += write_fmt(fp, '4I', *self.rectangle) written += write_fmt(fp, 'HB', self.pixel_depth, self.compression.value) written += write_bytes(fp, self.data) return written
def write(self, fp, padding=4, **kwargs): written = 0 for item in self: written += write_length_block(fp, lambda f: write_bytes(f, item), fmt='Q') written += write_padding(fp, written, padding) return written
def writer(f): if hasattr(self.data, 'write'): # It seems padding size applies at the block level here. inner_padding = 1 if padding == 4 else 4 return self.data.write(f, padding=inner_padding, version=version) return write_bytes(f, self.data)
def write_length_and_key(fp, value): """ Helper to write descriptor key. """ written = write_fmt(fp, 'I', 0 if value in _TERMS else len(value)) written += write_bytes(fp, value) return written
def write(self, fp, indent=None): written = write_bytes(fp, b'[') if indent is None: for item in self: if isinstance(item, Dict): written += item.write(fp, indent=None) else: written += write_bytes(fp, b' ') written += item.write(fp) written += write_bytes(fp, b' ') else: for item in self: written += item.write(fp, indent=indent) written += self._write_newline(fp, indent) written += self._write_indent(fp, indent) written += write_bytes(fp, b']') return written
def write(self, fp, *args, **kwargs): written = 0 for item in self: if hasattr(item, 'write'): written += item.write(fp, *args, **kwargs) elif isinstance(item, bytes): written += write_bytes(fp, item) return written
def _write_body(self, fp): written = write_unicode_string(fp, self.name, padding=1) written += write_length_and_key(fp, self.classID) written += write_fmt(fp, 'I', len(self)) for key in self: written += write_length_and_key(fp, key) written += write_bytes(fp, self[key].ostype.value) written += self[key].write(fp) return written
def write(self, fp, **kwargs): written = write_fmt(fp, '2HI', self.major_version, self.minor_version, len(self)) for item in self: data = item.tobytes() written += write_fmt(fp, 'I', len(data) + 4) written += write_bytes(fp, data) written += write_padding(fp, written, 4) return written
def write(self, fp, *args, **kwargs): written = 0 for key in self: value = self[key] if hasattr(value, 'write'): written += value.write(fp, *args, **kwargs) elif isinstance(value, bytes): written += write_bytes(fp, value) return written
def write(self, fp, **kwargs): """Write the element to a file-like object. :param fp: file-like object """ written = write_fmt(fp, 'H', self.compression.value) written += write_bytes(fp, self.data) # written += write_padding(fp, written, 2) # Seems no padding here. return written
def write(self, fp): """Write the element to a file-like object. :param fp: file-like object """ written = write_fmt(fp, 'I', len(self)) for item in self: written += write_bytes(fp, item.ostype.value) written += item.write(fp) return written
def write(self, fp): """Write the element to a file-like object. :param fp: file-like object :rtype: int """ start_pos = fp.tell() written = write_fmt(fp, 'H', self.compression.value) written += write_bytes(fp, self.data) logger.debug(' wrote image data, len=%d' % (fp.tell() - start_pos)) return written
def write(self, fp, **kwargs): written = write_fmt(fp, '4s2BH', self.kind, self.is_open, self.flags, self.optional_blocks) written += write_fmt(fp, '4i', *self.icon_location) written += write_fmt(fp, '4i', *self.popup_location) written += self.color.write(fp) written += write_pascal_string(fp, self.author, 'macroman', padding=2) written += write_pascal_string(fp, self.name, 'macroman', padding=2) written += write_pascal_string(fp, self.mod_date, 'macroman', padding=2) written += write_fmt(fp, 'I4s', len(self.data) + 12, self.marker) written += write_length_block(fp, lambda f: write_bytes(f, self.data)) return written
def write(self, fp, **kwargs): written = write_fmt(fp, '2H', self.version, self.monochrome) written += write_fmt(fp, '5h', *self.data) written += write_bytes(fp, self.unknown) return written
def write(self, fp): return write_bytes(fp, self.value)
def writer(f): if hasattr(self.data, 'write'): return self.data.write(f, padding=4) elif isinstance(self.data, int): return write_fmt(fp, 'I', self.data) return write_bytes(f, self.data)
def write(self, fp): return write_bytes(fp, b'/' + self.value.encode('macroman'))
def write(self, fp, indent=0): return write_bytes(fp, b'true' if self.value else b'false')
def write(self, fp, indent=0): return write_bytes(fp, b'%d' % (self.value))
def write(self, fp): value = self.value.encode('utf-16-be') for c in self._ESCAPED_CHARS: value = value.replace(c, b'\\' + c) return write_bytes(fp, b'(' + codecs.BOM_UTF16_BE + value + b')')
def _write_newline(self, fp, indent): if indent is None: return 0 return write_bytes(fp, b'\n')
def _write_indent(self, fp, indent): if indent is None: return write_bytes(fp, b' ') return write_bytes(fp, b'\t' * (indent))
def write(self, fp): written = write_fmt(fp, 'I', len(self)) for item in self: written += write_bytes(fp, item.ostype.value) written += item.write(fp) return written
def writer(f): if hasattr(self.value, 'write'): return self.value.write(f) return write_bytes(f, self.value)