コード例 #1
0
 def _repr_pretty_(self, p, cycle):
     if cycle:
         return self.__repr__()
     if isinstance(self.value, bytes):
         p.text(trimmed_repr(self.value))
     else:
         p.pretty(self.value)
コード例 #2
0
    def _repr_pretty_(self, p, cycle):
        if cycle:
            return "[...]".format(name=self.__class__.__name__)

        with p.group(2, '['.format(name=self.__class__.__name__), ']'):
            p.breakable('')
            for idx in range(len(self._items)):
                if idx:
                    p.text(',')
                    p.breakable()
                value = self._items[idx]
                if isinstance(value, bytes):
                    value = trimmed_repr(value)
                p.pretty(value)
            p.breakable('')
コード例 #3
0
    def _repr_pretty_(self, p, cycle):
        if cycle:
            return "{{...}".format(name=self.__class__.__name__)

        with p.group(2, '{{'.format(name=self.__class__.__name__), '}'):
            p.breakable('')
            for idx, key in enumerate(self._items):
                if idx:
                    p.text(',')
                    p.breakable()
                value = self._items[key]
                if hasattr(key, 'name'):
                    p.text(key.name)
                else:
                    p.pretty(key)
                p.text(': ')
                if isinstance(value, bytes):
                    value = trimmed_repr(value)
                p.pretty(value)
            p.breakable('')
コード例 #4
0
    def _repr_pretty_(self, p, cycle):
        if cycle:
            return "{name}(...)".format(name=self.__class__.__name__)

        with p.group(2, '{name}('.format(name=self.__class__.__name__), ')'):
            p.breakable('')
            fields = [f for f in attr.fields(self.__class__) if f.repr]
            for idx, field in enumerate(fields):
                if idx:
                    p.text(',')
                    p.breakable()
                p.text('{field}='.format(field=field.name))
                value = getattr(self, field.name)
                if isinstance(value, bytes):
                    p.text(trimmed_repr(value))
                elif isinstance(value, Enum):
                    p.text(value.name)
                else:
                    p.pretty(value)
            p.breakable('')
コード例 #5
0
ファイル: tagged_blocks.py プロジェクト: crazyrex/psd-tools2
    def read(cls, fp, version=1, padding=1):
        """Read the element from a file-like object.

        :param fp: file-like object
        :param version: psd file version
        :rtype: :py:class:`.TaggedBlock`
        """
        signature = read_fmt('4s', fp)[0]
        if signature not in cls._SIGNATURES:
            logger.warning('Invalid signature (%r)' % (signature))
            fp.seek(-4, 1)
            return None

        key = read_fmt('4s', fp)[0]
        try:
            key = TaggedBlockID(key)
        except ValueError:
            message = 'Unknown key: %r' % (key)
            warn(message)
            logger.warning(message)

        fmt = cls._length_format(key, version)
        raw_data = read_length_block(fp, fmt=fmt, padding=padding)
        kls = TYPES.get(key)
        if kls:
            data = kls.frombytes(raw_data, version=version)
            # _raw_data = data.tobytes(version=version,
            #                          padding=1 if padding == 4 else 4)
            # assert raw_data == _raw_data, '%r: %s vs %s' % (
            #     kls, trimmed_repr(raw_data), trimmed_repr(_raw_data)
            # )
        else:
            message = 'Unknown tagged block: %r, %s' % (key,
                                                        trimmed_repr(raw_data))
            warn(message)
            logger.warning(message)
            data = raw_data
        return cls(signature, key, data)
コード例 #6
0
ファイル: utils.py プロジェクト: crazyrex/psd-tools2
def check_read_write(cls, data, *args, **kwargs):
    element = cls.frombytes(data, *args, **kwargs)
    new_data = element.tobytes(*args, **kwargs)
    assert data == new_data, '%s vs %s' % (trimmed_repr(data),
                                           trimmed_repr(new_data))