Skip to content

Commit

Permalink
Prefer f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Mar 28, 2024
1 parent 8b42471 commit cc1e7d1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
8 changes: 5 additions & 3 deletions irc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,14 @@ def _multi_parameter(args):
def ctcp(self, ctcptype, target, parameter=""):
"""Send a CTCP command."""
ctcptype = ctcptype.upper()
tmpl = "\001{ctcptype} {parameter}\001" if parameter else "\001{ctcptype}\001"
self.privmsg(target, tmpl.format(**vars()))
self.privmsg(
target,
f"\001{ctcptype} {parameter}\001" if parameter else "\001{ctcptype}\001",
)

def ctcp_reply(self, target, parameter):
"""Send a CTCP REPLY command."""
self.notice(target, "\001%s\001" % parameter)
self.notice(target, f"\001{parameter}\001")

def disconnect(self, message=""):
"""Hang up the connection.
Expand Down
25 changes: 12 additions & 13 deletions irc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,11 @@ def _handle_line(self, line):
try:
log.debug(f'from {self.client_ident()}: {line}')
command, sep, params = line.partition(' ')
handler = getattr(self, 'handle_%s' % command.lower(), None)
handler = getattr(self, f'handle_{command.lower()}', None)
if not handler:
_tmpl = 'No handler for command: %s. Full line: %s'
log.info(_tmpl % (command, line))
log.info(f'No handler for command: {command}. Full line: {line}')
raise IRCError.from_name(
'unknowncommand', '%s :Unknown command' % command
'unknowncommand', f'{command} :Unknown command'
)
response = handler(params)
except AttributeError as e:
Expand Down Expand Up @@ -202,15 +201,15 @@ def handle_nick(self, params):

# Valid nickname?
if re.search(r'[^a-zA-Z0-9\-\[\]\'`^{}_]', nick):
raise IRCError.from_name('erroneusnickname', ':%s' % nick)
raise IRCError.from_name('erroneusnickname', f':{nick}')

if self.server.clients.get(nick, None) == self:
# Already registered to user
return

if nick in self.server.clients:
# Someone else is using the nick
raise IRCError.from_name('nicknameinuse', 'NICK :%s' % (nick))
raise IRCError.from_name('nicknameinuse', f'NICK :{nick}')

if not self.nick:
# New connection and nick is available; register and send welcome
Expand Down Expand Up @@ -280,7 +279,7 @@ def handle_join(self, params):
# Valid channel name?
if not re.match('^#([a-zA-Z0-9_])+$', r_channel_name):
raise IRCError.from_name(
'nosuchchannel', '%s :No such channel' % r_channel_name
'nosuchchannel', f'{r_channel_name} :No such channel'
)

# Add user to the channel (create new channel if not exists)
Expand Down Expand Up @@ -334,20 +333,20 @@ def _send_msg(self, cmd, params):
# Message to channel. Check if the channel exists.
channel = self.server.channels.get(target)
if not channel:
raise IRCError.from_name('nosuchnick', cmd + ' :%s' % target)
raise IRCError.from_name('nosuchnick', cmd + f' :{target}')

if channel.name not in self.channels:
# The user isn't in the channel.
raise IRCError.from_name(
'cannotsendtochan', '%s :Cannot send to channel' % channel.name
'cannotsendtochan', f'{channel.name} :Cannot send to channel'
)

self._send_to_others(message, channel)
else:
# Message to user
client = self.server.clients.get(target, None)
if not client:
raise IRCError.from_name('nosuchnick', cmd + ' :%s' % target)
raise IRCError.from_name('nosuchnick', cmd + f' :{target}')

client.send_queue.append(message)

Expand All @@ -368,11 +367,11 @@ def handle_topic(self, params):

channel = self.server.channels.get(channel_name)
if not channel:
raise IRCError.from_name('nosuchnick', 'PRIVMSG :%s' % channel_name)
raise IRCError.from_name('nosuchnick', f'PRIVMSG :{channel_name}')
if channel.name not in self.channels:
# The user isn't in the channel.
raise IRCError.from_name(
'cannotsendtochan', '%s :Cannot send to channel' % channel.name
'cannotsendtochan', f'{channel.name} :Cannot send to channel'
)

if topic:
Expand Down Expand Up @@ -454,7 +453,7 @@ def finish(self):
the client didn't properly close the connection with PART and QUIT.
"""
log.info('Client disconnected: %s', self.client_ident())
response = ':%s QUIT :EOF from client' % self.client_ident()
response = f':{self.client_ident()} QUIT :EOF from client'
for channel in self.channels.values():
if self in channel.clients:
# Client is gone without properly QUITing or PARTing this
Expand Down
2 changes: 1 addition & 1 deletion scripts/dccreceive.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def on_dccmsg(self, connection, event):

def on_dcc_disconnect(self, connection, event):
self.file.close()
print("Received file %s (%d bytes)." % (self.filename, self.received_bytes))
print(f"Received file {self.filename} ({self.received_bytes} bytes).")
self.connection.quit()

def on_disconnect(self, connection, event):
Expand Down
2 changes: 1 addition & 1 deletion scripts/dccsend.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def on_dcc_connect(self, connection, event):
self.send_chunk()

def on_dcc_disconnect(self, connection, event):
print("Sent file %s (%d bytes)." % (self.filename, self.filesize))
print(f"Sent file {self.filename} ({self.filesize} bytes).")
self.connection.quit()

def on_dccmsg(self, connection, event):
Expand Down
4 changes: 1 addition & 3 deletions scripts/servermap.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def on_endoflinks(connection, event):
else:
hubs = 0

print(
"%d servers (%d leaves and %d hubs)\n" % (len(links), len(links) - hubs, hubs)
)
print(f"{len(links)} servers ({len(links) - hubs} leaves and {hubs} hubs)\n")

print_tree(0, [], connection.get_server_name(), m)
connection.quit("Using irc.client.py")
Expand Down
3 changes: 1 addition & 2 deletions scripts/testbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ def do_command(self, e, cmd):
c.ctcp(
"DCC",
nick,
"CHAT chat %s %d"
% (ip_quad_to_numstr(dcc.localaddress), dcc.localport),
f"CHAT chat {ip_quad_to_numstr(dcc.localaddress)} {dcc.localport}",
)
else:
c.notice(nick, "Not understood: " + cmd)
Expand Down

0 comments on commit cc1e7d1

Please sign in to comment.