|
1 | 1 | """Server roles for the docbuild application.""" |
2 | 2 |
|
3 | 3 | from enum import StrEnum |
4 | | - |
5 | | -# from typing import Literal, Type |
6 | | -# from pydantic import BaseModel, field_validator, Field |
7 | | -# from ...constants import SERVER_ROLES |
8 | | -# |
9 | | -# ServerRole = StrEnum( |
10 | | -# "ServerRole", |
11 | | -# # Allow lowercase and uppercase names |
12 | | -# {name: name for name in SERVER_ROLES} |
13 | | -# | {name.upper(): name for name in SERVER_ROLES}, |
14 | | -# ) |
| 4 | +from typing import Self |
15 | 5 |
|
16 | 6 |
|
17 | 7 | class ServerRole(StrEnum): |
18 | | - """The server role.""" |
| 8 | + """The server role. |
| 9 | +
|
| 10 | + This Enum supports various aliases and case variations for each role. |
| 11 | + """ |
19 | 12 |
|
20 | | - # production |
| 13 | + # Primary Members |
21 | 14 | PRODUCTION = "production" |
22 | | - """Server is in production mode, serving live traffic.""" |
23 | | - PROD = PRODUCTION |
24 | | - P = PRODUCTION |
25 | | - production = PRODUCTION |
26 | | - prod = PRODUCTION |
27 | | - p = PRODUCTION |
28 | | - # staging |
29 | 15 | STAGING = "staging" |
30 | | - """Server is in staging mode, used for testing before production.""" |
31 | | - STAGE = STAGING |
32 | | - S = STAGING |
33 | | - staging = STAGING |
34 | | - stage = STAGING |
35 | | - s = STAGING |
36 | | - # testing |
37 | 16 | TESTING = "testing" |
38 | | - """Server is in testing mode, used for development and QA.""" |
39 | | - TEST = TESTING |
40 | | - T = TESTING |
41 | | - testing = TESTING |
42 | | - test = TESTING |
43 | | - t = TESTING |
| 17 | + |
| 18 | + # Aliases for PRODUCTION |
| 19 | + PROD = "production" |
| 20 | + P = "production" |
| 21 | + prod = "production" |
| 22 | + p = "production" |
| 23 | + |
| 24 | + # Aliases for STAGING |
| 25 | + STAGE = "staging" |
| 26 | + S = "staging" |
| 27 | + stage = "staging" |
| 28 | + s = "staging" |
| 29 | + |
| 30 | + # Aliases for TESTING |
| 31 | + TEST = "testing" |
| 32 | + T = "testing" |
| 33 | + test = "testing" |
| 34 | + t = "testing" |
| 35 | + DEVEL = "testing" |
| 36 | + devel = "testing" |
| 37 | + DEV = "testing" |
| 38 | + dev = "testing" |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def _missing_(cls: type[Self], value: object) -> "ServerRole | None": |
| 42 | + """Handle aliases and case-insensitive lookups using class members. |
| 43 | +
|
| 44 | + If the value passed isn't a valid value (for example, 'production'), |
| 45 | + check if it matches one of the alias names (for example, 'p'). |
| 46 | + """ |
| 47 | + # Convert the input to a string to check against member keys |
| 48 | + name = str(value) |
| 49 | + member = cls.__members__.get(name) |
| 50 | + |
| 51 | + if member is not None: |
| 52 | + return member |
| 53 | + |
| 54 | + # If no match is found, raise ValueError with all valid names/aliases |
| 55 | + valid = ", ".join(cls.__members__.keys()) |
| 56 | + raise ValueError( |
| 57 | + f"{name!r} is not a valid {cls.__name__}; valid names/aliases: {valid}" |
| 58 | + ) |
0 commit comments