Ejemplo n.º 1
0
def test_env_singleton(cwas_env_path: Path):
    env1 = Env()
    env2 = Env()
    assert env1 is env2

    new_env_path = Path(str(cwas_env_path) + "_new")
    env3 = Env(new_env_path)
    env4 = Env()
    assert env1 is env3
    assert env1 is env4
    assert env1.get_path() is new_env_path

    env1.set_path(cwas_env_path)
Ejemplo n.º 2
0
class Runnable(ABC):
    def __init__(self, args: argparse.Namespace):
        self.env = Env()
        arg_dict = vars(args)
        for arg in arg_dict:
            arg_val = arg_dict.get(arg)
            if isinstance(arg_val, Path):
                arg_val = arg_val.resolve()
            setattr(self, arg, arg_val)

    @classmethod
    def get_instance(cls, argv: list = []) -> Runnable:
        arg_parser = cls._create_arg_parser()
        args = arg_parser.parse_args(argv)
        cls._print_args(args)
        cls._check_args_validity(args)
        return cls(args)

    def set_env_path(self, path: Path):
        self.env.set_path(path)

    def set_env(self, env_key: str, env_value: Any):
        self.env.set_env(env_key, env_value)

    def get_env(self, env_key: str) -> Optional[str]:
        return self.env.get_env(env_key)

    def save_env(self):
        self.env.save()

    @staticmethod
    def _create_arg_parser() -> argparse.ArgumentParser:
        return argparse.ArgumentParser()

    @staticmethod
    def _print_args(args: argparse.Namespace):
        pass

    @staticmethod
    def _check_args_validity(args: argparse.Namespace):
        pass

    @abstractmethod
    def run(self):
        pass