Coverage for /home/ubuntu/openexr-tools/python/openexr_tools/enum.py: 100%
38 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-08 15:56 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-08 15:56 +0000
1from enum import Enum
3from lunchbox.enforce import Enforce
4# ------------------------------------------------------------------------------
7'''
8The enum module contains enum classes for working with EXR image codes.
9'''
12class ImageCodec(Enum):
13 '''
14 Legal EXR image codecs.
16 Includes:
18 * B44
19 * B44A
20 * DWAA
21 * DWAB
22 * PIZ
23 * PXR24
24 * RLE
25 * UNCOMPRESSED
26 * ZIP
27 * ZIPS
28 '''
29 B44 = ('b44', 6) # Imath.Compression.B44_COMPRESSION
30 B44A = ('b44a', 7) # Imath.Compression.B44A_COMPRESSION
31 DWAA = ('dwaa', 8) # Imath.Compression.DWAA_COMPRESSION
32 DWAB = ('dwab', 9) # Imath.Compression.DWAB_COMPRESSION
33 PIZ = ('piz', 4) # Imath.Compression.PIZ_COMPRESSION
34 PXR24 = ('pxr24', 5) # Imath.Compression.PXR24_COMPRESSION
35 RLE = ('rle', 1) # Imath.Compression.RLE_COMPRESSION
36 UNCOMPRESSED = ('uncompressed', 0) # Imath.Compression.NO_COMPRESSION
37 ZIP = ('zip', 3) # Imath.Compression.ZIP_COMPRESSION
38 ZIPS = ('zips', 2) # Imath.Compression.ZIPS_COMPRESSION
40 def __init__(self, string, exr_code):
41 # type: (str, int) -> None
42 '''
43 Args:
44 string (str): String representation of codec.
45 exr_code (int): EXR compression code.
46 '''
47 self.string = string # type: ignore
48 self.exr_code = exr_code
50 def __repr__(self):
51 # type: () -> str
52 return f'''
53<ImageCodec.{self.name.upper()}>
54 string: {self.string}
55exr_code: {self.exr_code}'''[1:]
57 @staticmethod
58 def from_exr_code(code):
59 # type: (int) -> ImageCodec
60 '''
61 Constructs a ImageCodec instance from a given EXR code.
63 Args:
64 code (int): EXR compression code.
66 Raises:
67 EnforceError: If value given is not an integer.
68 EnforceError: If no ImageCodec type can be found for given code.
70 Returns:
71 ImageCodec: ImageCodec instance.
72 '''
73 msg = 'Value given is not an integer. {a} != {b}.'
74 Enforce(code, 'instance of', int, message=msg)
76 lut = {x.exr_code: x for x in ImageCodec.__members__.values()}
78 msg = 'EXR code {a} has no legal ImageCodec type. '
79 msg += f'Legal EXR codes: {sorted(lut.keys())}.'
80 Enforce(code, 'in', lut.keys(), message=msg)
82 return lut[code]
84 @staticmethod
85 def from_string(string):
86 # type: (str) -> ImageCodec
87 '''
88 Constructs a ImageCodec instance from a given string.
90 Args:
91 string (int): ImageCodec string.
93 Raises:
94 EnforceError: If value given is not a string.
95 EnforceError: If no ImageCodec type can be found for given string.
97 Returns:
98 ImageCodec: ImageCodec instance.
99 '''
100 msg = 'Value given is not a string. {a} != {b}.'
101 Enforce(string, 'instance of', str, message=msg)
103 lut = {x.string: x for x in ImageCodec.__members__.values()}
105 string = string.lower()
106 msg = '"{a}" has no legal ImageCodec type. '
107 msg += f'Legal codec strings: {sorted(lut.keys())}.'
108 Enforce(string, 'in', lut.keys(), message=msg)
110 return lut[string]