|
1 | 1 | import re |
2 | 2 | from typing_extensions import Self |
3 | | -from contextlib import contextmanager |
4 | | -from typing import TYPE_CHECKING, Any, Dict, List, Union, Callable, Optional, Generator |
| 3 | +from contextlib import asynccontextmanager |
| 4 | +from typing import ( |
| 5 | + TYPE_CHECKING, |
| 6 | + Any, |
| 7 | + Dict, |
| 8 | + List, |
| 9 | + Union, |
| 10 | + Callable, |
| 11 | + Optional, |
| 12 | + AsyncGenerator, |
| 13 | +) |
5 | 14 |
|
6 | 15 | from nonebot.typing import overrides |
7 | 16 | from githubkit.utils import UNSET, Unset |
@@ -59,11 +68,9 @@ def _check_nickname(bot: "Bot", event: Event) -> None: |
59 | 68 | if message[0].type != "markdown": |
60 | 69 | return |
61 | 70 |
|
62 | | - seg = message[0] |
63 | | - text = str(seg).lstrip() |
64 | | - |
65 | 71 | if nicknames := {nickname for nickname in bot.config.nickname if nickname}: |
66 | 72 | # check if the user is calling me with my nickname |
| 73 | + text = str(message[0]).lstrip() |
67 | 74 | nickname_regex = "|".join(nicknames) |
68 | 75 | if m := re.match(rf"^({nickname_regex})([\s,,]*|$)", text, re.IGNORECASE): |
69 | 76 | event.to_me = True |
@@ -156,29 +163,31 @@ def __init__(self, adapter: "Adapter", app: OAuthApp): |
156 | 163 | timeout=self.config.api_timeout, |
157 | 164 | ) |
158 | 165 |
|
159 | | - @contextmanager |
160 | | - def as_web_user( |
| 166 | + @asynccontextmanager |
| 167 | + async def as_web_user( |
161 | 168 | self, code: str, redirect_uri: Optional[str] = None |
162 | | - ) -> Generator[Self, None, None]: |
| 169 | + ) -> AsyncGenerator[Self, None]: |
163 | 170 | if self._ctx_github is not None: |
164 | 171 | raise RuntimeError("Can not enter context twice.") |
165 | 172 | self._ctx_github = self._github.with_auth( |
166 | 173 | self._github.auth.as_web_user(code, redirect_uri) |
167 | 174 | ) |
168 | | - try: |
169 | | - yield self |
170 | | - finally: |
171 | | - self._ctx_github = None |
172 | | - |
173 | | - @contextmanager |
174 | | - def as_user(self, token: str) -> Generator[Self, None, None]: |
| 175 | + async with self._ctx_github: |
| 176 | + try: |
| 177 | + yield self |
| 178 | + finally: |
| 179 | + self._ctx_github = None |
| 180 | + |
| 181 | + @asynccontextmanager |
| 182 | + async def as_user(self, token: str) -> AsyncGenerator[Self, None]: |
175 | 183 | if self._ctx_github is not None: |
176 | 184 | raise RuntimeError("Can not enter context twice.") |
177 | 185 | self._ctx_github = GitHub(TokenAuthStrategy(token)) |
178 | | - try: |
179 | | - yield self |
180 | | - finally: |
181 | | - self._ctx_github = None |
| 186 | + async with self._ctx_github: |
| 187 | + try: |
| 188 | + yield self |
| 189 | + finally: |
| 190 | + self._ctx_github = None |
182 | 191 |
|
183 | 192 | @overrides(Bot) |
184 | 193 | async def handle_event(self, event: Event) -> None: |
@@ -207,59 +216,63 @@ async def _get_self_info(self): |
207 | 216 | slug if isinstance((slug := res.parsed_data.slug), str) else None |
208 | 217 | ) |
209 | 218 |
|
210 | | - @contextmanager |
211 | | - def as_oauth_app(self) -> Generator[Self, None, None]: |
| 219 | + @asynccontextmanager |
| 220 | + async def as_oauth_app(self) -> AsyncGenerator[Self, None]: |
212 | 221 | if self._ctx_github is not None: |
213 | 222 | raise RuntimeError("Can not enter context twice.") |
214 | 223 | self._ctx_github = self._github.with_auth(self._github.auth.as_oauth_app()) |
215 | | - try: |
216 | | - yield self |
217 | | - finally: |
218 | | - self._ctx_github = None |
219 | | - |
220 | | - @contextmanager |
221 | | - def as_installation( |
| 224 | + async with self._ctx_github: |
| 225 | + try: |
| 226 | + yield self |
| 227 | + finally: |
| 228 | + self._ctx_github = None |
| 229 | + |
| 230 | + @asynccontextmanager |
| 231 | + async def as_installation( |
222 | 232 | self, |
223 | 233 | installation_id: int, |
224 | 234 | repositories: Union[Unset, List[str]] = UNSET, |
225 | 235 | repository_ids: Union[Unset, List[int]] = UNSET, |
226 | 236 | permissions: Union[Unset, "AppPermissionsType"] = UNSET, |
227 | | - ) -> Generator[Self, None, None]: |
| 237 | + ) -> AsyncGenerator[Self, None]: |
228 | 238 | if self._ctx_github is not None: |
229 | 239 | raise RuntimeError("Can not enter context twice.") |
230 | 240 | self._ctx_github = self._github.with_auth( |
231 | 241 | self._github.auth.as_installation( |
232 | 242 | installation_id, repositories, repository_ids, permissions |
233 | 243 | ) |
234 | 244 | ) |
235 | | - try: |
236 | | - yield self |
237 | | - finally: |
238 | | - self._ctx_github = None |
239 | | - |
240 | | - @contextmanager |
241 | | - def as_web_user( |
| 245 | + async with self._ctx_github: |
| 246 | + try: |
| 247 | + yield self |
| 248 | + finally: |
| 249 | + self._ctx_github = None |
| 250 | + |
| 251 | + @asynccontextmanager |
| 252 | + async def as_web_user( |
242 | 253 | self, code: str, redirect_uri: Optional[str] = None |
243 | | - ) -> Generator[Self, None, None]: |
| 254 | + ) -> AsyncGenerator[Self, None]: |
244 | 255 | if self._ctx_github is not None: |
245 | 256 | raise RuntimeError("Can not enter context twice.") |
246 | 257 | self._ctx_github = self._github.with_auth( |
247 | 258 | self._github.auth.as_oauth_app().as_web_user(code, redirect_uri) |
248 | 259 | ) |
249 | | - try: |
250 | | - yield self |
251 | | - finally: |
252 | | - self._ctx_github = None |
253 | | - |
254 | | - @contextmanager |
255 | | - def as_user(self, token: str) -> Generator[Self, None, None]: |
| 260 | + async with self._ctx_github: |
| 261 | + try: |
| 262 | + yield self |
| 263 | + finally: |
| 264 | + self._ctx_github = None |
| 265 | + |
| 266 | + @asynccontextmanager |
| 267 | + async def as_user(self, token: str) -> AsyncGenerator[Self, None]: |
256 | 268 | if self._ctx_github is not None: |
257 | 269 | raise RuntimeError("Can not enter context twice.") |
258 | 270 | self._ctx_github = GitHub(TokenAuthStrategy(token)) |
259 | | - try: |
260 | | - yield self |
261 | | - finally: |
262 | | - self._ctx_github = None |
| 271 | + async with self._ctx_github: |
| 272 | + try: |
| 273 | + yield self |
| 274 | + finally: |
| 275 | + self._ctx_github = None |
263 | 276 |
|
264 | 277 | @overrides(Bot) |
265 | 278 | async def handle_event(self, event: Event) -> None: |
|
0 commit comments