You've already forked hotpocket
BTHLABS-64: Support for customized environments
Co-authored-by: Tomek Wójcik <labs@tomekwojcik.pl> Co-committed-by: Tomek Wójcik <labs@tomekwojcik.pl>
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
from .accounts import AccessTokenOriginApp # noqa: F401
|
||||
from .associations import AssociationsSearchMode # noqa: F401
|
||||
from .core import NULL_UUID, App, Env # noqa: F401
|
||||
from .core import NULL_UUID, App, Env, Environment # noqa: F401
|
||||
|
||||
@@ -2,16 +2,69 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import enum
|
||||
import typing
|
||||
import uuid
|
||||
|
||||
NULL_UUID = uuid.UUID('00000000-0000-0000-0000-000000000000')
|
||||
|
||||
|
||||
class Env(enum.Enum):
|
||||
METAL = 'metal'
|
||||
DOCKER = 'docker'
|
||||
DEPLOYMENT = 'deployment'
|
||||
AIO = 'aio'
|
||||
class Environment:
|
||||
def __init__(self, name: str, value: str):
|
||||
self._name = name
|
||||
self._value = value
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return self._value
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f'<Env {self.name}: {self.value}>'
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
def to_rpc(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
class Environments:
|
||||
METAL = Environment('METAL', 'metal')
|
||||
DOCKER = Environment('DOCKER', 'docker')
|
||||
DEPLOYMENT = Environment('DEPLOYMENT', 'deployment')
|
||||
AIO = Environment('AIO', 'aio')
|
||||
|
||||
def _current_envs(self) -> typing.Generator[Environment, None, None]:
|
||||
for attribute in dir(self):
|
||||
candidate = getattr(self, attribute)
|
||||
if isinstance(candidate, Environment):
|
||||
yield candidate
|
||||
|
||||
def __call__(self, value: str) -> Environment:
|
||||
result: Environment | None = None
|
||||
for candidate in self._current_envs():
|
||||
if candidate.value == value:
|
||||
result = candidate
|
||||
break
|
||||
|
||||
if result is None:
|
||||
raise ValueError(f'Could not resolve env: `{value}`')
|
||||
|
||||
return result
|
||||
|
||||
def choices(self) -> list[tuple[str, str]]:
|
||||
result = []
|
||||
|
||||
for env in self._current_envs():
|
||||
result.append((env.value, env.name))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
Env = Environments()
|
||||
|
||||
|
||||
class App(enum.Enum):
|
||||
|
||||
Reference in New Issue
Block a user