def test_load_env_from_file(env_inst: Env): env_inst.set_env("TEST", "TEST") env_inst.save() env_inst.reset() assert not env_inst.env env_inst.load_env_from_file() assert env_inst.env
def set_env(cwas_workspace, annotation_dir): env = Env() env.set_env("CWAS_WORKSPACE", cwas_workspace) env.set_env("VEP", "VEP") env.set_env("ANNOTATION_DATA", annotation_dir) env.set_env("ANNOTATION_BW_KEY", annotation_dir / "bw_key.yaml") env.set_env("MERGED_BED", cwas_workspace / "merged.bed.gz") env.set_env("MERGED_BED_INDEX", cwas_workspace / "merged.bed.gz.tbi") env.save()
def test_load_env_to_os(env_inst: Env): env_key = "CWAS_TEST" env_value = "HELLO" env_inst.set_env(env_key, env_value) env_inst.save() env_inst.load_env_to_os() assert os.getenv(env_key) == env_value env_inst.get_path().unlink() os.environ.pop(env_key)
def test_env_save(env_inst: Env): env_key = "TEST" expected = "Hello!" env_inst.set_env(env_key, expected) env_inst.save() cwas_env_path = env_inst.get_path() with cwas_env_path.open() as env_file: env_line = env_file.read() env_line = env_line.strip() assert env_line == "TEST=Hello!" env_inst.get_path().unlink()
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
def set_env(cwas_workspace: Path, annotation_dir: Path): env = Env() env.set_env("CWAS_WORKSPACE", cwas_workspace) env.set_env("ANNOTATION_DATA", annotation_dir) env.set_env("ANNOTATION_BED_KEY", cwas_workspace / "bed_key.yaml") env.save()
def set_env(cwas_workspace: Path): env = Env() env.set_env("CWAS_WORKSPACE", cwas_workspace) env.save()
def _make_env_empty(): env = Env() env.reset() env.save()