def verify_or_create_private_keys_ctx( ctx: Context, aea_project_path: Path = ROOT, exit_on_error: bool = False, ) -> None: """ Verify or create private keys with ctx provided. :param ctx: Context """ try: AgentConfigManager.verify_or_create_private_keys( aea_project_path, private_key_helper=private_key_verify_or_create, substitude_env_vars=False, ).dump_config() agent_config = AgentConfigManager.verify_or_create_private_keys( aea_project_path, private_key_helper=private_key_verify_or_create).agent_config if ctx is not None: ctx.agent_config = agent_config except ValueError as e: # pragma: nocover if exit_on_error: sys.exit(1) raise click.ClickException(str(e))
def verify_or_create_private_keys(ctx: Context) -> None: """ Verify or create private keys. :param ctx: Context """ path = Path(DEFAULT_AEA_CONFIG_FILE) agent_loader = ConfigLoader("aea-config_schema.json", AgentConfig) fp = path.open(mode="r", encoding="utf-8") aea_conf = agent_loader.load(fp) for identifier, _value in aea_conf.private_key_paths.read_all(): if identifier not in registry.supported_crypto_ids: ValueError("Unsupported identifier in private key paths.") for identifier, private_key_path in IDENTIFIER_TO_KEY_FILES.items(): config_private_key_path = aea_conf.private_key_paths.read(identifier) if config_private_key_path is None: create_private_key(identifier) aea_conf.private_key_paths.update(identifier, private_key_path) else: try: _try_validate_private_key_path(identifier, private_key_path) except FileNotFoundError: # pragma: no cover raise click.ClickException( "File {} for private key {} not found.".format( repr(private_key_path), identifier, ) ) # update aea config path = Path(DEFAULT_AEA_CONFIG_FILE) fp = path.open(mode="w", encoding="utf-8") agent_loader.dump(aea_conf, fp) ctx.agent_config = aea_conf
def try_to_load_agent_config( ctx: Context, is_exit_on_except: bool = True, agent_src_path: str = None ) -> None: """ Load agent config to a click context object. :param ctx: click command context object. :param is_exit_on_except: bool option to exit on exception (default = True). :param agent_src_path: path to an agent dir if needed to load a custom config. :return None """ if agent_src_path is None: agent_src_path = ctx.cwd try: path = Path(os.path.join(agent_src_path, DEFAULT_AEA_CONFIG_FILE)) with path.open(mode="r", encoding="utf-8") as fp: ctx.agent_config = ctx.agent_loader.load(fp) logging.config.dictConfig(ctx.agent_config.logging_config) except FileNotFoundError: if is_exit_on_except: raise click.ClickException( "Agent configuration file '{}' not found in the current directory.".format( DEFAULT_AEA_CONFIG_FILE ) ) except jsonschema.exceptions.ValidationError: if is_exit_on_except: raise click.ClickException( "Agent configuration file '{}' is invalid. Please check the documentation.".format( DEFAULT_AEA_CONFIG_FILE ) )
def verify_or_create_private_keys_ctx( ctx: Context, aea_project_path: Path = ROOT, exit_on_error: bool = True, ) -> None: """ Verify or create private keys with ctx provided. :param ctx: Context """ try: agent_config = verify_or_create_private_keys(aea_project_path, exit_on_error) if ctx is not None: ctx.agent_config = agent_config except ValueError as e: # pragma: nocover click.ClickException(str(e))
def try_to_load_agent_config( ctx: Context, is_exit_on_except: bool = True, agent_src_path: str = None ) -> None: """ Load agent config to a click context object. :param ctx: click command context object. :param is_exit_on_except: bool option to exit on exception (default = True). :param agent_src_path: path to an agent dir if needed to load a custom config. :return None """ if agent_src_path is None: agent_src_path = ctx.cwd try: path = Path(os.path.join(agent_src_path, DEFAULT_AEA_CONFIG_FILE)) with open_file(path, mode="r", encoding="utf-8") as fp: ctx.agent_config = ctx.agent_loader.load(fp) ctx.agent_config.directory = Path(agent_src_path) except FileNotFoundError: if is_exit_on_except: raise click.ClickException( "Agent configuration file '{}' not found in the current directory.".format( DEFAULT_AEA_CONFIG_FILE ) ) except ( jsonschema.exceptions.ValidationError, ExtraPropertiesError, AEAValidationError, ) as e: if is_exit_on_except: raise click.ClickException( "Agent configuration file '{}' is invalid: `{}`. Please check the documentation.".format( DEFAULT_AEA_CONFIG_FILE, str(e) ) ) except AEAEnforceError as e: raise click.ClickException(str(e)) # pragma: nocover
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))