Skip to content

Commit 17717bb

Browse files
committed
Add a global context
The global context will be used by other parts of the code in the future, to store information related to the current execution.
1 parent 1bdd726 commit 17717bb

File tree

3 files changed

+98
-17
lines changed

3 files changed

+98
-17
lines changed

botogram/context.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) 2015-2017 The Botogram Authors (see AUTHORS)
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
# DEALINGS IN THE SOFTWARE.
20+
21+
import threading
22+
23+
24+
_local = threading.local()
25+
_local._botogram_context = []
26+
27+
28+
class Context:
29+
"""Context of an hook call"""
30+
31+
def __init__(self, bot, hook, update):
32+
self.bot = bot
33+
self.hook = hook
34+
self.update = update
35+
36+
def __enter__(self):
37+
_local._botogram_context.append(self)
38+
39+
def __exit__(self, *_):
40+
_local._botogram_context.pop()
41+
42+
def component_name(self):
43+
"""Get the name of the current component"""
44+
return self.hook.component.component_name
45+
46+
def chat(self):
47+
"""Get the current chat"""
48+
if self.update:
49+
return self.update.chat()
50+
51+
52+
def ctx():
53+
"""Get the current context"""
54+
if _local._botogram_context:
55+
return _local._botogram_context[-1]

botogram/hooks.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import re
2121

2222
from .callbacks import hashed_callback_name
23+
from .context import Context
2324

2425

2526
class Hook:
@@ -53,9 +54,10 @@ def _after_init(self, args):
5354

5455
def call(self, bot, update):
5556
"""Call the hook"""
56-
if self._only_texts and update.message.text is None:
57-
return
58-
return self._call(bot, update)
57+
with Context(bot, self, update):
58+
if self._only_texts and update.message.text is None:
59+
return
60+
return self._call(bot, update)
5961

6062
def _call(self, bot, update):
6163
"""*Actually* call the hook"""
@@ -217,28 +219,30 @@ def _after_init(self, args):
217219
)
218220

219221
def call(self, bot, update, name, data):
220-
if not update.callback_query:
221-
return
222-
q = update.callback_query
222+
with Context(bot, self, update):
223+
if not update.callback_query:
224+
return
225+
q = update.callback_query
223226

224-
if name != self._name:
225-
return
227+
if name != self._name:
228+
return
226229

227-
bot._call(
228-
self.func, self.component_id, query=q, chat=q.message.chat,
229-
message=q.message, data=data,
230-
)
230+
bot._call(
231+
self.func, self.component_id, query=q, chat=q.message.chat,
232+
message=q.message, data=data,
233+
)
231234

232-
update.callback_query._maybe_send_noop()
233-
return True
235+
update.callback_query._maybe_send_noop()
236+
return True
234237

235238

236239
class ChatUnavailableHook(Hook):
237240
"""Underlying hook for @bot.chat_unavailable"""
238241

239242
def call(self, bot, chat_id, reason):
240-
return bot._call(self.func, self.component_id, chat_id=chat_id,
241-
reason=reason)
243+
with Context(bot, self, None):
244+
return bot._call(self.func, self.component_id, chat_id=chat_id,
245+
reason=reason)
242246

243247

244248
class MessageEditedHook(Hook):
@@ -272,4 +276,5 @@ class TimerHook(Hook):
272276
"""Underlying hook for a timer"""
273277

274278
def call(self, bot):
275-
return bot._call(self.func, self.component_id)
279+
with Context(bot, self, None):
280+
return bot._call(self.func, self.component_id)

botogram/objects/updates.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Update(BaseObject):
3030
https://core.telegram.org/bots/api#update
3131
"""
3232

33+
# Please update the chat method below when adding new types, thanks!
34+
3335
required = {
3436
"update_id": int,
3537
}
@@ -42,6 +44,25 @@ class Update(BaseObject):
4244
}
4345
_check_equality_ = "update_id"
4446

47+
def chat(self):
48+
"""Get the chat related to this update"""
49+
if self.message is not None:
50+
return self.message.chat
51+
52+
if self.edited_message is not None:
53+
return self.edited_message.chat
54+
55+
if self.channel_post is not None:
56+
return self.channel_post.chat
57+
58+
if self.edited_channel_post is not None:
59+
return self.edited_channel_post.chat
60+
61+
if self.callback_query is not None:
62+
return self.callback_query.message.chat
63+
64+
raise NotImplementedError
65+
4566

4667
# Shortcut for the Updates type
4768
Updates = multiple(Update)

0 commit comments

Comments
 (0)