def _load_state(self, local: bool) -> None: """ Load saved state from file. :param local: bool is local project and agents re-creation. :return: None :raises: ValueError if failed to load state. """ if not os.path.exists(self._save_path): return save_json = {} with open(self._save_path) as f: save_json = json.load(f) if not save_json: return # pragma: nocover try: for public_id in save_json["projects"]: self.add_project( PublicId.from_str(public_id), local=local, restore=True ) for agent_settings in save_json["agents"]: self.add_agent_with_config( public_id=PublicId.from_str(agent_settings["public_id"]), agent_name=agent_settings["agent_name"], config=agent_settings["config"], ) except ValueError as e: # pragma: nocover raise ValueError(f"Failed to load state. {e}")
def _load_state(self, local: bool, remote: bool) -> None: """ Load saved state from file. Fetch agent project and all dependencies to working_dir. If local = False and remote = False, then the packages are fetched in mixed mode (i.e. first try from local registry, and then from remote registry in case of failure). :param local: whether or not to fetch from local registry. :param remote: whether or not to fetch from remote registry. :return: None :raises: ValueError if failed to load state. """ if not os.path.exists(self._save_path): return save_json = {} with open_file(self._save_path) as f: save_json = json.load(f) if not save_json: return # pragma: nocover try: for public_id in save_json["projects"]: self.add_project( PublicId.from_str(public_id), local=local, remote=remote, restore=True, ) for agent_settings in save_json["agents"]: self.add_agent_with_config( public_id=PublicId.from_str(agent_settings["public_id"]), agent_name=agent_settings["agent_name"], config=agent_settings["config"], ) except ValueError as e: # pragma: nocover raise ValueError(f"Failed to load state. {e}")
def get_package_latest_public_id(package_id: PackageId) -> PublicId: """ Get package latest package id from the remote repo. :param package_id: id of the package to check :return: package id of the latest package in remote repo """ package_meta = get_package_meta(str(package_id.package_type), package_id.public_id.to_latest()) return PublicId.from_str(package_meta["public_id"])
def split_component_id_and_config( component_index: int, component_configuration_json: Dict) -> ComponentId: """ Split component id and configuration. :param component_index: the position of the component configuration in the agent config file.. :param component_configuration_json: the JSON object to process. :return: the component id and the configuration object. :raises ValueError: if the component id cannot be extracted. """ # author, name, version, type are mandatory fields missing_fields = {"public_id", "type" }.difference(component_configuration_json.keys()) if len(missing_fields) > 0: raise ValueError( f"There are missing fields in component id {component_index + 1}: {missing_fields}." ) public_id_str = component_configuration_json.pop("public_id") component_type = ComponentType( component_configuration_json.pop("type")) component_public_id = PublicId.from_str(public_id_str) component_id = ComponentId(component_type, component_public_id) return component_id