Skip to content

Commit 39a66a9

Browse files
committed
Add connection reference to User and Channel objects
1 parent 24e1ea3 commit 39a66a9

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

plugins/chan_track.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import gc
88
import json
9+
import weakref
910
from collections import Mapping, Iterable, namedtuple
1011
from contextlib import suppress
1112
from numbers import Number
@@ -70,20 +71,36 @@ class KeyFoldWeakValueDict(KeyFoldMixin, WeakValueDictionary):
7071

7172

7273
class ChanDict(KeyFoldDict):
74+
def __init__(self, conn):
75+
"""
76+
:type conn: cloudbot.client.Client
77+
"""
78+
super().__init__()
79+
80+
self.conn = weakref.ref(conn)
81+
7382
def getchan(self, name):
7483
try:
7584
return self[name]
7685
except KeyError:
77-
self[name] = value = Channel(name)
86+
self[name] = value = Channel(name, self.conn())
7887
return value
7988

8089

8190
class UsersDict(KeyFoldWeakValueDict):
91+
def __init__(self, conn):
92+
"""
93+
:type conn: cloudbot.client.Client
94+
"""
95+
super().__init__()
96+
97+
self.conn = weakref.ref(conn)
98+
8299
def getuser(self, nick):
83100
try:
84101
return self[nick]
85102
except KeyError:
86-
self[nick] = value = User(nick)
103+
self[nick] = value = User(nick, self.conn())
87104
return value
88105

89106

@@ -109,14 +126,15 @@ class Member(MappingAttributeAdapter):
109126
def __init__(self, user, channel):
110127
self.user = user
111128
self.channel = channel
129+
self.conn = user.conn
112130
self.status = []
113131
super().__init__()
114132

115133
def add_status(self, status, sort=True):
116134
if status in self.status:
117135
logger.warning(
118-
"[chantrack] Attempted to add existing status to channel member: %s %s",
119-
self, status
136+
"[%s|chantrack] Attempted to add existing status to channel member: %s %s",
137+
self.conn.name, self, status
120138
)
121139
else:
122140
self.status.append(status)
@@ -126,8 +144,8 @@ def add_status(self, status, sort=True):
126144
def remove_status(self, status):
127145
if status not in self.status:
128146
logger.warning(
129-
"[chantrack] Attempted to remove status not set on member: %s %s",
130-
self, status
147+
"[%s|chantrack] Attempted to remove status not set on member: %s %s",
148+
self.conn.name, self, status
131149
)
132150
else:
133151
self.status.remove(status)
@@ -137,8 +155,13 @@ def sort_status(self):
137155
status.sort(key=attrgetter("level"), reverse=True)
138156
self.status = status
139157

140-
def __init__(self, name):
158+
def __init__(self, name, conn):
159+
"""
160+
:type name: str
161+
:type conn: cloudbot.client.Client
162+
"""
141163
self.name = name
164+
self.conn = weakref.proxy(conn)
142165
self.users = KeyFoldDict()
143166
self.receiving_names = False
144167
super().__init__()
@@ -161,8 +184,13 @@ def get_member(self, user, create=False):
161184

162185

163186
class User(MappingAttributeAdapter):
164-
def __init__(self, name):
187+
def __init__(self, name, conn):
188+
"""
189+
:type name: str
190+
:type conn: cloudbot.client.Client
191+
"""
165192
self.mask = Prefix(name)
193+
self.conn = weakref.proxy(conn)
166194
self.realname = None
167195
self._account = None
168196
self.server = None
@@ -226,15 +254,15 @@ def get_users(conn):
226254
:type conn: cloudbot.client.Client
227255
:rtype: UsersDict
228256
"""
229-
return conn.memory.setdefault("users", UsersDict())
257+
return conn.memory.setdefault("users", UsersDict(conn))
230258

231259

232260
def get_chans(conn):
233261
"""
234262
:type conn: cloudbot.client.Client
235263
:rtype: ChanDict
236264
"""
237-
return conn.memory.setdefault("chan_data", ChanDict())
265+
return conn.memory.setdefault("chan_data", ChanDict(conn))
238266

239267

240268
# endregion util functions
@@ -298,9 +326,7 @@ def clean_user_data(user):
298326
:type user: User
299327
"""
300328
for memb in user.channels.values():
301-
status = list(set(memb.status))
302-
status.sort(key=attrgetter("level"), reverse=True)
303-
memb.status = status
329+
memb.sort_status()
304330

305331

306332
def clean_chan_data(chan):

0 commit comments

Comments
 (0)