Refactor to run in both Python 2.7 and Python 3.6
Refactor to run in both Python 2.7 and Python 3.6. This is important as python 2.7 is end of life. I tried to edit as few things as possible in this commit to minimize conflicts which means the code could be prettier. Signed-off-by: Viktor Sjölind <viktor@sjolind.se>
This commit is contained in:
parent
2125b2d739
commit
96e6da2752
5 changed files with 80 additions and 74 deletions
92
irclib.py
92
irclib.py
|
@ -207,8 +207,8 @@ class IRC:
|
|||
incoming data, if there are any. If that seems boring, look
|
||||
at the process_forever method.
|
||||
"""
|
||||
sockets = map(lambda x: x._get_socket(), self.connections)
|
||||
sockets = filter(lambda x: x != None, sockets)
|
||||
sockets = [x._get_socket() for x in self.connections]
|
||||
sockets = [x for x in sockets if x != None]
|
||||
if sockets:
|
||||
(i, o, e) = select.select(sockets, [], [], timeout)
|
||||
self.process_data(i)
|
||||
|
@ -342,7 +342,7 @@ class Connection:
|
|||
self.irclibobj = irclibobj
|
||||
|
||||
def _get_socket():
|
||||
raise IRCError, "Not overridden"
|
||||
raise IRCError("Not overridden")
|
||||
|
||||
##############################
|
||||
### Convenience wrappers.
|
||||
|
@ -433,10 +433,10 @@ class ServerConnection(Connection):
|
|||
self.socket.connect((self.server, self.port))
|
||||
if ssl:
|
||||
self.ssl = socket.ssl(self.socket)
|
||||
except socket.error, x:
|
||||
except socket.error as x:
|
||||
self.socket.close()
|
||||
self.socket = None
|
||||
raise ServerConnectionError, "Couldn't connect to socket: %s" % x
|
||||
raise ServerConnectionError("Couldn't connect to socket: %s" % x)
|
||||
self.connected = 1
|
||||
if self.irclibobj.fn_to_add_socket:
|
||||
self.irclibobj.fn_to_add_socket(self.socket)
|
||||
|
@ -491,7 +491,7 @@ class ServerConnection(Connection):
|
|||
new_data = self.ssl.read(2**14)
|
||||
else:
|
||||
new_data = self.socket.recv(2**14)
|
||||
except socket.error, x:
|
||||
except socket.error as x:
|
||||
# The server hung up.
|
||||
self.disconnect("Connection reset by peer")
|
||||
return
|
||||
|
@ -500,14 +500,14 @@ class ServerConnection(Connection):
|
|||
self.disconnect("Connection reset by peer")
|
||||
return
|
||||
|
||||
lines = _linesep_regexp.split(self.previous_buffer + new_data)
|
||||
lines = _linesep_regexp.split(self.previous_buffer + new_data.decode())
|
||||
|
||||
# Save the last, unfinished line.
|
||||
self.previous_buffer = lines.pop()
|
||||
|
||||
for line in lines:
|
||||
if DEBUG:
|
||||
print "FROM SERVER:", line
|
||||
print("FROM SERVER:", line)
|
||||
|
||||
if not line:
|
||||
continue
|
||||
|
@ -561,7 +561,7 @@ class ServerConnection(Connection):
|
|||
command = "privnotice"
|
||||
|
||||
for m in messages:
|
||||
if type(m) is types.TupleType:
|
||||
if type(m) is tuple:
|
||||
if command in ["privmsg", "pubmsg"]:
|
||||
command = "ctcp"
|
||||
else:
|
||||
|
@ -569,15 +569,15 @@ class ServerConnection(Connection):
|
|||
|
||||
m = list(m)
|
||||
if DEBUG:
|
||||
print "command: %s, source: %s, target: %s, arguments: %s" % (
|
||||
command, prefix, target, m)
|
||||
print("command: %s, source: %s, target: %s, arguments: %s" % (
|
||||
command, prefix, target, m))
|
||||
self._handle_event(Event(command, prefix, target, m))
|
||||
if command == "ctcp" and m[0] == "ACTION":
|
||||
self._handle_event(Event("action", prefix, target, m[1:]))
|
||||
else:
|
||||
if DEBUG:
|
||||
print "command: %s, source: %s, target: %s, arguments: %s" % (
|
||||
command, prefix, target, [m])
|
||||
print("command: %s, source: %s, target: %s, arguments: %s" % (
|
||||
command, prefix, target, [m]))
|
||||
self._handle_event(Event(command, prefix, target, [m]))
|
||||
else:
|
||||
target = None
|
||||
|
@ -595,8 +595,8 @@ class ServerConnection(Connection):
|
|||
command = "umode"
|
||||
|
||||
if DEBUG:
|
||||
print "command: %s, source: %s, target: %s, arguments: %s" % (
|
||||
command, prefix, target, arguments)
|
||||
print("command: %s, source: %s, target: %s, arguments: %s" % (
|
||||
command, prefix, target, arguments))
|
||||
self._handle_event(Event(command, prefix, target, arguments))
|
||||
|
||||
def _handle_event(self, event):
|
||||
|
@ -660,7 +660,7 @@ class ServerConnection(Connection):
|
|||
|
||||
try:
|
||||
self.socket.close()
|
||||
except socket.error, x:
|
||||
except socket.error as x:
|
||||
pass
|
||||
self.socket = None
|
||||
self._handle_event(Event("disconnect", self.server, "", [message]))
|
||||
|
@ -743,7 +743,7 @@ class ServerConnection(Connection):
|
|||
|
||||
def part(self, channels, message=""):
|
||||
"""Send a PART command."""
|
||||
if type(channels) == types.StringType:
|
||||
if type(channels) == bytes:
|
||||
self.send_raw("PART " + channels + (message and (" " + message)))
|
||||
else:
|
||||
self.send_raw("PART " + ",".join(channels) + (message and (" " + message)))
|
||||
|
@ -782,15 +782,16 @@ class ServerConnection(Connection):
|
|||
The string will be padded with appropriate CR LF.
|
||||
"""
|
||||
if self.socket is None:
|
||||
raise ServerNotConnectedError, "Not connected."
|
||||
raise ServerNotConnectedError("Not connected.")
|
||||
try:
|
||||
string += "\r\n"
|
||||
if self.ssl:
|
||||
self.ssl.write(string + "\r\n")
|
||||
self.ssl.write(string.encode())
|
||||
else:
|
||||
self.socket.send(string + "\r\n")
|
||||
self.socket.send(string.encode())
|
||||
if DEBUG:
|
||||
print "TO SERVER:", string
|
||||
except socket.error, x:
|
||||
print("TO SERVER:", string)
|
||||
except socket.error as x:
|
||||
# Ouch!
|
||||
self.disconnect("Connection reset by peer.")
|
||||
|
||||
|
@ -888,8 +889,8 @@ class DCCConnection(Connection):
|
|||
self.passive = 0
|
||||
try:
|
||||
self.socket.connect((self.peeraddress, self.peerport))
|
||||
except socket.error, x:
|
||||
raise DCCConnectionError, "Couldn't connect to socket: %s" % x
|
||||
except socket.error as x:
|
||||
raise DCCConnectionError("Couldn't connect to socket: %s" % x)
|
||||
self.connected = 1
|
||||
if self.irclibobj.fn_to_add_socket:
|
||||
self.irclibobj.fn_to_add_socket(self.socket)
|
||||
|
@ -913,8 +914,8 @@ class DCCConnection(Connection):
|
|||
self.socket.bind((socket.gethostbyname(socket.gethostname()), 0))
|
||||
self.localaddress, self.localport = self.socket.getsockname()
|
||||
self.socket.listen(10)
|
||||
except socket.error, x:
|
||||
raise DCCConnectionError, "Couldn't bind socket: %s" % x
|
||||
except socket.error as x:
|
||||
raise DCCConnectionError("Couldn't bind socket: %s" % x)
|
||||
return self
|
||||
|
||||
def disconnect(self, message=""):
|
||||
|
@ -930,7 +931,7 @@ class DCCConnection(Connection):
|
|||
self.connected = 0
|
||||
try:
|
||||
self.socket.close()
|
||||
except socket.error, x:
|
||||
except socket.error as x:
|
||||
pass
|
||||
self.socket = None
|
||||
self.irclibobj._handle_event(
|
||||
|
@ -947,8 +948,8 @@ class DCCConnection(Connection):
|
|||
self.socket = conn
|
||||
self.connected = 1
|
||||
if DEBUG:
|
||||
print "DCC connection from %s:%d" % (
|
||||
self.peeraddress, self.peerport)
|
||||
print("DCC connection from %s:%d" % (
|
||||
self.peeraddress, self.peerport))
|
||||
self.irclibobj._handle_event(
|
||||
self,
|
||||
Event("dcc_connect", self.peeraddress, None, None))
|
||||
|
@ -956,7 +957,7 @@ class DCCConnection(Connection):
|
|||
|
||||
try:
|
||||
new_data = self.socket.recv(2**14)
|
||||
except socket.error, x:
|
||||
except socket.error as x:
|
||||
# The server hung up.
|
||||
self.disconnect("Connection reset by peer")
|
||||
return
|
||||
|
@ -985,11 +986,11 @@ class DCCConnection(Connection):
|
|||
target = None
|
||||
for chunk in chunks:
|
||||
if DEBUG:
|
||||
print "FROM PEER:", chunk
|
||||
print("FROM PEER:", chunk)
|
||||
arguments = [chunk]
|
||||
if DEBUG:
|
||||
print "command: %s, source: %s, target: %s, arguments: %s" % (
|
||||
command, prefix, target, arguments)
|
||||
print("command: %s, source: %s, target: %s, arguments: %s" % (
|
||||
command, prefix, target, arguments))
|
||||
self.irclibobj._handle_event(
|
||||
self,
|
||||
Event(command, prefix, target, arguments))
|
||||
|
@ -1009,8 +1010,8 @@ class DCCConnection(Connection):
|
|||
if self.dcctype == "chat":
|
||||
self.socket.send("\n")
|
||||
if DEBUG:
|
||||
print "TO PEER: %s\n" % string
|
||||
except socket.error, x:
|
||||
print("TO PEER: %s\n" % string)
|
||||
except socket.error as x:
|
||||
# Ouch!
|
||||
self.disconnect("Connection reset by peer.")
|
||||
|
||||
|
@ -1181,10 +1182,6 @@ def mask_matches(nick, mask):
|
|||
r = re.compile(mask, re.IGNORECASE)
|
||||
return r.match(nick)
|
||||
|
||||
_special = "-[]\\`^{}"
|
||||
nick_characters = string.ascii_letters + string.digits + _special
|
||||
_ircstring_translation = string.maketrans(string.ascii_uppercase + "[]\\^",
|
||||
string.ascii_lowercase + "{}|~")
|
||||
|
||||
def irc_lower(s):
|
||||
"""Returns a lowercased string.
|
||||
|
@ -1192,7 +1189,12 @@ def irc_lower(s):
|
|||
The definition of lowercased comes from the IRC specification (RFC
|
||||
1459).
|
||||
"""
|
||||
return s.translate(_ircstring_translation)
|
||||
s = s.lower()
|
||||
s = s.replace("[", "{")
|
||||
s = s.replace("]", "}")
|
||||
s = s.replace("\\", "|")
|
||||
s = s.replace("^", "~")
|
||||
return s
|
||||
|
||||
def _ctcp_dequote(message):
|
||||
"""[Internal] Dequote a message according to CTCP specifications.
|
||||
|
@ -1259,16 +1261,16 @@ def ip_numstr_to_quad(num):
|
|||
"""Convert an IP number as an integer given in ASCII
|
||||
representation (e.g. '3232235521') to an IP address string
|
||||
(e.g. '192.168.0.1')."""
|
||||
n = long(num)
|
||||
p = map(str, map(int, [n >> 24 & 0xFF, n >> 16 & 0xFF,
|
||||
n >> 8 & 0xFF, n & 0xFF]))
|
||||
n = int(num)
|
||||
p = list(map(str, list(map(int, [n >> 24 & 0xFF, n >> 16 & 0xFF,
|
||||
n >> 8 & 0xFF, n & 0xFF]))))
|
||||
return ".".join(p)
|
||||
|
||||
def ip_quad_to_numstr(quad):
|
||||
"""Convert an IP address string (e.g. '192.168.0.1') to an IP
|
||||
number as an integer given in ASCII representation
|
||||
(e.g. '3232235521')."""
|
||||
p = map(long, quad.split("."))
|
||||
p = list(map(int, quad.split(".")))
|
||||
s = str((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3])
|
||||
if s[-1] == "L":
|
||||
s = s[:-1]
|
||||
|
@ -1557,4 +1559,4 @@ protocol_events = [
|
|||
"pong",
|
||||
]
|
||||
|
||||
all_events = generated_events + protocol_events + numeric_events.values()
|
||||
all_events = generated_events + protocol_events + list(numeric_events.values())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue