Exemplo n.º 1
0
def test_pascal_string(fixture, padding):
    with io.BytesIO() as f:
        write_pascal_string(f, fixture, padding=padding)
        data = f.getvalue()

    with io.BytesIO(data) as f:
        output = read_pascal_string(f, padding=padding)
        assert fixture == output
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
 def write(self, fp, padding=4, **kwargs):
     written = write_fmt(fp, '4sI', self.kind, self.version)
     written += write_pascal_string(fp, self.uuid, 'macroman', padding=1)
     written += write_fmt(fp, '4I', self.page, self.total_pages,
                          self.anti_alias, self.layer_type.value)
     written += write_fmt(fp, '8d', *self.transform)
     written += self.warp.write(fp, padding=1)
     written += write_padding(fp, written, padding)
     return written
Exemplo n.º 5
0
    def _write_extra(self, fp, encoding, version):
        written = 0
        if self.mask_data:
            written += self.mask_data.write(fp)
        else:
            written += write_fmt(fp, 'I', 0)

        written += self.blending_ranges.write(fp)
        written += write_pascal_string(fp, self.name, encoding, padding=4)
        written += self.tagged_blocks.write(fp, version, padding=1)
        written += write_padding(fp, written, 2)
        return written
Exemplo n.º 6
0
 def write(self, fp, **kwargs):
     written = write_fmt(fp, '2I', self.version, self.image_mode.value)
     written += write_fmt(fp, '2h', *self.point)
     written += write_unicode_string(fp, self.name)
     written += write_pascal_string(fp, self.pattern_id, encoding='ascii',
                                    padding=1)
     if self.color_table:
         for row in self.color_table:
             written += write_fmt(fp, '3B', *row)
         written += write_fmt(fp, '4x')
     written += self.data.write(fp)
     return written
Exemplo n.º 7
0
    def write(self, fp, **kwargs):
        written = write_pascal_string(fp, self.uuid, encoding='ascii', padding=1)
        written += write_fmt(fp, 'I', self.version)

        def writer(f):
            return self._write_body(f)

        written += write_length_block(fp, writer, fmt='Q')

        if self.extra is not None:
            written += self.extra.write(fp)
        return written
Exemplo n.º 8
0
    def write(self, fp, encoding='macroman'):
        written = write_fmt(fp, '4sH', self.signature,
                            getattr(self.key, 'value', self.key))
        written += write_pascal_string(fp, self.name, encoding, 2)

        def writer(f):
            if hasattr(self.data, 'write'):
                return self.data.write(f, padding=1)
            return write_bytes(f, self.data)

        written += write_length_block(fp, writer, padding=2)
        return written
Exemplo n.º 9
0
    def write(self, fp, encoding='macroman'):
        """Write the element to a file-like object.

        :param fp: file-like object
        :rtype: int
        """
        written = write_fmt(fp, '4sH', self.signature,
                            getattr(self.key, 'value', self.key))
        written += write_pascal_string(fp, self.name, encoding, 2)

        def writer(f):
            if hasattr(self.data, 'write'):
                return self.data.write(f, padding=1)
            return write_bytes(f, self.data)

        written += write_length_block(fp, writer, padding=2)
        return written
Exemplo n.º 10
0
 def write(self, fp, **kwargs):
     return write_pascal_string(fp, self.value, 'macroman', padding=1)
Exemplo n.º 11
0
 def write(self, fp, **kwargs):
     return sum(write_pascal_string(fp, item, padding=1) for item in self)
Exemplo n.º 12
0
def test_pascal_string_format(input, expected, padding):
    with io.BytesIO() as f:
        write_pascal_string(f, input, padding=padding)
        assert f.getvalue() == expected