def inherit_modification()

in src/aaz_dev/command/controller/workspace_cfg_editor.py [0:0]


    def inherit_modification(self, ref_cfg: CfgReader):
        command_rename_list = []
        existing_resources = set()
        existing_sub_resources = set()
        for cmd_names, command in self.iter_commands():
            counterpart = self._find_ref_command_counterpart(cmd_names, command, ref_cfg)
            if not counterpart:
                continue
            ref_cmd_names, ref_command = counterpart
            self._inherit_modification_in_command(command, ref_command)
            command_rename_list.append((cmd_names, ref_cmd_names))
            for r in ref_command.resources:
                existing_resources.add(r.id)
                if r.subresource:
                    existing_sub_resources.add((r.id, r.subresource))

        # rename commands
        for cmd_names, ref_cmd_names in command_rename_list:
            self.rename_command(*cmd_names, new_cmd_names=ref_cmd_names)

        # inherit sub command
        sub_resources = set()
        array_sub_resources = set()
        dict_sub_resources = set()
        for ref_cmd_names, ref_command in ref_cfg.iter_commands():
            for r in ref_command.resources:
                if r.id not in existing_resources:
                    # ignore unrelated resources
                    continue
                if not r.subresource:
                    continue
                subresource_id = r.subresource
                if subresource_id.endswith('[]'):
                    subresource_id = subresource_id[:-2]
                    array_sub_resources.add((r.id, subresource_id))
                elif subresource_id.endswith('{}'):
                    subresource_id = subresource_id[:-2]
                    dict_sub_resources.add((r.id, subresource_id))
                sub_resources.add((r.id, subresource_id))
        sub_resources.difference_update(existing_sub_resources)
        if not sub_resources:
            return

        command_rename_list = []
        for resource_id, subresource_id in sub_resources:
            update_cmd_info = self.get_update_cmd(resource_id)
            if not update_cmd_info:
                continue
            _, update_cmd, update_by = update_cmd_info
            if update_by != "GenericOnly":
                continue

            update_cmd.link()
            update_op = None
            for operation in update_cmd.operations:
                if isinstance(operation, CMDInstanceUpdateOperation):
                    update_op = operation
            assert update_op is not None
            update_json = update_op.instance_update.json
            subresource_idx = self.idx_to_list(subresource_id)
            schema = self.find_schema_in_json(update_json, subresource_idx)
            if not schema:
                # schema not exist
                continue

            # skip inherit identity subcommands, it will be generated in build_identity_subresource
            if isinstance(schema, CMDIdentityObjectSchemaBase):
                continue

            assert isinstance(schema, CMDSchema)

            # build ref_args_options
            cg_names = None
            ref_args_options = {}
            for ref_cmd_names, ref_command in ref_cfg.iter_commands_by_resource(resource_id, subresource_id):
                cg_names = ref_cmd_names[:-1]
                for ref_arg_group in ref_command.arg_groups:
                    for ref_arg in ref_arg_group.args:
                        ref_args_options[ref_arg.var] = [*ref_arg.options]
            if (resource_id, subresource_id) in array_sub_resources:
                for ref_cmd_names, ref_command in ref_cfg.iter_commands_by_resource(resource_id, subresource_id + '[]'):
                    cg_names = ref_cmd_names[:-1]
                    for ref_arg_group in ref_command.arg_groups:
                        for ref_arg in ref_arg_group.args:
                            ref_args_options[ref_arg.var] = [*ref_arg.options]
            if (resource_id, subresource_id) in dict_sub_resources:
                for ref_cmd_names, ref_command in ref_cfg.iter_commands_by_resource(resource_id, subresource_id + '{}'):
                    cg_names = ref_cmd_names[:-1]
                    for ref_arg_group in ref_command.arg_groups:
                        for ref_arg in ref_arg_group.args:
                            ref_args_options[ref_arg.var] = [*ref_arg.options]
            assert cg_names is not None

            # generate sub commands
            sub_commands = self._generate_sub_commands(schema, subresource_idx, update_cmd, ref_args_options)
            for sub_command in sub_commands:
                cmd_names = [*cg_names, sub_command.name]
                self._add_command(*cmd_names, command=sub_command)
                counterpart = self._find_ref_command_counterpart(cmd_names, sub_command, ref_cfg)
                if not counterpart:
                    continue
                ref_cmd_names, ref_command = counterpart
                self._inherit_modification_in_command(sub_command, ref_command)
                command_rename_list.append((cmd_names, ref_cmd_names))

        # rename sub commands
        for cmd_names, ref_cmd_names in command_rename_list:
            self.rename_command(*cmd_names, new_cmd_names=ref_cmd_names)

        self.reformat()