config.py 1.1 KB
Newer Older
1 2
from __future__ import annotations
import argparse
3

4
from typing import Optional
5

6 7 8
from .misc import CrashGenError

# from crash_gen.misc import CrashGenError
9 10

# gConfig:    Optional[argparse.Namespace]
11

12
class Config:
13 14
    _config = None # type Optional[argparse.Namespace]

15
    @classmethod    
16 17 18 19 20
    def init(cls, parser: argparse.ArgumentParser):
        if cls._config is not None:
            raise CrashGenError("Config can only be initialized once")
        cls._config = parser.parse_args()
        # print(cls._config)
21 22 23 24 25 26 27 28 29 30 31

    @classmethod
    def setConfig(cls, config: argparse.Namespace):
        cls._config = config

    @classmethod
    # TODO: check items instead of exposing everything
    def getConfig(cls) -> argparse.Namespace:
        if cls._config is None:
            raise CrashGenError("invalid state")
        return cls._config
32 33

    @classmethod
34
    def clearConfig(cls):
35 36 37 38 39 40 41 42
        cls._config = None

    @classmethod
    def isSet(cls, cfgKey):
        cfg = cls.getConfig()
        if cfgKey not in cfg:
            return False
        return cfg.__getattribute__(cfgKey)