def run(StmTemplateConfig, TemplateElementTagNameList, PduPropertyList, PduValueList):
    '''
        StmTemplateConfig is the container for the StreamBlock's template.
        TemplateElementTagNameList holds a list of tag names that associate with StreamBlocks in
                the template that are to be targeted for modification.
        PduInfoList represents a list of PDU information to update in the FrameConfig
                for all StreamBlocks in the template that associate with any of the
                given tag names.
    '''
    plLogger = PLLogger.GetLogger('methodology')
    plLogger.LogDebug('ConfigTemplatePdusCommand.run')

    template = CHandleRegistry.Instance().Find(StmTemplateConfig)
    if not template or not template.IsTypeOf('StmTemplateConfig'):
        plLogger.LogError('Input StmTemplateConfig is not an StmTemplateConfig')
        return False

    # Load the template's contents into a DOM for processing...
    root = etree.fromstring(template.Get('TemplateXml'))

    sb_elements = xml_utils.find_tagged_elements_by_tag_name(root,
                                                             TemplateElementTagNameList,
                                                             'StreamBlock')
    if not sb_elements or len(sb_elements) == 0:
        plLogger.LogError('TemplateElementTagNameList does not refer ' +
                          'to any streamblock elements.')
        return False

    for sb_ele in sb_elements:
        if not process_pdus(PduPropertyList, PduValueList, sb_ele):
            return False

    # Update the StmTemplateConfig object with all the changes made above...
    template.Set('TemplateXml', etree.tostring(root))
    return True
def find_tagged_filtered_elements(root, tag_list, ignore_tags_obj=True):
    ele_list = xml_utils.find_tagged_elements_by_tag_name(root, tag_list)
    if ele_list is None:
        return []
    if ignore_tags_obj:
        # Remove the Tags element as it has a UserTag relation
        # to each of the Tag objects that will cause it to
        # appear to be tagged
        filtered_ele_list = []
        for ele in ele_list:
            if ele.tag != "Tags":
                filtered_ele_list.append(ele)
        return filtered_ele_list
    return ele_list
def run(
    StmTemplateConfig,
    TagName,
    TargetObjectTagName,
    ObjectName,
    PropertyName,
    ModifierType,
    StartList,
    StepList,
    RepeatList,
    RecycleList,
    TargetObjectStepList,
    ResetOnNewTargetObject,
):
    plLogger = PLLogger.GetLogger("methodology")
    plLogger.LogDebug("ConfigTemplateStmPropertyModifierCommand.start")
    hnd_reg = CHandleRegistry.Instance()
    ctor = CScriptableCreator()
    this_cmd = get_this_cmd()

    template = hnd_reg.Find(StmTemplateConfig)
    if template is None:
        err_str = "Invalid or missing StmTemplateConfig"
        plLogger.LogError(err_str)
        this_cmd.Set("Status", err_str)
        return False
    if not template.IsTypeOf("StmTemplateConfig"):
        err_str = "Input StmTemplateConfig is not an StmTemplateConfig"
        plLogger.LogError(err_str)
        this_cmd.Set("Status", err_str)
        return False

    # Check the ModifierType
    if ModifierType != "RANGE":
        err_str = "Unspported ModifierType: " + ModifierType
        this_cmd.Set("Status", err_str)
        return False

    # Validate the ObjectName and PropertyName
    # Note that neither are required if an existing modifier can
    # be modified.  However, both are required if a new modifier
    # is being added.
    err_str, obj_name, prop_name = check_obj_and_prop_names(ObjectName, PropertyName)

    plLogger.LogDebug("checked ObjectName: " + str(obj_name))
    plLogger.LogDebug("checked PropertyName: " + str(prop_name))

    # Find the Tag with TagName in the StmTemplateConfig
    mod_ele_list = []
    if TagName != "":
        root = etree.fromstring(template.Get("TemplateXml"))
        tagged_ele_list = xml_utils.find_tagged_elements_by_tag_name(root, [TagName], ignore_tags_element=True)

        if len(tagged_ele_list):
            err_str, res_list = find_matching_modifiers(tagged_ele_list, obj_name, prop_name)
            if err_str != "":
                this_cmd.Set("Status", err_str)
                return False
            if len(res_list):
                mod_ele_list = res_list

    # If an existing StmPropertyModifier is being modified,
    # nothing is required.  Whatever exists will be updated; whatever
    # doesn't exist will be added.
    # If there is no existing StmPropertyModifier, PropertyName,
    # ObjectName, start, and step are required.
    start_list = []
    step_list = []
    repeat_list = []
    recycle_list = []
    target_step_list = []
    if len(mod_ele_list) == 0:
        plLogger.LogInfo("Add a new StmPropertyModifier on " + ObjectName + "'s " + PropertyName + ".")
        if obj_name == "":
            err_str = "ObjectName is required to add a new " + "StmPropertyModifier with tag name " + TagName + "."
            this_cmd.Set("Status", err_str)
            return False
        if prop_name == "":
            err_str = (
                "PropertyName is required to add a new "
                + "StmPropertyModifier on "
                + ObjectName
                + " with tag name "
                + TagName
                + "."
            )
            this_cmd.Set("Status", err_str)

        # StartList
        if StartList is None or StartList == "" or StartList == []:
            err_str = (
                "StartList is required to add a new "
                + "StmPropertyModifier on "
                + ObjectName
                + " property "
                + PropertyName
                + " with tag name "
                + TagName
                + "."
            )
            this_cmd.Set("Status", err_str)
        else:
            # Check start_list values for validity
            for value in StartList:
                res = dm_utils.validate_obj_prop_val(obj_name, prop_name, value)
                if res != "":
                    this_cmd.Set("Status", res)
                    return False
            start_list = StartList

        # StepList
        if StepList is None or StepList == "" or StepList == []:
            err_str = (
                "StepList is required to add a new "
                + "StmPropertyModifier on "
                + ObjectName
                + " property "
                + PropertyName
                + " with tag name "
                + TagName
                + "."
            )
            this_cmd.Set("Status", err_str)
        else:
            # Check step_list values for validity
            for value in step_list:
                res = dm_utils.validate_obj_prop_val(obj_name, prop_name, value)
                if res != "":
                    this_cmd.Set("Status", res)
                    return False
            step_list = StepList

        # RepeatList
        if RepeatList is None or RepeatList == "" or RepeatList == []:
            repeat_list = None
        else:
            repeat_list = RepeatList

        # RecycleList
        if RecycleList is None or RecycleList == "" or RecycleList == []:
            recycle_list = None
        else:
            recycle_list = RecycleList

        # TargetObjectStepList
        if TargetObjectStepList is None or TargetObjectStepList == "" or TargetObjectStepList == []:
            target_step_list = None
        else:
            # Check target_step_list values for validity
            for value in target_step_list:
                res = dm_utils.validate_obj_prop_val(obj_name, prop_name, value)
                if res != "":
                    this_cmd.Set("Status", res)
                    return False
            target_step_list = TargetObjectStepList

        # Build the new modifier info
        mod_info = gen_range_modifier_json(
            obj_name,
            prop_name,
            start_list,
            step_list,
            repeat=repeat_list,
            recycle=recycle_list,
            target_step=target_step_list,
            reset=ResetOnNewTargetObject,
        )

        # Create a new StmPropertyModifier under the elements tagged by
        # TargetObjectTagName
        # Call AddTemplateObjectCommand
        add_cmd = ctor.CreateCommand(PKG + ".AddTemplateObjectCommand")
        add_cmd.Set("StmTemplateConfig", template.GetObjectHandle())
        add_cmd.Set("ParentTagName", TargetObjectTagName)
        add_cmd.Set("TagName", TagName)
        add_cmd.Set("ClassName", "StmPropertyModifier")
        add_cmd.SetCollection("PropertyList", ["ModifierInfo", "TagName"])
        add_cmd.SetCollection("ValueList", [mod_info, TargetObjectTagName])
        add_cmd.Execute()
        add_cmd.MarkDelete()
    else:
        # Get the ObjectName/PropertyName from the existing info
        for mod_ele in mod_ele_list:
            mod_info = mod_ele.get("ModifierInfo")
            err_str, mod_dict = json_utils.load_json(mod_info)
            if err_str != "":
                t_err_str = (
                    "Failed to read ModifierInfo out of the " + "StmTemplateModifier that is being modified: " + err_str
                )
                this_cmd.Set("Status", t_err_str)
                return False

            plLogger.LogDebug("ModifierInfo json: " + json.dumps(mod_dict))
            if prop_name == "":
                prop_name = mod_dict["propertyName"]
            if obj_name == "":
                obj_name = mod_dict["objectName"]
            plLogger.LogInfo(
                "Found an StmPropertyModifier tagged with "
                + TagName
                + " that is modifying "
                + str(obj_name)
                + "'s "
                + str(prop_name)
                + " to modify."
            )
            pv_dict = mod_dict["propertyValueDict"]

            # StartList
            if StartList is not None and StartList != "" and StartList != []:
                start_list = StartList
                for value in start_list:
                    res = dm_utils.validate_obj_prop_val(obj_name, prop_name, value)
                    if res != "":
                        this_cmd.Set("Status", res)
                        return False
                pv_dict["start"] = start_list

            # StepList
            if StepList is not None and StepList != "" and StepList != []:
                step_list = StepList
                for value in step_list:
                    res = dm_utils.validate_obj_prop_val(obj_name, prop_name, value)
                    if res != "":
                        this_cmd.Set("Status", res)
                        return False
                pv_dict["step"] = step_list

            # RepeatList
            if RepeatList is not None and RepeatList != "" and RepeatList != []:
                pv_dict["repeat"] = RepeatList

            # RecycleList
            if RecycleList is not None and RecycleList != "" and RecycleList != []:
                pv_dict["recycle"] = RecycleList

            # TargetObjectStepList
            if TargetObjectStepList is not None and TargetObjectStepList != "" and TargetObjectStepList != []:
                target_step_list = TargetObjectStepList
                for value in target_step_list:
                    res = dm_utils.validate_obj_prop_val(obj_name, prop_name, value)
                    if res != "":
                        this_cmd.Set("Status", res)
                        return False
                pv_dict["targetObjectStepList"] = target_step_list

            # Call ModifyTemplatePropertyCommand
            plLogger.LogInfo("call the modify template property command")
            mod_cmd = ctor.CreateCommand(PKG + ".ModifyTemplatePropertyCommand")
            mod_cmd.Set("StmTemplateConfig", template.GetObjectHandle())
            mod_cmd.SetCollection("TagNameList", [TagName])
            mod_cmd.SetCollection("PropertyList", ["StmPropertyModifier.ModifierInfo"])
            mod_cmd.SetCollection("ValueList", [json.dumps(mod_dict)])
            mod_cmd.Execute()
            mod_cmd.MarkDelete()

    plLogger.LogDebug("ConfigTemplateStmPropertyModifierCommand.end")
    return True
def run(StmTemplateConfig, TagNameList,
        PropertyList, ValueList):
    plLogger = PLLogger.GetLogger('methodology')
    plLogger.LogDebug("Run ModifyTemplatePropertyCommand")

    hnd_reg = CHandleRegistry.Instance()
    template = hnd_reg.Find(StmTemplateConfig)
    if template is None:
        plLogger.LogError("Invalid or missing StmTemplateConfig")
    if not template.IsTypeOf("StmTemplateConfig"):
        plLogger.LogError("Input StmTemplateConfig is not an " +
                          "StmTemplateConfig")
        return False

    # Parse the PropertyList and ValueList
    attr_dict, msg = xml_utils.parse_prop_and_val_list(PropertyList,
                                                       ValueList)
    if msg != "":
        plLogger.LogError("Failed to parse PropertyList and " +
                          "ValueList: " + msg)
        return False

    plLogger.LogDebug("attr_dict contains: " + str(attr_dict))
    root = etree.fromstring(template.Get("TemplateXml"))

    # Build a unique list of the tagged objects and print a warning if no
    # objects found
    tagged_objs = set()
    for tagName in TagNameList:
        taggedObjects = xml_utils.find_tagged_elements_by_tag_name(root,
                                                                   [tagName])
        # Check if returned list is empty
        if not taggedObjects:
            plLogger.LogWarn("No tagged objects found for tag: " + str(tagName))
        else:
            for tagged_obj in taggedObjects:
                if tagged_obj.tag != "Tags":
                    tagged_objs.add(tagged_obj)

    for tagged_obj in tagged_objs:
        plLogger.LogDebug("tagged_obj: " + str(tagged_obj))

    for obj_type in attr_dict["obj_list"]:
        if len(TagNameList) > 0:
            # If there are tags specified (valid or invalid)
            found = False
            for tagged_obj in tagged_objs:
                # Go through list of tagged objects and determine if
                # the object type matches a tagged object
                if obj_type == tagged_obj.tag:
                    # If it matches, set all given properties for that object
                    found = True
                    for param in attr_dict[tagged_obj.tag]:
                        tagged_obj.set(param, attr_dict[tagged_obj.tag + "." + param])
            if not found:
                # Object to modify was not found in list of tagged objects
                err = "Did not find object " + obj_type + " in list of " + \
                      "tagged objects. The following properties in " + \
                      obj_type + " were not modified: " + str(attr_dict[obj_type])
                plLogger.LogError(err)
                raise RuntimeError(err)
        else:
            # Else no tags specified, modify everything
            ele_list = root.findall(".//" + obj_type)
            plLogger.LogDebug("ele_list contains: " + str(ele_list))
            for ele in ele_list:
                for param in attr_dict[ele.tag]:
                    ele.set(param, attr_dict[ele.tag + "." + param])

    template.Set("TemplateXml", etree.tostring(root))
    plLogger.LogDebug("new TemplateXml is " + template.Get("TemplateXml"))
    return True