1616"""
1717# isort: STDLIB
1818from enum import Enum , IntEnum
19+ from typing import Dict , List , Optional , Union
1920
2021
2122class Level (Enum ):
@@ -27,7 +28,7 @@ class Level(Enum):
2728 WARNING = "W"
2829 INFO = "I"
2930
30- def __str__ (self ):
31+ def __str__ (self ) -> str :
3132 return self .value
3233
3334
@@ -39,22 +40,10 @@ class PoolMaintenanceErrorCode(IntEnum):
3940 NO_IPC_REQUESTS = 1
4041 NO_POOL_CHANGES = 2
4142
42- def __str__ (self ):
43+ def __str__ (self ) -> str :
4344 return f"{ Level .ERROR } M{ str (self .value ).zfill (3 )} "
4445
45- @staticmethod
46- def from_str (code_str ):
47- """
48- Discover the code, if any, from the code string.
49-
50- :returns: the code if it finds a match, otherwise None
51- :rtype: PoolMaintenanceErrorCode or NoneType
52- """
53- return next (
54- (code for code in PoolMaintenanceErrorCode if code_str == str (code )), None
55- )
56-
57- def explain (self ):
46+ def explain (self ) -> str :
5847 """
5948 Return an explanation of the error return code.
6049 """
@@ -75,7 +64,7 @@ def explain(self):
7564
7665 assert False , "impossible error code reached" # pragma: no cover
7766
78- def summarize (self ):
67+ def summarize (self ) -> str :
7968 """
8069 Return a short summary of the return code.
8170 """
@@ -95,10 +84,10 @@ class PoolAllocSpaceErrorCode(IntEnum):
9584
9685 NO_ALLOC_SPACE = 1
9786
98- def __str__ (self ):
87+ def __str__ (self ) -> str :
9988 return f"{ Level .WARNING } S{ str (self .value ).zfill (3 )} "
10089
101- def explain (self ):
90+ def explain (self ) -> str :
10291 """
10392 Return an explanation of the return code.
10493 """
@@ -111,7 +100,7 @@ def explain(self):
111100
112101 assert False , "impossible error code reached" # pragma: no cover
113102
114- def summarize (self ):
103+ def summarize (self ) -> str :
115104 """
116105 Return a short summary of the return code.
117106 """
@@ -120,18 +109,6 @@ def summarize(self):
120109
121110 assert False , "impossible error code reached" # pragma: no cover
122111
123- @staticmethod
124- def from_str (code_str ):
125- """
126- Discover the code, if any, from the code string.
127-
128- :returns: the code if it finds a match, otherwise None
129- :rtype: PoolAllocSpaceErrorCode or NoneType
130- """
131- return next (
132- (code for code in PoolAllocSpaceErrorCode if code_str == str (code )), None
133- )
134-
135112
136113class PoolDeviceSizeChangeCode (IntEnum ):
137114 """
@@ -142,7 +119,7 @@ class PoolDeviceSizeChangeCode(IntEnum):
142119 DEVICE_SIZE_INCREASED = 1
143120 DEVICE_SIZE_DECREASED = 2
144121
145- def __str__ (self ):
122+ def __str__ (self ) -> str :
146123 if self is PoolDeviceSizeChangeCode .DEVICE_SIZE_INCREASED :
147124 return f"{ Level .INFO } DS{ str (self .value ).zfill (3 )} "
148125
@@ -151,7 +128,7 @@ def __str__(self):
151128
152129 assert False , "impossible error code reached" # pragma: no cover
153130
154- def explain (self ):
131+ def explain (self ) -> str :
155132 """
156133 Return an explanation of the return code.
157134 """
@@ -169,7 +146,7 @@ def explain(self):
169146
170147 assert False , "impossible error code reached" # pragma: no cover
171148
172- def summarize (self ):
149+ def summarize (self ) -> str :
173150 """
174151 Return a short summary of the return code.
175152 """
@@ -181,59 +158,50 @@ def summarize(self):
181158
182159 assert False , "impossible error code reached" # pragma: no cover
183160
184- @staticmethod
185- def from_str (code_str ):
186- """
187- Discover the code, if any, from the code string.
188161
189- :returns: the code if it finds a match, otherwise None
190- :rtype: PoolAllocSpaceErrorCode or NoneType
191- """
192- return next (
193- (code for code in PoolDeviceSizeChangeCode if code_str == str (code )), None
194- )
162+ CLASSES = [
163+ PoolAllocSpaceErrorCode ,
164+ PoolDeviceSizeChangeCode ,
165+ PoolMaintenanceErrorCode ,
166+ ]
167+
168+ type PoolErrorCodeType = Union [
169+ PoolAllocSpaceErrorCode , PoolDeviceSizeChangeCode , PoolMaintenanceErrorCode
170+ ]
195171
196172
197173class PoolErrorCode :
198174 """
199175 Summary class for all pool error codes.
200176 """
201177
202- CLASSES = [
203- PoolMaintenanceErrorCode ,
204- PoolAllocSpaceErrorCode ,
205- PoolDeviceSizeChangeCode ,
206- ]
178+ CODE_MAP : Dict [str , PoolErrorCodeType ] = dict (
179+ (str (code ), code ) for c in CLASSES for code in list (c )
180+ )
207181
208182 @staticmethod
209- def codes ():
183+ def codes () -> List [ PoolErrorCodeType ] :
210184 """
211185 Return all pool error codes.
212186 """
213- return [ code for c in PoolErrorCode .CLASSES for code in list ( c )]
187+ return list ( PoolErrorCode .CODE_MAP . values ())
214188
215189 @staticmethod
216- def error_from_str ( error_code ) :
190+ def code_strs () -> List [ str ] :
217191 """
218- Obtain an error object from a distinguishing error string.
219-
220- :param str error_code:
221- :returns: error object
222- :raises: StopIteration if no match found
192+ Return str representations of all pool error codes.
223193 """
224- return next (
225- (
226- code
227- for code in (c .from_str (error_code ) for c in PoolErrorCode .CLASSES )
228- if code is not None
229- )
230- )
194+ return list (PoolErrorCode .CODE_MAP .keys ())
231195
232196 @staticmethod
233- def explain (error_code ):
197+ def error_from_str (
198+ error_code : str ,
199+ ) -> Optional [PoolErrorCodeType ]:
234200 """
235- Return explanation for error code, else None .
201+ Obtain an error object from a distinguishing error string .
236202
237203 :param str error_code:
204+ :returns: error object
238205 """
239- return PoolErrorCode .error_from_str (error_code ).explain ()
206+
207+ return PoolErrorCode .CODE_MAP .get (error_code )
0 commit comments