Exemplo n.º 1
0
    def prefix_match(self, target_chain: MessageChain):
        target_chain = target_chain.asMerged()

        chain_frames: List[MessageChain] = target_chain.split(" ",
                                                              raw_string=True)

        # 前缀匹配
        if len(self.prefixs) > len(chain_frames):
            return
        for index, current_prefix in enumerate(self.prefixs):
            current_frame = chain_frames[index]
            if (not current_frame.__root__
                    or type(current_frame.__root__[0]) is not Plain):
                return
            if current_frame.__root__[0].text != current_prefix:
                return

        chain_frames = chain_frames[len(self.prefixs):]
        return MessageChain.create(
            list(
                itertools.chain(
                    *[i.__root__ + [Plain(" ")]
                      for i in chain_frames]))[:-1]).asMerged()
Exemplo n.º 2
0
 def cmdFilter(self, message: MessageChain):
     message: str = message.asDisplay()
     match = self.ripeReg(message)
     dis: [str] = message.split(' ')[0].upper()
     if dis[:3] not in self.APP_COMMANDS and not match:
         raise ExecutionStop()
Exemplo n.º 3
0
    def detect_index(self, target_chain: MessageChain):
        target_chain = target_chain.asMerged()
        detect_result: Dict[str, List[Tuple[MessageIndex, MessageIndex]]] = {
            "_": []  # 相当于 *args.
        }

        chain_frames: List[MessageChain] = target_chain.split(self.delimiter,
                                                              raw_string=True)

        # 前缀匹配
        if len(self.prefixs) > len(chain_frames):
            return
        for index in range(len(self.prefixs)):
            current_prefix = self.prefixs[index]
            current_frame = chain_frames[index]
            if (not current_frame.__root__
                    or type(current_frame.__root__[0]) is not Plain):
                return
            if current_frame.__root__[0].text != current_prefix:
                return

        chain_frames = chain_frames[len(self.prefixs):]  # 清除无关数据, 开始执行.

        collections: List[Element] = detect_result["_"]

        detecting_param = None
        local_iter = MultiUsageGenerator(enumerate(chain_frames))
        # print(list(local_iter))
        for iter_item in local_iter:
            print(92, iter_item)
            index, current_frame = iter_item
            current_frame: MessageChain
            if not current_frame.__root__:
                collections.append(current_frame)
                continue

            if detecting_param and isinstance(
                    self.param_settings[detecting_param], BoxParameter):
                detect_result[detecting_param] = current_frame
                detecting_param = None
                continue

            splited = current_frame.split("=", raw_string=True)
            origin_data = self.param_settings_index.get(splited[0].asDisplay())
            if not origin_data:
                if not detecting_param:
                    if current_frame.startswith(
                            '"'):  # TODO: 我现在还需要一个更加合理的引号出现判断.
                        # debug("ocur", index, chain_frames, current_frame)
                        afters_root = MessageChain.create(
                            sum(
                                [[*i.__root__,
                                  Plain(self.delimiter)]
                                 for i in chain_frames[max(0, index - 1):]],
                                [],
                            )[:-1]).asMerged()
                        print("aftersRoot", index, afters_root)

                        break_flag_root = False
                        param_content_root = []
                        for elem in afters_root.subchain(
                                slice((0, 1), None,
                                      None), ignore_text_index=True):
                            print(elem)
                            if break_flag_root:
                                break_flag_root = False
                                break
                            if isinstance(elem, Plain):
                                continue_flag = False
                                for text_index, text_i in enumerate(elem.text):
                                    if continue_flag:
                                        continue_flag = False
                                        continue

                                    if text_i == "\\":
                                        continue_flag = True
                                    if text_i == '"':
                                        param_content_root.append(
                                            Plain(elem.text[:text_index]))
                                        break_flag_root = True
                                        break
                                else:
                                    # 没找到
                                    param_content_root.append(elem)
                                    break
                            else:
                                param_content_root.append(elem)
                                break
                        else:
                            if not break_flag_root:
                                raise ValueError("no closing quotes")
                        param_content_chain_root = MessageChain.create(
                            param_content_root)
                        local_iter.continue_count += len(
                            param_content_chain_root.split(self.delimiter,
                                                           raw_string=True))
                        collections.append(param_content_chain_root)
                    else:
                        collections.append(current_frame)
                continue
            param_name, setting = origin_data
            detecting_param = param_name
            if param_name in detect_result:
                continue  # 用户重复输入了参数

            if isinstance(setting, SwitchParameter):  # 这里是已经被 catch 到了.
                if setting.auto_reverse:
                    detect_result[param_name] = not setting.default
                else:
                    detect_result[param_name] = True
            elif isinstance(setting, BoxParameter):
                afters = MessageChain.create(
                    sum(
                        ([i.__root__
                          for i in splited[1:]] + [[Plain(self.delimiter)]]
                         if len(splited) > 1 else []) +
                        [[*i.__root__, Plain(self.delimiter)]
                         for i in chain_frames[index + 1:]],
                        [],
                    )[:-1]).asMerged()
                break_flag = False
                if afters.startswith('"'):
                    param_content = []
                    for elem in afters.subchain(slice((0, 1), None, None),
                                                ignore_text_index=True):
                        if break_flag:
                            break_flag = False
                            break
                        if isinstance(elem, Plain):
                            continue_flag = False
                            for text_index, text_i in enumerate(elem.text):
                                if continue_flag:
                                    continue_flag = False
                                    continue

                                if text_i == "\\":
                                    continue_flag = True
                                if text_i == '"':
                                    param_content.append(
                                        Plain(elem.text[:text_index]))
                                    break_flag = True
                                    break
                            else:
                                # 没找到
                                param_content.append(elem)
                        else:
                            param_content.append(elem)
                    else:
                        if not break_flag:
                            raise ValueError("no closing quotes")
                    param_content_chain = MessageChain.create(param_content)
                    print(
                        param_content,
                        chain_frames[len(
                            param_content_chain.
                            split(self.delimiter, raw_string=True)) + 1:],
                    )
                    local_iter.continue_count += (len(
                        param_content_chain.split(self.delimiter,
                                                  raw_string=True)) - 1)
                    detect_result[param_name] = [
                        MessageChain.create(param_content)
                    ]
                else:
                    detecting_param = param_name

                if len(splited) > 1 and not break_flag:
                    local_iter.insert_items.extend([
                        (index + l_index, value)
                        for l_index, value in enumerate(splited[1:], 1)
                    ])
                detecting_param = None
        else:
            if detecting_param and detecting_param not in detect_result:
                if self.param_settings[detecting_param].default is None:
                    raise ValueError("require for " + detecting_param)

        # 后处理
        for k, v in self.param_settings.items():
            if isinstance(v, SwitchParameter) and k not in detect_result:
                detect_result[k] = v.default
            elif isinstance(v, BoxParameter) and k not in detect_result:
                detect_result[k] = Force(None)
        return detect_result