1
1
from abc import ABC , abstractmethod
2
- from typing import Any , Optional , Sequence , Tuple
2
+ from reprlib import recursive_repr
3
+ from typing import Any , Generic , Optional , Sequence , Tuple , TypeVar
3
4
4
5
from . import DeprecationHelper as dh
5
6
14
15
"Value" ,
15
16
)
16
17
18
+ T = TypeVar ("T" )
19
+
17
20
18
21
class Base (ABC ):
19
22
"""
@@ -39,7 +42,7 @@ def __init__(
39
42
raise NotImplementedError
40
43
41
44
42
- class Value (Base ):
45
+ class Value (Base , Generic [ T ] ):
43
46
"""
44
47
The abstract base class for all options that store a value.
45
48
@@ -52,22 +55,22 @@ class Value(Base):
52
55
53
56
IsHidden: If the option is hidden from the options menu.
54
57
"""
55
- CurrentValue : Any
56
- StartingValue : Any
58
+ CurrentValue : T
59
+ StartingValue : T
57
60
58
61
@abstractmethod
59
62
def __init__ (
60
63
self ,
61
64
Caption : str ,
62
65
Description : str ,
63
- StartingValue : Any ,
66
+ StartingValue : T ,
64
67
* ,
65
68
IsHidden : bool = True
66
69
) -> None :
67
70
raise NotImplementedError
68
71
69
72
70
- class Hidden (Value ):
73
+ class Hidden (Value [ T ] ):
71
74
"""
72
75
A hidden option that never displays in the menu but stores an arbitrary (json serializable)
73
76
value to the settings file.
@@ -81,11 +84,12 @@ class Hidden(Value):
81
84
StartingValue: The default value of the option.
82
85
IsHidden: If the option is hidden from the options menu. This is forced to True.
83
86
"""
87
+
84
88
def __init__ (
85
89
self ,
86
90
Caption : str ,
87
91
Description : str = "" ,
88
- StartingValue : Any = None ,
92
+ StartingValue : T = None , # type: ignore
89
93
* ,
90
94
IsHidden : bool = True
91
95
) -> None :
@@ -116,8 +120,18 @@ def IsHidden(self) -> bool: # type: ignore
116
120
def IsHidden (self , val : bool ) -> None :
117
121
pass
118
122
123
+ @recursive_repr ()
124
+ def __repr__ (self ) -> str :
125
+ return (
126
+ f"Hidden("
127
+ f"Caption={ repr (self .Caption )} ,"
128
+ f"Description={ repr (self .Description )} ,"
129
+ f"*,IsHidden={ repr (self .IsHidden )} "
130
+ f")"
131
+ )
119
132
120
- class Slider (Value ):
133
+
134
+ class Slider (Value [int ]):
121
135
"""
122
136
An option which allows users to select a value along a slider.
123
137
@@ -176,8 +190,23 @@ def __init__(
176
190
self .Increment = Increment
177
191
self .IsHidden = IsHidden
178
192
179
-
180
- class Spinner (Value ):
193
+ @recursive_repr ()
194
+ def __repr__ (self ) -> str :
195
+ return (
196
+ f"Slider("
197
+ f"Caption={ repr (self .Caption )} ,"
198
+ f"Description={ repr (self .Description )} ,"
199
+ f"CurrentValue={ repr (self .CurrentValue )} ,"
200
+ f"StartingValue={ repr (self .StartingValue )} ,"
201
+ f"MinValue={ repr (self .MinValue )} ,"
202
+ f"MaxValue={ repr (self .MaxValue )} ,"
203
+ f"Increment={ repr (self .Increment )} ,"
204
+ f"*,IsHidden={ repr (self .IsHidden )} "
205
+ f")"
206
+ )
207
+
208
+
209
+ class Spinner (Value [str ]):
181
210
"""
182
211
An option which allows users to select one value from a sequence of strings.
183
212
@@ -242,8 +271,21 @@ def __init__(
242
271
f"Provided starting value '{ self .StartingValue } ' is not in the list of choices."
243
272
)
244
273
245
-
246
- class Boolean (Spinner ):
274
+ @recursive_repr ()
275
+ def __repr__ (self ) -> str :
276
+ return (
277
+ f"Spinner("
278
+ f"Caption={ repr (self .Caption )} ,"
279
+ f"Description={ repr (self .Description )} ,"
280
+ f"CurrentValue={ repr (self .CurrentValue )} ,"
281
+ f"StartingValue={ repr (self .StartingValue )} ,"
282
+ f"Choices={ repr (self .Choices )} ,"
283
+ f"*,IsHidden={ repr (self .IsHidden )} "
284
+ f")"
285
+ )
286
+
287
+
288
+ class Boolean (Spinner , Value [bool ]):
247
289
"""
248
290
A special form of a spinner, with two options representing boolean values.
249
291
@@ -309,6 +351,19 @@ def CurrentValue(self, val: Any) -> None:
309
351
else :
310
352
self ._current_value = bool (val )
311
353
354
+ @recursive_repr ()
355
+ def __repr__ (self ) -> str :
356
+ return (
357
+ f"Boolean("
358
+ f"Caption={ repr (self .Caption )} ,"
359
+ f"Description={ repr (self .Description )} ,"
360
+ f"CurrentValue={ repr (self .CurrentValue )} ,"
361
+ f"StartingValue={ repr (self .StartingValue )} ,"
362
+ f"Choices={ repr (self .Choices )} ,"
363
+ f"*,IsHidden={ repr (self .IsHidden )} "
364
+ f")"
365
+ )
366
+
312
367
313
368
class Field (Base ):
314
369
"""
@@ -320,33 +375,11 @@ class Field(Base):
320
375
IsHidden: If the field is hidden from the options menu.
321
376
"""
322
377
323
- def __init__ (
324
- self ,
325
- Caption : str ,
326
- Description : str = "" ,
327
- * ,
328
- IsHidden : bool = False
329
- ) -> None :
330
- """
331
- Creates the option.
332
-
333
- Args:
334
- Caption: The name of the option.
335
- Description: A short description of the option to show when hovering over it in the menu.
336
- IsHidden (keyword only): If the value is hidden from the options menu.
337
- """
338
- self .Caption = Caption
339
- self .Description = Description
340
- self .IsHidden = IsHidden
341
-
342
378
343
379
class Nested (Field ):
344
380
"""
345
381
A field which when clicked opens up a nested menu with more options.
346
382
347
- These are distinguished from normal fields by having the "new" exclaimation mark to the side of
348
- it, but you should probably still give it a meaningful description.
349
-
350
383
Note that these fields will be disabled if all child options are either hidden or other disabled
351
384
nested fields.
352
385
@@ -383,3 +416,14 @@ def __init__(
383
416
self .Description = Description
384
417
self .Children = Children
385
418
self .IsHidden = IsHidden
419
+
420
+ @recursive_repr ()
421
+ def __repr__ (self ) -> str :
422
+ return (
423
+ f"Nested("
424
+ f"Caption={ repr (self .Caption )} ,"
425
+ f"Description={ repr (self .Description )} ,"
426
+ f"Children={ repr (self .Children )} ,"
427
+ f"*,IsHidden={ repr (self .IsHidden )} "
428
+ f")"
429
+ )
0 commit comments