Skip to content

Commit c38e284

Browse files
committed
Add docstrings and typedocs
1 parent 39a66a9 commit c38e284

1 file changed

Lines changed: 109 additions & 7 deletions

File tree

plugins/chan_track.py

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,26 @@
1515

1616
import cloudbot.bot
1717
from cloudbot import hook
18+
from cloudbot.clients.irc import IrcClient
1819
from cloudbot.util import web
1920
from cloudbot.util.parsers.irc import Prefix
2021

2122
logger = cloudbot.bot.logger
2223

2324

2425
class WeakDict(dict):
25-
# Subclass dict to allow it to be weakly referenced
26+
"""
27+
A subclass of dict to allow it to be weakly referenced
28+
"""
2629
pass
2730

2831

2932
# noinspection PyUnresolvedReferences
3033
class KeyFoldMixin:
34+
"""
35+
A mixin for Mapping to allow for case-insensitive keys
36+
"""
37+
3138
def __contains__(self, item):
3239
return super().__contains__(item.casefold())
3340

@@ -41,15 +48,27 @@ def __delitem__(self, key):
4148
return super().__delitem__(key.casefold())
4249

4350
def pop(self, key, *args, **kwargs):
51+
"""
52+
Wraps `dict.pop`
53+
"""
4454
return super().pop(key.casefold(), *args, **kwargs)
4555

4656
def get(self, key, default=None):
57+
"""
58+
Wrap `dict.get`
59+
"""
4760
return super().get(key.casefold(), default)
4861

4962
def setdefault(self, key, default=None):
63+
"""
64+
Wrap `dict.setdefault`
65+
"""
5066
return super().setdefault(key.casefold(), default)
5167

5268
def update(self, mapping=None, **kwargs):
69+
"""
70+
Wrap `dict.update`
71+
"""
5372
if mapping is not None:
5473
if hasattr(mapping, 'keys'):
5574
for k in mapping.keys():
@@ -63,14 +82,24 @@ def update(self, mapping=None, **kwargs):
6382

6483

6584
class KeyFoldDict(KeyFoldMixin, dict):
85+
"""
86+
KeyFolded dict type
87+
"""
6688
pass
6789

6890

6991
class KeyFoldWeakValueDict(KeyFoldMixin, WeakValueDictionary):
92+
"""
93+
KeyFolded WeakValueDictionary
94+
"""
7095
pass
7196

7297

7398
class ChanDict(KeyFoldDict):
99+
"""
100+
Mapping for channels on a network
101+
"""
102+
74103
def __init__(self, conn):
75104
"""
76105
:type conn: cloudbot.client.Client
@@ -80,6 +109,9 @@ def __init__(self, conn):
80109
self.conn = weakref.ref(conn)
81110

82111
def getchan(self, name):
112+
"""
113+
:type name: str
114+
"""
83115
try:
84116
return self[name]
85117
except KeyError:
@@ -88,6 +120,10 @@ def getchan(self, name):
88120

89121

90122
class UsersDict(KeyFoldWeakValueDict):
123+
"""
124+
Mapping for users on a network
125+
"""
126+
91127
def __init__(self, conn):
92128
"""
93129
:type conn: cloudbot.client.Client
@@ -97,6 +133,9 @@ def __init__(self, conn):
97133
self.conn = weakref.ref(conn)
98134

99135
def getuser(self, nick):
136+
"""
137+
:type nick: str
138+
"""
100139
try:
101140
return self[nick]
102141
except KeyError:
@@ -105,6 +144,10 @@ def getuser(self, nick):
105144

106145

107146
class MappingAttributeAdapter:
147+
"""
148+
Map item lookups to attribute lookups
149+
"""
150+
108151
def __init__(self):
109152
self.data = {}
110153

@@ -122,7 +165,15 @@ def __setitem__(self, key, value):
122165

123166

124167
class Channel(MappingAttributeAdapter):
168+
"""
169+
Represents a channel and relevant data
170+
"""
171+
125172
class Member(MappingAttributeAdapter):
173+
"""
174+
Store a user's membership with the channel
175+
"""
176+
126177
def __init__(self, user, channel):
127178
self.user = user
128179
self.channel = channel
@@ -131,6 +182,11 @@ def __init__(self, user, channel):
131182
super().__init__()
132183

133184
def add_status(self, status, sort=True):
185+
"""
186+
Add a status to this membership
187+
:type status: plugins.core.server_info.Status
188+
:type sort: bool
189+
"""
134190
if status in self.status:
135191
logger.warning(
136192
"[%s|chantrack] Attempted to add existing status to channel member: %s %s",
@@ -142,6 +198,9 @@ def add_status(self, status, sort=True):
142198
self.sort_status()
143199

144200
def remove_status(self, status):
201+
"""
202+
:type status: plugins.core.server_info.Status
203+
"""
145204
if status not in self.status:
146205
logger.warning(
147206
"[%s|chantrack] Attempted to remove status not set on member: %s %s",
@@ -151,6 +210,9 @@ def remove_status(self, status):
151210
self.status.remove(status)
152211

153212
def sort_status(self):
213+
"""
214+
Ensure the status list is properly sorted
215+
"""
154216
status = list(set(self.status))
155217
status.sort(key=attrgetter("level"), reverse=True)
156218
self.status = status
@@ -184,6 +246,10 @@ def get_member(self, user, create=False):
184246

185247

186248
class User(MappingAttributeAdapter):
249+
"""
250+
Represent a user on a network
251+
"""
252+
187253
def __init__(self, name, conn):
188254
"""
189255
:type name: str
@@ -212,6 +278,9 @@ def join_channel(self, channel):
212278

213279
@property
214280
def account(self):
281+
"""
282+
The user's nickserv account
283+
"""
215284
return self._account
216285

217286
@account.setter
@@ -223,6 +292,9 @@ def account(self, value):
223292

224293
@property
225294
def nick(self):
295+
"""
296+
The user's nickname
297+
"""
226298
return self.mask.nick
227299

228300
@nick.setter
@@ -231,6 +303,9 @@ def nick(self, value):
231303

232304
@property
233305
def ident(self):
306+
"""
307+
The user's ident/username
308+
"""
234309
return self.mask.user
235310

236311
@ident.setter
@@ -239,6 +314,9 @@ def ident(self, value):
239314

240315
@property
241316
def host(self):
317+
"""
318+
The user's host/address
319+
"""
242320
return self.mask.host
243321

244322
@host.setter
@@ -269,18 +347,22 @@ def get_chans(conn):
269347

270348

271349
def update_chan_data(conn, chan):
350+
# type: (IrcClient, str) -> None
272351
"""
273-
:type conn: cloudbot.client.Client
274-
:type chan: str
352+
Start the process of updating channel data from /NAMES
353+
:param conn: The current connection
354+
:param chan: The channel to update
275355
"""
276356
chan_data = get_chans(conn).getchan(chan)
277357
chan_data.receiving_names = False
278358
conn.cmd("NAMES", chan)
279359

280360

281361
def update_conn_data(conn):
362+
# type: (IrcClient) -> None
282363
"""
283-
:type conn: cloudbot.client.Client
364+
Update all channel data for this connection
365+
:param conn: The connection to update
284366
"""
285367
for chan in set(conn.channels):
286368
update_chan_data(conn, chan)
@@ -298,6 +380,9 @@ def update_conn_data(conn):
298380

299381
@hook.on_cap_available(*SUPPORTED_CAPS)
300382
def do_caps():
383+
"""
384+
Request all available CAPs we support
385+
"""
301386
return True
302387

303388

@@ -316,7 +401,8 @@ def get_chan_data(bot):
316401
:type bot: cloudbot.bot.CloudBot
317402
"""
318403
for conn in bot.connections.values():
319-
if conn.connected:
404+
if conn.connected and conn.type == 'irc':
405+
assert isinstance(conn, IrcClient)
320406
init_chan_data(conn, False)
321407
update_conn_data(conn)
322408

@@ -377,6 +463,14 @@ def init_chan_data(conn, _clear=True):
377463

378464

379465
def parse_names_item(item, statuses, has_multi_prefix, has_userhost):
466+
"""
467+
Parse an entry from /NAMES
468+
:param item: The entry to parse
469+
:param statuses: Status prefixes on this network
470+
:param has_multi_prefix: Whether multi-prefix CAP is enabled
471+
:param has_userhost: Whether userhost-in-names CAP is enabled
472+
:return: The parsed data
473+
"""
380474
user_status = []
381475
while item[:1] in statuses:
382476
status, item = item[:1], item[1:]
@@ -445,6 +539,10 @@ def on_names(conn, irc_paramlist, irc_command):
445539

446540

447541
class MappingSerializer:
542+
"""
543+
Serialize generic mappings to json
544+
"""
545+
448546
def __init__(self):
449547
self._seen_objects = []
450548

@@ -475,6 +573,9 @@ def _serialize(self, obj):
475573
return repr(obj)
476574

477575
def serialize(self, mapping, **kwargs):
576+
"""
577+
Serialize mapping to JSON
578+
"""
478579
return json.dumps(self._serialize(mapping), **kwargs)
479580

480581

@@ -549,14 +650,15 @@ def clearusers(bot):
549650
:type bot: cloudbot.bot.CloudBot
550651
"""
551652
for conn in bot.connections.values():
552-
init_chan_data(conn, True)
653+
init_chan_data(conn)
553654

554655
gc.collect()
555656
return "Data cleared."
556657

557658

558659
@hook.command("getdata", permissions=["botcontrol"], autohelp=False)
559-
def getdata_cmd(conn, chan, text, nick):
660+
def getdata_cmd(conn, chan, nick):
661+
"""- Get data for current user"""
560662
chan_data = get_chans(conn).getchan(chan)
561663
user_data = get_users(conn).getuser(nick)
562664
memb = chan_data.get_member(user_data)

0 commit comments

Comments
 (0)