跳转至

context

本模块创建了 Ariadne 中的上下文变量

enter_context 🔗

enter_context(
    app: Optional[Ariadne] = None, event: Optional[Dispatchable] = None
)

进入事件上下文

Parameters:

  • app (Ariadne) –

    Ariadne 实例.

  • event (Dispatchable) –

    当前事件

Source code in graia/ariadne/context.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@contextmanager
def enter_context(app: Optional["Ariadne"] = None, event: Optional["Dispatchable"] = None):
    """进入事件上下文

    Args:
        app (Ariadne, optional): Ariadne 实例.
        event (Dispatchable, optional): 当前事件
    """
    token_loop = None
    token_bcc = None
    token_app = None
    if app:
        token_app = ariadne_ctx.set(app)
        token_loop = event_loop_ctx.set(app.service.loop)
        token_bcc = broadcast_ctx.set(app.service.broadcast)
    token_event = event_ctx.set(event) if event else None
    yield

    with suppress(ValueError):
        if token_app:
            ariadne_ctx.reset(token_app)
        if token_event:
            event_ctx.reset(token_event)
        if token_loop:
            event_loop_ctx.reset(token_loop)
        if token_bcc:
            broadcast_ctx.reset(token_bcc)

enter_message_send_context 🔗

enter_message_send_context(method: UploadMethod)

进入消息发送上下文

Parameters:

Source code in graia/ariadne/context.py
33
34
35
36
37
38
39
40
41
42
@contextmanager
def enter_message_send_context(method: "UploadMethod"):
    """进入消息发送上下文

    Args:
        method (UploadMethod): 消息上下文的枚举对象
    """
    t = upload_method_ctx.set(method)
    yield
    upload_method_ctx.reset(t)