def update_attributes()

in projects/conversational-commerce-agent/data-ingestion/flipkart_to_retail_search.py [0:0]


def update_attributes(source_obj) -> dict:
    """
    Update site level attribute controls
    Args:
        source_obj: source product attribute controls
    Returns:
        dict: updated attribute controls
    """
    target_obj = {
        "attributes" : {}
    }
    if "product_specifications" in source_obj:
        source_prod_specs = source_obj["product_specifications"]
        try:
            source_prod_specs = source_prod_specs.replace(
                    "=>nil", ":null"
                )
            source_prod_specs = source_prod_specs.replace(
                    "=>", ":"
                )
            specs = json.loads(
                source_prod_specs
            )
        except json.JSONDecodeError:
            logging.warning(
                ("[Warning]Could not decode "
                "product_specifications: %s"),
                source_obj
            )
            return {}

        if specs.get("product_specification") is None:
            return {}

        for attr in specs["product_specification"]:
            target_attrs = target_obj["attributes"]
            try:
                if not isinstance(attr, dict):
                    continue

                attr_key = ""
                attr_value = ""
                if "key" in attr:
                    logging.info("* processing: %s", attr)
                    attr_key = attr["key"]
                    attr_value = attr.get("value", "")

                if (attr_value == "" or
                    attr_key == ""):
                    continue

                target_attrs[attr_key] = {
                    "text": [attr_value]
                }
                if attr_key == "Type":
                    target_attrs[attr_key]["searchable"] = True
                    target_attrs[attr_key]["indexable"] = True
                elif not re.search(r"[ \-/\\]", attr_key):
                    target_attrs[attr_key]["searchable"] = True
                    target_attrs[attr_key]["indexable"] = True
            except KeyError as e:
                logging.error("[Error]%s", e)
                logging.error("* Attribute:%s", attr)
    else:
        logging.error("Product Spec not found.")
    return target_attrs