confighelper: add getchoice method
This getter provides a pattern for configuring an item that within a range of choices. Signed-off-by: Eric Callahan <arksine@gmail.com>
This commit is contained in:
parent
96b1c22e28
commit
9beecbda92
|
@ -249,6 +249,31 @@ class ConfigHelper:
|
||||||
self.config.getfloat, option, default,
|
self.config.getfloat, option, default,
|
||||||
above, below, minval, maxval, deprecate)
|
above, below, minval, maxval, deprecate)
|
||||||
|
|
||||||
|
def getchoice(
|
||||||
|
self,
|
||||||
|
option: str,
|
||||||
|
choices: Union[Dict[str, _T], List[_T]],
|
||||||
|
default_key: Union[Sentinel, str] = Sentinel.MISSING,
|
||||||
|
force_lowercase: bool = False,
|
||||||
|
deprecate: bool = False
|
||||||
|
) -> _T:
|
||||||
|
result: str = self._get_option(
|
||||||
|
self.config.get, option, default_key, deprecate=deprecate
|
||||||
|
)
|
||||||
|
if force_lowercase:
|
||||||
|
result = result.lower()
|
||||||
|
if result not in choices:
|
||||||
|
items = list(choices.keys()) if isinstance(choices, dict) else choices
|
||||||
|
raise ConfigError(
|
||||||
|
f"Section [{self.section}], Option '{option}: Value "
|
||||||
|
f"{result} is not a vailid choice. Must be one of the "
|
||||||
|
f"following {items}"
|
||||||
|
)
|
||||||
|
if isinstance(choices, dict):
|
||||||
|
return choices[result]
|
||||||
|
else:
|
||||||
|
return result # type: ignore
|
||||||
|
|
||||||
def getlists(self,
|
def getlists(self,
|
||||||
option: str,
|
option: str,
|
||||||
default: Union[Sentinel, _T] = Sentinel.MISSING,
|
default: Union[Sentinel, _T] = Sentinel.MISSING,
|
||||||
|
|
Loading…
Reference in New Issue