Skip to content

Commit

Permalink
Use more descriptive variable names. Fixes #140.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed May 20, 2018
1 parent 5e2307e commit e8f6ed2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 58 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
16.3
====

* #140: Methods now use 'connection' and 'event' for parameter names.

16.2.1
======

Expand Down
74 changes: 37 additions & 37 deletions irc/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,48 +190,48 @@ def _connect(self):
except irc.client.ServerConnectionError:
pass

def _on_disconnect(self, c, e):
def _on_disconnect(self, connection, event):
self.channels = IRCDict()
self.recon.run(self)

def _on_join(self, c, e):
ch = e.target
nick = e.source.nick
if nick == c.get_nickname():
def _on_join(self, connection, event):
ch = event.target
nick = event.source.nick
if nick == connection.get_nickname():
self.channels[ch] = Channel()
self.channels[ch].add_user(nick)

def _on_kick(self, c, e):
nick = e.arguments[0]
channel = e.target
def _on_kick(self, connection, event):
nick = event.arguments[0]
channel = event.target

if nick == c.get_nickname():
if nick == connection.get_nickname():
del self.channels[channel]
else:
self.channels[channel].remove_user(nick)

def _on_mode(self, c, e):
t = e.target
def _on_mode(self, connection, event):
t = event.target
if not irc.client.is_channel(t):
# mode on self; disregard
return
ch = self.channels[t]

modes = irc.modes.parse_channel_modes(" ".join(e.arguments))
modes = irc.modes.parse_channel_modes(" ".join(event.arguments))
for sign, mode, argument in modes:
f = {"+": ch.set_mode, "-": ch.clear_mode}[sign]
f(mode, argument)

def _on_namreply(self, c, e):
def _on_namreply(self, connection, event):
"""
e.arguments[0] == "@" for secret channels,
event.arguments[0] == "@" for secret channels,
"*" for private channels,
"=" for others (public channels)
e.arguments[1] == channel
e.arguments[2] == nick list
event.arguments[1] == channel
event.arguments[2] == nick list
"""

ch_type, channel, nick_list = e.arguments
ch_type, channel, nick_list = event.arguments

if channel == '*':
# User is not in any visible channel
Expand All @@ -250,24 +250,24 @@ def _on_namreply(self, c, e):

self.channels[channel].add_user(nick)

def _on_nick(self, c, e):
before = e.source.nick
after = e.target
def _on_nick(self, connection, event):
before = event.source.nick
after = event.target
for ch in self.channels.values():
if ch.has_user(before):
ch.change_nick(before, after)

def _on_part(self, c, e):
nick = e.source.nick
channel = e.target
def _on_part(self, connection, event):
nick = event.source.nick
channel = event.target

if nick == c.get_nickname():
if nick == connection.get_nickname():
del self.channels[channel]
else:
self.channels[channel].remove_user(nick)

def _on_quit(self, c, e):
nick = e.source.nick
def _on_quit(self, connection, event):
nick = event.source.nick
for ch in self.channels.values():
if ch.has_user(nick):
ch.remove_user(nick)
Expand Down Expand Up @@ -314,24 +314,24 @@ def jump_server(self, msg="Changing servers"):
self.server_list.append(self.server_list.pop(0))
self._connect()

def on_ctcp(self, c, e):
def on_ctcp(self, connection, event):
"""Default handler for ctcp events.
Replies to VERSION and PING requests and relays DCC requests
to the on_dccchat method.
"""
nick = e.source.nick
if e.arguments[0] == "VERSION":
c.ctcp_reply(nick, "VERSION " + self.get_version())
elif e.arguments[0] == "PING":
if len(e.arguments) > 1:
c.ctcp_reply(nick, "PING " + e.arguments[1])
nick = event.source.nick
if event.arguments[0] == "VERSION":
connection.ctcp_reply(nick, "VERSION " + self.get_version())
elif event.arguments[0] == "PING":
if len(event.arguments) > 1:
connection.ctcp_reply(nick, "PING " + event.arguments[1])
elif (
e.arguments[0] == "DCC"
and e.arguments[1].split(" ", 1)[0] == "CHAT"):
self.on_dccchat(c, e)
event.arguments[0] == "DCC"
and event.arguments[1].split(" ", 1)[0] == "CHAT"):
self.on_dccchat(connection, event)

def on_dccchat(self, c, e):
def on_dccchat(self, connection, event):
pass

def start(self):
Expand Down
40 changes: 19 additions & 21 deletions irc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ def __init__(self, on_connect=__do_nothing, on_disconnect=__do_nothing):
def server(self):
"""Creates and returns a ServerConnection object."""

c = ServerConnection(self)
conn = ServerConnection(self)
with self.mutex:
self.connections.append(c)
return c
self.connections.append(conn)
return conn

def process_data(self, sockets):
"""Called when there is more data to read on connection sockets.
Expand All @@ -215,9 +215,9 @@ def process_data(self, sockets):
"""
with self.mutex:
log.log(logging.DEBUG - 2, "process_data()")
for s, c in itertools.product(sockets, self.connections):
if s == c.socket:
c.process_data()
for sock, conn in itertools.product(sockets, self.connections):
if sock == conn.socket:
conn.process_data()

def process_timeout(self):
"""Called when a timeout notification is due.
Expand Down Expand Up @@ -252,8 +252,8 @@ def process_once(self, timeout=0):
log.log(logging.DEBUG - 2, "process_once()")
sockets = self.sockets
if sockets:
(i, o, e) = select.select(sockets, [], [], timeout)
self.process_data(i)
in_, out, err = select.select(sockets, [], [], timeout)
self.process_data(in_)
else:
time.sleep(timeout)
self.process_timeout()
Expand All @@ -277,8 +277,8 @@ def process_forever(self, timeout=0.2):
def disconnect_all(self, message=""):
"""Disconnects all connections."""
with self.mutex:
for c in self.connections:
c.disconnect(message)
for conn in self.connections:
conn.disconnect(message)

def add_global_handler(self, event, handler, priority=0):
"""Adds a global handler function for a specific event type.
Expand Down Expand Up @@ -335,19 +335,18 @@ def dcc(self, dcctype="chat"):
chunks. If "raw", incoming data is not touched.
"""
with self.mutex:
c = DCCConnection(self, dcctype)
self.connections.append(c)
return c
conn = DCCConnection(self, dcctype)
self.connections.append(conn)
return conn

def _handle_event(self, connection, event):
"""
Handle an Event event incoming on ServerConnection connection.
"""
with self.mutex:
h = self.handlers
matching_handlers = sorted(
h.get("all_events", [])
+ h.get(event.type, [])
self.handlers.get("all_events", [])
+ self.handlers.get(event.type, [])
)
for handler in matching_handlers:
result = handler.callback(connection, event)
Expand Down Expand Up @@ -1155,13 +1154,13 @@ def _dispatcher(self, connection, event):
"""
log.debug("_dispatcher: %s", event.type)

def do_nothing(c, e):
def do_nothing(connection, event):
return None
method = getattr(self, "on_" + event.type, do_nothing)
method(connection, event)

def _dcc_disconnect(self, c, e):
self.dcc_connections.remove(c)
def _dcc_disconnect(self, connection, event):
self.dcc_connections.remove(connection)

def connect(self, *args, **kwargs):
"""Connect using the underlying connection"""
Expand Down Expand Up @@ -1258,8 +1257,7 @@ def ip_numstr_to_quad(num):
>>> ip_numstr_to_quad(3232235521)
'192.168.0.1'
"""
n = int(num)
packed = struct.pack('>L', n)
packed = struct.pack('>L', int(num))
bytes = struct.unpack('BBBB', packed)
return ".".join(map(str, bytes))

Expand Down

0 comments on commit e8f6ed2

Please sign in to comment.