Exemple #1
0
def get_or_create_cli_config() -> Dict:
    """
    Read or create CLI config from yaml file.

    :return: dict CLI config.
    """
    try:
        return load_yaml(CLI_CONFIG_PATH)
    except FileNotFoundError:
        _init_cli_config()
    return load_yaml(CLI_CONFIG_PATH)
Exemple #2
0
def _set_config(ctx: Context, json_path: List[str], value: str,
                type_str: str) -> None:
    config_loader = cast(ConfigLoader, ctx.config.get("configuration_loader"))
    configuration_file_path = cast(str,
                                   ctx.config.get("configuration_file_path"))

    configuration_object = load_yaml(configuration_file_path)
    config_loader.validator.validate(instance=configuration_object)

    parent_object_path = json_path[:-1]
    attribute_name = json_path[-1]
    parent_object = _get_and_validate_parent_obj(configuration_object,
                                                 parent_object_path,
                                                 attribute_name)

    type_ = FROM_STRING_TO_TYPE[type_str]
    try:
        if type_ != bool:
            parent_object[attribute_name] = type_(value)
        else:
            parent_object[attribute_name] = value not in FALSE_EQUIVALENTS
    except ValueError:  # pragma: no cover
        raise click.ClickException("Cannot convert {} to type {}".format(
            value, type_))

    try:
        configuration_obj = config_loader.configuration_class.from_json(
            configuration_object)
        config_loader.validator.validate(instance=configuration_obj.json)
        config_loader.dump(configuration_obj, open(configuration_file_path,
                                                   "w"))
    except Exception:
        raise click.ClickException("Attribute or value not valid.")
Exemple #3
0
def load_component_public_id(source_path: str, item_type: str) -> PublicId:
    """Get component version from source path."""
    config = load_yaml(os.path.join(source_path, item_type + ".yaml"))
    item_author = config.get("author", "")
    item_name = config.get("name", "")
    item_version = config.get("version", "")
    return PublicId(item_author, item_name, item_version)
Exemple #4
0
def push_item(ctx: Context, item_type: str, item_id: PublicId) -> None:
    """
    Push item to the Registry.

    :param item_type: str type of item (connection/protocol/skill).
    :param item_id: str item name.

    :return: None
    """
    item_type_plural = item_type + "s"

    items_folder = os.path.join(ctx.cwd, item_type_plural)
    item_path = os.path.join(items_folder, item_id.name)

    item_config_filepath = os.path.join(item_path, "{}.yaml".format(item_type))
    logger.debug("Reading {} {} config ...".format(item_id.name, item_type))
    item_config = load_yaml(item_config_filepath)
    check_is_author_logged_in(item_config["author"])

    logger.debug("Searching for {} {} in {} ...".format(
        item_id.name, item_type, items_folder))
    if not os.path.exists(item_path):
        raise click.ClickException(
            '{} "{}" not found  in {}. Make sure you run push command '
            "from a correct folder.".format(item_type.title(), item_id.name,
                                            items_folder))

    output_filename = "{}.tar.gz".format(item_id.name)
    logger.debug("Compressing {} {} to {} ...".format(item_id.name, item_type,
                                                      output_filename))
    _compress_dir(output_filename, item_path)
    output_filepath = os.path.join(ctx.cwd, output_filename)

    data = {
        "name": item_id.name,
        "description": item_config["description"],
        "version": item_config["version"],
    }

    # dependencies
    for key in ["connections", "contracts", "protocols", "skills"]:
        deps_list = item_config.get(key)
        if deps_list:
            data.update({key: deps_list})

    path = "/{}/create".format(item_type_plural)
    logger.debug("Pushing {} {} to Registry ...".format(
        item_id.name, item_type))
    resp = request_api("POST",
                       path,
                       data=data,
                       is_auth=True,
                       filepath=output_filepath)
    click.echo(
        "Successfully pushed {} {} to the Registry. Public ID: {}".format(
            item_type, item_id.name, resp["public_id"]))
Exemple #5
0
    def _load_configuration_object(self) -> Dict:
        """Load configuration object for component/agent."""
        if self.is_target_agent:
            configuration_object = self.agent_config.json
        else:
            configuration_object = load_yaml(
                str(self.path_to_resource_configuration))

        self.config_loader.validate(configuration_object)
        return configuration_object
Exemple #6
0
def _check_package_public_id(source_path, item_type, item_id) -> None:
    # we load only based on item_name, hence also check item_version and item_author match.
    config = load_yaml(os.path.join(source_path, item_type + ".yaml"))
    item_author = config.get("author", "")
    item_name = config.get("name", "")
    item_version = config.get("version", "")
    actual_item_id = PublicId(item_author, item_name, item_version)
    if not actual_item_id.same_prefix(item_id) or (
            not item_id.package_version.is_latest
            and item_id.version != actual_item_id.version):
        raise click.ClickException(
            "Version, name or author does not match. Expected '{}', found '{}'"
            .format(item_id,
                    item_author + "/" + item_name + ":" + item_version))
Exemple #7
0
def _get_config_value(ctx: Context, json_path: List[str]):
    config_loader = cast(ConfigLoader, ctx.config.get("configuration_loader"))
    configuration_file_path = cast(str,
                                   ctx.config.get("configuration_file_path"))

    configuration_object = load_yaml(configuration_file_path)
    config_loader.validator.validate(instance=configuration_object)

    parent_object_path = json_path[:-1]
    attribute_name = json_path[-1]
    parent_object = _get_and_validate_parent_obj(configuration_object,
                                                 parent_object_path,
                                                 attribute_name)

    return parent_object.get(attribute_name)
Exemple #8
0
def _check_package_public_id(source_path, item_type, item_id) -> None:
    # we load only based on item_name, hence also check item_version and item_author match.
    config = load_yaml(os.path.join(source_path, item_type + ".yaml"))
    item_author = config.get("author", "")
    item_name = config.get("name", "")
    item_version = config.get("version", "")
    if (
        item_id.name != item_name
        or item_id.author != item_author
        or item_id.version != item_version
    ):
        raise click.ClickException(
            "Version, name or author does not match. Expected '{}', found '{}'".format(
                item_id, item_author + "/" + item_name + ":" + item_version
            )
        )
Exemple #9
0
def push_item(ctx: Context, item_type: str, item_id: PublicId) -> None:
    """
    Push item to the Registry.

    :param item_type: str type of item (connection/protocol/skill).
    :param item_id: str item name.

    :return: None
    """
    item_type_plural = item_type + "s"

    items_folder = os.path.join(ctx.cwd, item_type_plural)
    item_path = os.path.join(items_folder, item_id.name)

    if not os.path.exists(item_path):
        raise click.ClickException(
            '{} "{}" not found  in {}. Make sure you run push command '
            "from a correct folder.".format(item_type.title(), item_id.name,
                                            items_folder))

    check_package_public_id(item_path, item_type, item_id)

    item_config_filepath = os.path.join(item_path, "{}.yaml".format(item_type))
    logger.debug("Reading {} {} config ...".format(item_id.name, item_type))
    item_config = load_yaml(item_config_filepath)
    check_is_author_logged_in(item_config["author"])

    logger.debug("Searching for {} {} in {} ...".format(
        item_id.name, item_type, items_folder))

    output_filename = "{}.tar.gz".format(item_id.name)
    logger.debug("Compressing {} {} to {} ...".format(item_id.name, item_type,
                                                      output_filename))
    _compress_dir(output_filename, item_path)
    output_filepath = os.path.join(ctx.cwd, output_filename)

    data = {
        "name": item_id.name,
        "description": item_config["description"],
        "version": item_config["version"],
    }

    # dependencies
    for key in [CONNECTIONS, CONTRACTS, PROTOCOLS, SKILLS]:
        deps_list = item_config.get(key)
        if deps_list:
            data.update({key: deps_list})

    try:
        files = {"file": open(output_filepath, "rb")}
        readme_path = os.path.join(item_path, DEFAULT_README_FILE)
        if is_readme_present(readme_path):
            files["readme"] = open(readme_path, "rb")

        path = "/{}/create".format(item_type_plural)
        logger.debug("Pushing {} {} to Registry ...".format(
            item_id.name, item_type))
        resp = cast(
            JSONLike,
            request_api("POST", path, data=data, is_auth=True, files=files))
    finally:
        for fd in files.values():
            fd.close()
            click.echo(
                "Successfully pushed {} {} to the Registry. Public ID: {}".
                format(item_type, item_id.name, resp["public_id"]))