Commits

Mikhail Korobov  committed ba2c1e8

better representation for more psd-tools objects

  • Participants
  • Parent commits 7903dde

Comments (0)

Files changed (4)

File src/psd_tools/decoder/actions.py

 """
 from __future__ import absolute_import, unicode_literals
 
-import collections
-
 from psd_tools.utils import read_unicode_string, read_fmt
 from psd_tools.constants import OSType, ReferenceOSType, UnitFloatType
+from psd_tools.debug import pretty_namedtuple
+from psd_tools.utils import trimmed_repr
 
-Descriptor = collections.namedtuple('Descriptor', 'name classID items')
-Reference = collections.namedtuple('Descriptor', 'items')
-Property = collections.namedtuple('Property', 'name classID keyID')
-UnitFloat = collections.namedtuple('UnitFloat', 'unit value')
-Double = collections.namedtuple('Double', 'value')
-Class = collections.namedtuple('Class', 'name classID')
-String = collections.namedtuple('String', 'value')
-EnumReference = collections.namedtuple('String', 'name classID typeID enum')
-Boolean = collections.namedtuple('Boolean', 'value')
-Offset = collections.namedtuple('Offset', 'name classID value')
-Alias = collections.namedtuple('Alias', 'value')
-List = collections.namedtuple('List', 'items')
-Integer = collections.namedtuple('Integer', 'value')
-Enum = collections.namedtuple('Enum', 'type enum')
-EngineData = collections.namedtuple('EngineData', 'value')
+Descriptor = pretty_namedtuple('Descriptor', 'name classID items')
+Reference = pretty_namedtuple('Descriptor', 'items')
+Property = pretty_namedtuple('Property', 'name classID keyID')
+UnitFloat = pretty_namedtuple('UnitFloat', 'unit value')
+Double = pretty_namedtuple('Double', 'value')
+Class = pretty_namedtuple('Class', 'name classID')
+String = pretty_namedtuple('String', 'value')
+EnumReference = pretty_namedtuple('String', 'name classID typeID enum')
+Boolean = pretty_namedtuple('Boolean', 'value')
+Offset = pretty_namedtuple('Offset', 'name classID value')
+Alias = pretty_namedtuple('Alias', 'value')
+List = pretty_namedtuple('List', 'items')
+Integer = pretty_namedtuple('Integer', 'value')
+Enum = pretty_namedtuple('Enum', 'type enum')
+_EngineData = pretty_namedtuple('EngineData', 'value')
+
+class EngineData(_EngineData):
+    def __repr__(self):
+        return "EngineData(value=%s)" % trimmed_repr(self.value)
+
+    def _repr_pretty_(self, p, cycle):
+        if cycle:
+            p.text("EngineData(...)")
+        else:
+            with p.group(1, "EngineData(", ")"):
+                p.breakable()
+                p.text("value=")
+                if isinstance(self.value, bytes):
+                    p.text(trimmed_repr(self.value))
+                else:
+                    p.pretty(self.value)
 
 
 def get_ostype(ostype):

File src/psd_tools/decoder/tagged_blocks.py

 from psd_tools.utils import read_fmt, read_unicode_string, unpack
 from psd_tools.decoder import decoders
 from psd_tools.reader.layers import Block
+from psd_tools.debug import pretty_namedtuple
 
 _tagged_block_decoders, register = decoders.new_registry()
 
 })
 
 
-SolidColorSettings = collections.namedtuple('SolidColorSettings', 'version data')
-MetadataItem = collections.namedtuple('MetadataItem', 'sig key copy_on_sheet_duplication data')
-ProtectedSetting = collections.namedtuple('ProtectedSetting', 'transparency, composite, position')
-TypeToolObjectSetting = collections.namedtuple('TypeToolObjectSetting',
+SolidColorSettings = pretty_namedtuple('SolidColorSettings', 'version data')
+MetadataItem = pretty_namedtuple('MetadataItem', 'sig key copy_on_sheet_duplication data')
+ProtectedSetting = pretty_namedtuple('ProtectedSetting', 'transparency, composite, position')
+TypeToolObjectSetting = pretty_namedtuple('TypeToolObjectSetting',
                         'version xx xy yx yy tx ty text_version descriptor_version1 text_data')
                         #'warp_version descriptor_version2 warp_data'
                         #'left top right bottom')
     def __repr__(self):
         return "Divider(%r %s, %s)" % (self.type, SectionDivider.name_of(self.type), self.key)
 
+
 def decode(tagged_blocks):
     """
     Replaces "data" attribute of a blocks from ``tagged_blocks`` list

File src/psd_tools/reader/layers.py

 _Block = pretty_namedtuple('Block', 'key data')
 GlobalMaskInfo = pretty_namedtuple('GlobalMaskInfo', 'overlay color_components opacity kind')
 
+
 class MaskData(_MaskData):
 
     def width(self):
         if cycle:
             p.text('Block(...)')
         else:
-            p.text(repr(self))
+            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(fp, encoding, depth):

File src/psd_tools/user_api/pil_support.py

     """
     header = decoded_data.header
     size = header.width, header.height
-
     if header.color_mode == ColorMode.RGB:
 
         if header.number_of_channels == 3:
         a = a.point(lambda i: i*opacity_scale)
         return Image.merge('RGBA', [r, g, b, a])
     else:
-        raise NotImplementedError()
+        raise NotImplementedError()