1
|
|
|
import enum |
|
|
|
|
2
|
|
|
import logging |
3
|
|
|
from typing import AbstractSet, Optional, Union |
4
|
|
|
|
5
|
|
|
from pocketutils.core.exceptions import XValueError |
6
|
|
|
|
7
|
|
|
logger = logging.getLogger("pocketutils") |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class DisjointEnum(enum.Enum): |
11
|
|
|
""" |
12
|
|
|
An enum that does not have combinations. |
13
|
|
|
""" |
14
|
|
|
|
15
|
|
|
@classmethod |
16
|
|
|
def _fix_lookup(cls, s: str) -> str: |
|
|
|
|
17
|
|
|
return s |
18
|
|
|
|
19
|
|
|
@classmethod |
20
|
|
|
def or_none(cls, s: Union[str, __qualname__]) -> Optional[__qualname__]: |
|
|
|
|
21
|
|
|
""" |
22
|
|
|
Returns a choice by name (or returns ``s`` itself). |
23
|
|
|
Returns ``None`` if the choice is not found. |
24
|
|
|
""" |
25
|
|
|
try: |
26
|
|
|
return cls.of(s) |
27
|
|
|
except KeyError: |
28
|
|
|
return None |
29
|
|
|
|
30
|
|
|
@classmethod |
31
|
|
|
def of(cls, s: Union[str, __qualname__]) -> __qualname__: |
|
|
|
|
32
|
|
|
""" |
33
|
|
|
Returns a choice by name (or returns ``s`` itself). |
34
|
|
|
""" |
35
|
|
|
if isinstance(s, cls): |
36
|
|
|
return s |
37
|
|
|
return cls[cls._fix_lookup(s)] |
38
|
|
|
|
39
|
|
|
def __new__(cls, *args, **kwargs): |
|
|
|
|
40
|
|
|
value = len(cls.__members__) + 1 |
41
|
|
|
obj = object.__new__(cls) |
42
|
|
|
obj._value_ = value |
43
|
|
|
return obj |
44
|
|
|
|
45
|
|
|
def __repr__(self): |
|
|
|
|
46
|
|
|
return self.name |
47
|
|
|
|
48
|
|
|
def __str__(self): |
|
|
|
|
49
|
|
|
return self.name |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
class FlagEnum(enum.Flag): |
53
|
|
|
""" |
54
|
|
|
A bit flag that behaves as a set, has a null set, and auto-sets values and names. |
55
|
|
|
|
56
|
|
|
Example: |
57
|
|
|
.. code-block:: |
58
|
|
|
|
59
|
|
|
class Flavor(FlagEnum): |
60
|
|
|
none = () |
61
|
|
|
bitter = () |
62
|
|
|
sweet = () |
63
|
|
|
sour = () |
64
|
|
|
umami = () |
65
|
|
|
bittersweet = Flavor.bitter | Flavor.sweet |
66
|
|
|
print(bittersweet.value) # 1 + 2 == 3 |
67
|
|
|
print(bittersweet.name) # "bitter|sweet" |
68
|
|
|
|
69
|
|
|
.. important:: |
70
|
|
|
The *first element* must always be the null set ("no flags") |
71
|
|
|
and should be named something like 'none', 'empty', or 'zero' |
72
|
|
|
""" |
73
|
|
|
|
74
|
|
|
@classmethod |
75
|
|
|
def _fix_lookup(cls, s: str) -> str: |
|
|
|
|
76
|
|
|
return s |
77
|
|
|
|
78
|
|
|
def __new__(cls, *args, **kwargs): |
|
|
|
|
79
|
|
|
if len(cls.__members__) == 0: |
80
|
|
|
value = 0 |
81
|
|
|
else: |
82
|
|
|
value = 2 ** (len(cls.__members__) - 1) |
83
|
|
|
obj = object.__new__(cls) |
84
|
|
|
obj._value_ = value |
85
|
|
|
return obj |
86
|
|
|
|
87
|
|
|
@classmethod |
88
|
|
|
def _create_pseudo_member_(cls, value): |
89
|
|
|
value = super()._create_pseudo_member_(value) |
90
|
|
|
members, _ = enum._decompose(cls, value) |
|
|
|
|
91
|
|
|
value._name_ = "|".join([m.name for m in members]) |
|
|
|
|
92
|
|
|
return value |
93
|
|
|
|
94
|
|
|
@classmethod |
95
|
|
|
def or_none(cls, s: Union[str, __qualname__]) -> Optional[__qualname__]: |
|
|
|
|
96
|
|
|
""" |
97
|
|
|
Returns a choice by name (or returns ``s`` itself). |
98
|
|
|
Returns ``None`` if the choice is not found. |
99
|
|
|
""" |
100
|
|
|
try: |
101
|
|
|
return cls.of(s) |
102
|
|
|
except KeyError: |
103
|
|
|
return None |
104
|
|
|
|
105
|
|
|
@classmethod |
106
|
|
|
def of(cls, s: Union[str, __qualname__, AbstractSet[Union[str, __qualname__]]]) -> __qualname__: |
|
|
|
|
107
|
|
|
""" |
108
|
|
|
Returns a choice by name (or ``s`` itself), or a set of those. |
109
|
|
|
""" |
110
|
|
|
if isinstance(s, cls): |
111
|
|
|
return s |
112
|
|
|
if isinstance(s, str): |
113
|
|
|
return cls[cls._fix_lookup_(s)] |
|
|
|
|
114
|
|
|
z = cls[0] |
|
|
|
|
115
|
|
|
for m in s: |
|
|
|
|
116
|
|
|
z |= cls.of(m) |
|
|
|
|
117
|
|
|
return z |
118
|
|
|
|
119
|
|
|
def __repr__(self): |
|
|
|
|
120
|
|
|
return self.name |
121
|
|
|
|
122
|
|
|
def __str__(self): |
|
|
|
|
123
|
|
|
return self.name |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
class TrueFalseUnknown(DisjointEnum): |
127
|
|
|
""" |
128
|
|
|
A :class:`pocketutils.core.enums.DisjointEnum` of true, false, or unknown. |
129
|
|
|
""" |
130
|
|
|
|
131
|
|
|
true = () |
132
|
|
|
false = () |
133
|
|
|
unknown = () |
134
|
|
|
|
135
|
|
|
@classmethod |
136
|
|
|
def _unmatched_type(cls) -> Optional[__qualname__]: |
137
|
|
|
return cls.unknown |
138
|
|
|
|
139
|
|
|
@classmethod |
140
|
|
|
def _fix_lookup(cls, s: str) -> str: |
141
|
|
|
s = s.lower().strip() |
142
|
|
|
return dict(t="true", false="false").get(s, s) |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
class MultiTruth(FlagEnum): |
146
|
|
|
""" |
147
|
|
|
A :class:`pocketutils.core.enums.FlagEnum` for true, false, true+false, and neither. |
148
|
|
|
""" |
149
|
|
|
|
150
|
|
|
false = () |
151
|
|
|
true = () |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
class CleverEnum(DisjointEnum): |
155
|
|
|
""" |
156
|
|
|
An enum with a ``.of`` method that finds values with limited string/value fixing. |
157
|
|
|
Replaces ``" "``, ``"-"``, and ``"."`` with ``_`` and ignores case in :meth:`of`. |
158
|
|
|
May support an "unmatched" type -- a fallback value when there is no match. |
159
|
|
|
This is similar to the simpler :class:`pocketutils.core.SmartEnum`. |
160
|
|
|
""" |
161
|
|
|
|
162
|
|
|
@classmethod |
163
|
|
|
def of(cls, s: Union[str, __qualname__]) -> __qualname__: |
164
|
|
|
try: |
165
|
|
|
return super().of(s) |
166
|
|
|
except KeyError: |
167
|
|
|
unknown = cls._unmatched_type() |
168
|
|
|
logger.error(f"Value {s} not found. Using {unknown}") |
|
|
|
|
169
|
|
|
if unknown is None: |
170
|
|
|
raise XValueError(f"Value {s} not found and unmatched_type is None") |
171
|
|
|
return unknown |
172
|
|
|
|
173
|
|
|
@classmethod |
174
|
|
|
def _unmatched_type(cls) -> Optional[__qualname__]: |
175
|
|
|
return None |
176
|
|
|
|
177
|
|
|
@classmethod |
178
|
|
|
def _fix_lookup(cls, s: str) -> str: |
179
|
|
|
return s.strip().replace(" ", "_").replace(".", "_").replace("-", "_").lower() |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
__all__ = ["TrueFalseUnknown", "DisjointEnum", "FlagEnum", "CleverEnum"] |
183
|
|
|
|