Skip to content

Commit bf66128

Browse files
authored
Merge pull request #695 from TotallyNotRobots/fix-attr-access
refactor: move to using attribute constants for hook attributes
2 parents e610191 + 68ebddc commit bf66128

File tree

5 files changed

+32
-30
lines changed

5 files changed

+32
-30
lines changed

cloudbot/bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from cloudbot.hook import Action
2424
from cloudbot.plugin import PluginManager
2525
from cloudbot.reloader import ConfigReloader, PluginReloader
26-
from cloudbot.util import async_util, database, formatting
26+
from cloudbot.util import CLIENT_ATTR, async_util, database, formatting
2727
from cloudbot.util.mapping import KeyFoldDict
2828

2929
logger = logging.getLogger("cloudbot")
@@ -328,7 +328,7 @@ def load_clients(self):
328328
continue
329329

330330
try:
331-
_type = obj._cloudbot_client # type: ignore
331+
_type = getattr(obj, CLIENT_ATTR)
332332
except AttributeError:
333333
continue
334334

cloudbot/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from typing import Any, Dict
66

77
from cloudbot.permissions import PermissionManager
8-
from cloudbot.util import async_util
8+
from cloudbot.util import CLIENT_ATTR, async_util
99

1010
logger = logging.getLogger("cloudbot")
1111

1212

1313
def client(_type):
1414
def _decorate(cls):
15-
cls._cloudbot_client = _type
15+
setattr(cls, CLIENT_ATTR, _type)
1616
return cls
1717

1818
return _decorate

cloudbot/util/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
ATTR_PREFIX = "_cloudbot"
44
HOOK_ATTR = ATTR_PREFIX + "_hook"
5+
CLIENT_ATTR = ATTR_PREFIX + "_client"
56
LOADED_ATTR = ATTR_PREFIX + "_loaded"

tests/core_tests/test_hook.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,27 @@ def test_hook_decorate():
6666
def f():
6767
raise NotImplementedError
6868

69-
assert f._cloudbot_hook["event"].types == {
69+
assert getattr(f, HOOK_ATTR)["event"].types == {
7070
EventType.message,
7171
EventType.notice,
7272
EventType.action,
7373
}
74-
assert f._cloudbot_hook["command"].aliases == {"test"}
75-
assert f._cloudbot_hook["irc_raw"].triggers == {"*", "PRIVMSG"}
76-
77-
assert "irc_out" in f._cloudbot_hook
78-
assert "on_start" in f._cloudbot_hook
79-
assert "on_stop" in f._cloudbot_hook
80-
assert "regex" in f._cloudbot_hook
81-
assert "periodic" in f._cloudbot_hook
82-
assert "perm_check" in f._cloudbot_hook
83-
assert "post_hook" in f._cloudbot_hook
84-
assert "on_connect" in f._cloudbot_hook
85-
assert "on_cap_available" in f._cloudbot_hook
86-
assert "on_cap_ack" in f._cloudbot_hook
87-
88-
assert len(f._cloudbot_hook["regex"].regexes) == 4
89-
assert f._cloudbot_hook["periodic"].interval == 20
74+
assert getattr(f, HOOK_ATTR)["command"].aliases == {"test"}
75+
assert getattr(f, HOOK_ATTR)["irc_raw"].triggers == {"*", "PRIVMSG"}
76+
77+
assert "irc_out" in getattr(f, HOOK_ATTR)
78+
assert "on_start" in getattr(f, HOOK_ATTR)
79+
assert "on_stop" in getattr(f, HOOK_ATTR)
80+
assert "regex" in getattr(f, HOOK_ATTR)
81+
assert "periodic" in getattr(f, HOOK_ATTR)
82+
assert "perm_check" in getattr(f, HOOK_ATTR)
83+
assert "post_hook" in getattr(f, HOOK_ATTR)
84+
assert "on_connect" in getattr(f, HOOK_ATTR)
85+
assert "on_cap_available" in getattr(f, HOOK_ATTR)
86+
assert "on_cap_ack" in getattr(f, HOOK_ATTR)
87+
88+
assert len(getattr(f, HOOK_ATTR)["regex"].regexes) == 4
89+
assert getattr(f, HOOK_ATTR)["periodic"].interval == 20
9090

9191
with pytest.raises(ValueError, match="Invalid command name test 123"):
9292
hook.command("test 123")(f)
@@ -107,13 +107,13 @@ def f():
107107
def sieve_func(_bot, _event, _hook):
108108
raise NotImplementedError
109109

110-
assert "sieve" in sieve_func._cloudbot_hook
110+
assert "sieve" in getattr(sieve_func, HOOK_ATTR)
111111

112112
@hook.sieve()
113113
def sieve_func2(_bot, _event, _hook):
114114
raise NotImplementedError
115115

116-
assert "sieve" in sieve_func2._cloudbot_hook
116+
assert "sieve" in getattr(sieve_func2, HOOK_ATTR)
117117

118118
@hook.on_connect()
119119
@hook.irc_out()
@@ -123,7 +123,7 @@ def sieve_func2(_bot, _event, _hook):
123123
def plain_dec(_bot, _event, _hook):
124124
raise NotImplementedError
125125

126-
assert sorted(plain_dec._cloudbot_hook.keys()) == [
126+
assert sorted(getattr(plain_dec, HOOK_ATTR).keys()) == [
127127
"irc_out",
128128
"on_connect",
129129
"on_start",
@@ -141,7 +141,7 @@ def test(bot):
141141
142142
foo"""
143143

144-
cmd_hook = test._cloudbot_hook["command"]
144+
cmd_hook = getattr(test, HOOK_ATTR)["command"]
145145
assert cmd_hook.doc == "<arg> - foo bar baz"
146146

147147
@hook.command()
@@ -150,14 +150,14 @@ def test1(bot):
150150
151151
foo"""
152152

153-
cmd_hook = test1._cloudbot_hook["command"]
153+
cmd_hook = getattr(test1, HOOK_ATTR)["command"]
154154
assert cmd_hook.doc == "<arg> - foo bar baz"
155155

156156
@hook.command()
157157
def test2(bot):
158158
"""<arg> - foo bar baz"""
159159

160-
cmd_hook = test2._cloudbot_hook["command"]
160+
cmd_hook = getattr(test2, HOOK_ATTR)["command"]
161161
assert cmd_hook.doc == "<arg> - foo bar baz"
162162

163163
@hook.command()
@@ -166,12 +166,12 @@ def test3(bot):
166166
<arg> - foo bar baz
167167
"""
168168

169-
cmd_hook = test3._cloudbot_hook["command"]
169+
cmd_hook = getattr(test3, HOOK_ATTR)["command"]
170170
assert cmd_hook.doc == "<arg> - foo bar baz"
171171

172172
@hook.command()
173173
def test4(bot):
174174
"""<arg> - foo bar baz"""
175175

176-
cmd_hook = test4._cloudbot_hook["command"]
176+
cmd_hook = getattr(test4, HOOK_ATTR)["command"]
177177
assert cmd_hook.doc == "<arg> - foo bar baz"

tests/core_tests/test_plugin_hooks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
)
4646
from cloudbot.plugin import Plugin
4747
from cloudbot.plugin_hooks import Hook, hook_name_to_plugin
48+
from cloudbot.util import HOOK_ATTR
4849
from tests.util.mock_bot import MockBot
4950

5051
DOC_RE = re.compile(r"^(?:[<{\[][^-]+?[>}\]][^-]+?)*?-\s.+$")
@@ -256,7 +257,7 @@ def make_plugin():
256257

257258

258259
def get_and_wrap_hook(func, hook_type):
259-
func_hook = func._cloudbot_hook[hook_type]
260+
func_hook = getattr(func, HOOK_ATTR)[hook_type]
260261
plugin = make_plugin()
261262

262263
_hook = hook_name_to_plugin(hook_type)(plugin, func_hook)

0 commit comments

Comments
 (0)