OpenSlides/server/openslides/utils/arguments.py

26 lines
720 B
Python
Raw Normal View History

2018-02-06 08:04:31 +01:00
from argparse import Namespace
2018-08-22 22:00:08 +02:00
from typing import Any, Optional
2018-02-06 08:04:31 +01:00
2019-01-06 16:22:33 +01:00
class OpenSlidesArguments:
2018-08-22 22:00:08 +02:00
args: Optional[Namespace] = None
2018-02-06 08:04:31 +01:00
def __getitem__(self, key: str) -> Any:
if not self.args:
raise KeyError("Arguments are not set.")
if not hasattr(self.args, key):
2019-01-12 23:01:42 +01:00
raise KeyError(f"Key '{key}' is not in the OpenSlides arguments.")
2018-02-06 08:04:31 +01:00
return getattr(self.args, key)
def get(self, key: str, default: Any) -> Any:
if not self.args or not hasattr(self.args, key):
return default
else:
return getattr(self.args, key)
def set_arguments(self, args: Namespace) -> None:
self.args = args
arguments = OpenSlidesArguments()