跳转至

util

ContextVarDispatcher 🔗

Bases: BaseDispatcher

分发常量给指定名称的参数

Source code in src/graia/ariadne/message/commander/util.py
262
263
264
265
266
267
268
269
class ContextVarDispatcher(BaseDispatcher):
    """分发常量给指定名称的参数"""

    def __init__(self, data_ctx: ContextVar[dict[str, Any]]) -> None:
        self.data_ctx = data_ctx

    async def catch(self, interface: DispatcherInterface):
        return self.data_ctx.get().get(interface.name)

escape 🔗

escape(string: str) -> str

转义字符串

Parameters:

  • string (str) –

    要转义的字符串

Returns:

  • str( str ) –

    转义后的字符串

Source code in src/graia/ariadne/message/commander/util.py
33
34
35
36
37
38
39
40
41
42
43
44
45
def escape(string: str) -> str:
    """转义字符串

    Args:
        string (str): 要转义的字符串

    Returns:
        str: 转义后的字符串
    """
    for k, v in ESCAPE.items():
        string = string.replace("\\" + k, v)
    string = string.replace("\\", "")
    return string

tokenize 🔗

tokenize(string: str) -> list[U_Token]

将字符串转义化, 并处理为 Text, Param 两种 token

Parameters:

  • string (str) –

    要处理的字符串

Returns:

  • list[U_Token]

    List[Tuple[CommandToken, List[int, str]]]: 处理后的 Token

Source code in src/graia/ariadne/message/commander/util.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def tokenize(string: str) -> list[U_Token]:
    """将字符串转义化, 并处理为 Text,  Param 两种 token

    Args:
        string (str): 要处理的字符串

    Returns:
        List[Tuple[CommandToken, List[int, str]]]: 处理后的 Token
    """

    string = escape(string)

    paren: str = ""
    char_stk: list[str] = []
    token: list[U_Token] = []
    pop = functools.partial(_pop, char_stk)

    for index, char in enumerate(string):
        if char in L_PAREN + R_PAREN:
            if char in L_PAREN:
                assert not paren, (
                    f"""Duplicated parenthesis character "{char}" @ {index} !"""
                    """Are you sure you've escaped with "\\"?"""
                )
                paren = char
            elif char in R_PAREN:
                piece = pop()
                assert paren, f"No matching parenthesis: {paren} @ {index}"
                if paren == "[":  # CHOICE
                    token.append(Text(unescape(x) for x in piece.split("|")))
                elif paren == "{":  # PARAM
                    token.append(parse_param(piece))
                paren = ""
        elif char == " " and not paren:
            if char_stk:
                token.append(Text(pop()))
        else:
            char_stk.append(char)

    assert not paren, f"Unclosed parenthesis: {paren}"

    if char_stk:
        token.append(Text(pop()))

    return token

unescape 🔗

unescape(string: str) -> str

逆转义字符串, 自动去除空白符

Parameters:

  • string (str) –

    要逆转义的字符串

Returns:

  • str( str ) –

    逆转义后的字符串

Source code in src/graia/ariadne/message/commander/util.py
48
49
50
51
52
53
54
55
56
57
58
59
def unescape(string: str) -> str:
    """逆转义字符串, 自动去除空白符

    Args:
        string (str): 要逆转义的字符串

    Returns:
        str: 逆转义后的字符串
    """
    for k, v in R_ESCAPE.items():
        string = string.replace(k, v)
    return string.strip()