def fetch_local_or_mixed(ctx: Context, item_type: str, item_id: PublicId) -> None: """ Fetch item, either local or mixed, depending on the parameters/context configuration. It will first try to fetch from local registry; in case of failure, if the 'is_mixed' flag is set, it will try to fetch from remote registry. Context expects 'is_local' and 'is_mixed' to be set. :param ctx: the CLI context. :param item_type: the type of the package. :param item_id: the public id of the item. :return: None """ def _raise(item_type_: str, item_id_: PublicId, exception): """Temporary function to raise exception (used below twice).""" raise click.ClickException( f"Failed to add {item_type_} dependency {item_id_}: {str(exception)}" ) try: add_item(ctx, item_type, item_id) except click.ClickException as e: if not ctx.config.get("is_mixed", False): _raise(item_type, item_id, e) ctx.set_config("is_local", False) try: add_item(ctx, item_type, item_id) except click.ClickException as e: _raise(item_type, item_id, e) ctx.set_config("is_local", True)
def fetch_agent(ctx: Context, public_id: PublicId, alias: Optional[str] = None) -> None: """ Fetch Agent from Registry. :param ctx: Context :param public_id: str public ID of desirable Agent. :param ctx: a Context object. :param alias: an optional alias. :return: None """ author, name, version = public_id.author, public_id.name, public_id.version api_path = "/agents/{}/{}/{}".format(author, name, version) resp = request_api("GET", api_path) file_url = resp["file"] filepath = download_file(file_url, ctx.cwd) folder_name = name if alias is None else alias aea_folder = os.path.join(ctx.cwd, folder_name) ctx.clean_paths.append(aea_folder) extract(filepath, ctx.cwd) if alias is not None: os.rename(name, alias) ctx.cwd = aea_folder try_to_load_agent_config(ctx) if alias is not None: ctx.agent_config.agent_name = alias ctx.agent_loader.dump( ctx.agent_config, open(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w")) click.echo("Fetching dependencies...") for item_type in ("connection", "contract", "skill", "protocol"): item_type_plural = item_type + "s" # initialize fetched agent with empty folders for custom packages custom_items_folder = os.path.join(ctx.cwd, item_type_plural) os.makedirs(custom_items_folder) config = getattr(ctx.agent_config, item_type_plural) for item_public_id in config: try: add_item(ctx, item_type, item_public_id) except Exception as e: raise click.ClickException( 'Unable to fetch dependency for agent "{}", aborting. {}'. format(name, e)) click.echo("Dependencies successfully fetched.") click.echo("Agent {} successfully fetched to {}.".format(name, aea_folder))
def _fetch_agent_deps(ctx: Context) -> None: """ Fetch agent dependencies. :param ctx: context object. :return: None :raises: ClickException re-raises if occurs in add_item call. """ for item_type in (PROTOCOL, CONTRACT, CONNECTION, SKILL): item_type_plural = "{}s".format(item_type) required_items = getattr(ctx.agent_config, item_type_plural) for item_id in required_items: add_item(ctx, item_type, item_id)
def _fetch_agent_deps(ctx: Context) -> None: """ Fetch agent dependencies. :param ctx: context object. :return: None :raises: ClickException re-raises if occures in add_item call. """ ctx.set_config("is_local", True) for item_type in ("protocol", "contract", "connection", "skill"): item_type_plural = "{}s".format(item_type) required_items = getattr(ctx.agent_config, item_type_plural) for item_id in required_items: try: add_item(ctx, item_type, item_id) except click.ClickException as e: raise click.ClickException( "Failed to add {} dependency {}: {}".format( item_type, item_id, str(e)))
def create_aea( ctx: Context, agent_name: str, local: bool, author: Optional[str] = None, empty: bool = False, ) -> None: """ Create AEA project. :param ctx: Context object. :param local: boolean flag for local folder usage. :param agent_name: agent name. :param author: optional author name (valid with local=True only). :param empty: optional boolean flag for skip adding default dependencies. :return: None :raises: ClickException if an error occured. """ try: _check_is_parent_folders_are_aea_projects_recursively() except Exception: raise click.ClickException( "The current folder is already an AEA project. Please move to the parent folder." ) if author is not None: if local: do_init(author, False, False) else: raise click.ClickException( "Author is not set up. Please use 'aea init' to initialize.") config = get_or_create_cli_config() set_author = config.get(AUTHOR_KEY, None) if set_author is None: raise click.ClickException( "The AEA configurations are not initialized. Uses `aea init` before continuing or provide optional argument `--author`." ) if Path(agent_name).exists(): raise click.ClickException("Directory already exist. Aborting...") click.echo("Initializing AEA project '{}'".format(agent_name)) click.echo("Creating project directory './{}'".format(agent_name)) path = Path(agent_name) ctx.clean_paths.append(str(path)) # we have already checked that the directory does not exist. path.mkdir(exist_ok=False) try: # set up packages directories. _setup_package_folder(Path(agent_name, "protocols")) _setup_package_folder(Path(agent_name, "contracts")) _setup_package_folder(Path(agent_name, "connections")) _setup_package_folder(Path(agent_name, "skills")) # set up a vendor directory Path(agent_name, "vendor").mkdir(exist_ok=False) Path(agent_name, "vendor", "__init__.py").touch(exist_ok=False) # create a config file inside it click.echo("Creating config file {}".format(DEFAULT_AEA_CONFIG_FILE)) agent_config = _crete_agent_config(ctx, agent_name, set_author) # next commands must be done from the agent's directory -> overwrite ctx.cwd ctx.agent_config = agent_config ctx.cwd = agent_config.agent_name if not empty: click.echo("Adding default packages ...") if local: ctx.set_config("is_local", True) add_item(ctx, "connection", DEFAULT_CONNECTION) add_item(ctx, "skill", DEFAULT_SKILL) except Exception as e: raise click.ClickException(str(e))
def fetch_agent( ctx: Context, public_id: PublicId, alias: Optional[str] = None, target_dir: Optional[str] = None, ) -> None: """ Fetch Agent from Registry. :param ctx: Context :param public_id: str public ID of desirable agent. :param alias: an optional alias. :param target_dir: the target directory to which the agent is fetched. :return: None """ author, name, version = public_id.author, public_id.name, public_id.version folder_name = target_dir or (name if alias is None else alias) aea_folder = os.path.join(ctx.cwd, folder_name) if os.path.exists(aea_folder): raise ClickException( f'Item "{folder_name}" already exists in target folder.') ctx.clean_paths.append(aea_folder) api_path = f"/agents/{author}/{name}/{version}" resp = request_api("GET", api_path) file_url = resp["file"] filepath = download_file(file_url, ctx.cwd) extract(filepath, ctx.cwd) if alias or target_dir: shutil.move( os.path.join(ctx.cwd, name), aea_folder, ) ctx.cwd = aea_folder try_to_load_agent_config(ctx) if alias is not None: ctx.agent_config.agent_name = alias with open(os.path.join(ctx.cwd, DEFAULT_AEA_CONFIG_FILE), "w") as fp: ctx.agent_loader.dump(ctx.agent_config, fp) click.echo("Fetching dependencies...") for item_type in (CONNECTION, CONTRACT, SKILL, PROTOCOL): item_type_plural = item_type + "s" # initialize fetched agent with empty folders for custom packages custom_items_folder = os.path.join(ctx.cwd, item_type_plural) os.makedirs(custom_items_folder) config = getattr(ctx.agent_config, item_type_plural) for item_public_id in config: try: add_item(ctx, item_type, item_public_id) except Exception as e: raise click.ClickException( f'Unable to fetch dependency for agent "{name}", aborting. {e}' ) click.echo("Dependencies successfully fetched.") click.echo(f"Agent {name} successfully fetched to {aea_folder}.")
def add_item(self) -> None: """Add new package version to agent.""" add_item(self.ctx, str(self.item_type), self.item_public_id)