class ActionFactory(Action): def __init__(self, action: str, allowed_actions: dict): assert action in allowed_actions, ("unsupported action of job", action) super().__init__(action, allowed_actions[action]) suffix = action.replace('-', '_') for attr in ["define_arguments", "check_arguments", "do_action"]: if hasattr(self, f"{attr}_{suffix}"): setattr(self, attr, getattr(self, f"{attr}_{suffix}")) else: assert attr != "do_action", f"must specify a method named {attr}_{suffix} in {self.__class__.__name__}" self.__job__ = Job() self.__clusters__ = ClusterList() self.enable_svaing = dict(job=False, clusters=False) def restore(self, args): if getattr(args, 'job_name', None): self.__job__.load(job_name=args.job_name) self.__clusters__.load() return self def store(self, args): if self.enable_svaing["job"]: self.__job__.save() if self.enable_svaing["clusters"]: self.__clusters__.save() return self
class ActionFactory(Action): def __init__(self, action: str, allowed_actions: dict): assert action in allowed_actions, ("unsupported action of job", action) super().__init__(action, allowed_actions[action]) suffix = action.replace('-', '_') self.define_arguments = getattr(self, "define_arguments_" + suffix, super().define_arguments) self.check_arguments = getattr(self, "check_arguments_" + suffix, super().check_arguments) self.do_action = getattr(self, "do_action_" + suffix, None) self.__job__ = Job() self.__clusters__ = ClusterList() self.disable_saving = dict() def restore(self, args): if getattr(args, 'job_name', None): self.__job__.load(job_name=args.job_name) self.__clusters__.load() return self def store(self, args): if not self.disable_saving.get("job", False): self.__job__.save() if not self.disable_saving.get("clusters", False): self.__clusters__.save() return self