Use super() instead of parent class name

This commit is contained in:
nemunaire 2016-04-18 19:58:10 +02:00
parent fab747fcfd
commit 1e8cb3a12a
9 changed files with 80 additions and 37 deletions

View File

@ -22,7 +22,7 @@ class Command(Abstract):
"""This class represents a specialized TextMessage""" """This class represents a specialized TextMessage"""
def __init__(self, cmd, args=None, kwargs=None, *nargs, **kargs): def __init__(self, cmd, args=None, kwargs=None, *nargs, **kargs):
Abstract.__init__(self, *nargs, **kargs) super().__init__(*nargs, **kargs)
self.cmd = cmd self.cmd = cmd
self.args = args if args is not None else list() self.args = args if args is not None else list()

View File

@ -28,7 +28,7 @@ class DirectAsk(Text):
designated -- the user designated by the message designated -- the user designated by the message
""" """
Text.__init__(self, *args, **kargs) super().__init__(*args, **kargs)
self.designated = designated self.designated = designated

View File

@ -22,4 +22,4 @@ class IRC(SocketPrinter):
def visit_Text(self, msg): def visit_Text(self, msg):
self.pp += "PRIVMSG %s :" % ",".join(msg.to) self.pp += "PRIVMSG %s :" % ",".join(msg.to)
SocketPrinter.visit_Text(self, msg) super().visit_Text(msg)

View File

@ -0,0 +1,34 @@
# Nemubot is a smart and modulable IM bot.
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from nemubot.message.abstract import Abstract
class Response(Abstract):
def __init__(self, cmd, args=None, *nargs, **kargs):
super().__init__(*nargs, **kargs)
self.cmd = cmd
self.args = args if args is not None else list()
def __str__(self):
return self.cmd + " @" + ",@".join(self.args)
@property
def cmds(self):
# TODO: this is for legacy modules
return [self.cmd] + self.args

View File

@ -28,7 +28,7 @@ class Text(Abstract):
message -- the parsed message message -- the parsed message
""" """
Abstract.__init__(self, *args, **kargs) super().__init__(*args, **kargs)
self.message = message self.message = message

View File

@ -31,7 +31,7 @@ PORTS = list()
class DCC(server.AbstractServer): class DCC(server.AbstractServer):
def __init__(self, srv, dest, socket=None): def __init__(self, srv, dest, socket=None):
server.Server.__init__(self) super().__init__(self)
self.error = False # An error has occur, closing the connection? self.error = False # An error has occur, closing the connection?
self.messages = list() # Message queued before connexion self.messages = list() # Message queued before connexion

View File

@ -55,7 +55,7 @@ class IRC(SocketServer):
self.realname = realname self.realname = realname
self.id = self.username + "@" + host + ":" + str(port) self.id = self.username + "@" + host + ":" + str(port)
SocketServer.__init__(self, host=host, port=port, ssl=ssl) super().__init__(host=host, port=port, ssl=ssl)
self.printer = IRCPrinter self.printer = IRCPrinter
self.encoding = encoding self.encoding = encoding
@ -232,8 +232,8 @@ class IRC(SocketServer):
# Open/close # Open/close
def _open(self): def open(self):
if SocketServer._open(self): if super().open():
if self.password is not None: if self.password is not None:
self.write("PASS :" + self.password) self.write("PASS :" + self.password)
if self.capabilities is not None: if self.capabilities is not None:
@ -244,9 +244,10 @@ class IRC(SocketServer):
return False return False
def _close(self): def close(self):
if self.connected: self.write("QUIT") if not self.closed:
return SocketServer._close(self) self.write("QUIT")
return super().close()
# Writes: as inherited # Writes: as inherited
@ -254,7 +255,7 @@ class IRC(SocketServer):
# Read # Read
def read(self): def read(self):
for line in SocketServer.read(self): for line in super().read():
# PING should be handled here, so start parsing here :/ # PING should be handled here, so start parsing here :/
msg = IRCMessage(line, self.encoding) msg = IRCMessage(line, self.encoding)

View File

@ -32,6 +32,8 @@ class AbstractServer(io.IOBase):
send_callback -- Callback when developper want to send a message send_callback -- Callback when developper want to send a message
""" """
super().__init__()
if not hasattr(self, "id"): if not hasattr(self, "id"):
raise Exception("No id defined for this server. Please set one!") raise Exception("No id defined for this server. Please set one!")

View File

@ -26,7 +26,7 @@ class SocketServer(AbstractServer):
def __init__(self, sock_location=None, host=None, port=None, ssl=False, socket=None, id=None): def __init__(self, sock_location=None, host=None, port=None, ssl=False, socket=None, id=None):
if id is not None: if id is not None:
self.id = id self.id = id
AbstractServer.__init__(self) super().__init__()
if sock_location is not None: if sock_location is not None:
self.filename = sock_location self.filename = sock_location
elif host is not None: elif host is not None:
@ -44,18 +44,17 @@ class SocketServer(AbstractServer):
@property @property
def connected(self): def closed(self):
"""Indicator of the connection aliveness""" """Indicator of the connection aliveness"""
return self.socket is not None return self.socket is None
# Open/close # Open/close
def _open(self): def open(self):
import os
import socket import socket
if self.connected: if not self.closed:
return True return True
try: try:
@ -66,11 +65,14 @@ class SocketServer(AbstractServer):
else: else:
self.socket = socket.create_connection((self.host, self.port)) self.socket = socket.create_connection((self.host, self.port))
self.logger.info("Connected to %s:%d", self.host, self.port) self.logger.info("Connected to %s:%d", self.host, self.port)
except socket.error as e: except:
self.socket = None self.socket = None
self.logger.critical("Unable to connect to %s:%d: %s", if hasattr(self, "filename"):
self.host, self.port, self.logger.exception("Unable to connect to %s",
os.strerror(e.errno)) self.filename)
else:
self.logger.exception("Unable to connect to %s:%d",
self.host, self.port)
return False return False
# Wrap the socket for SSL # Wrap the socket for SSL
@ -79,17 +81,17 @@ class SocketServer(AbstractServer):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
self.socket = ctx.wrap_socket(self.socket) self.socket = ctx.wrap_socket(self.socket)
return True return super().open()
def _close(self): def close(self):
import socket import socket
from nemubot.server import _lock from nemubot.server import _lock
_lock.release() _lock.release()
self._sending_queue.join() self._sending_queue.join()
_lock.acquire() _lock.acquire()
if self.connected: if not self.closed:
try: try:
self.socket.shutdown(socket.SHUT_RDWR) self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close() self.socket.close()
@ -98,16 +100,16 @@ class SocketServer(AbstractServer):
self.socket = None self.socket = None
return True return super().close()
# Write # Write
def _write(self, cnt): def _write(self, cnt):
if not self.connected: if self.closed:
return return
self.socket.send(cnt) self.socket.sendall(cnt)
def format(self, txt): def format(self, txt):
@ -120,7 +122,7 @@ class SocketServer(AbstractServer):
# Read # Read
def read(self): def read(self):
if not self.connected: if self.closed:
return [] return []
raw = self.socket.recv(1024) raw = self.socket.recv(1024)
@ -147,7 +149,7 @@ class SocketListener(AbstractServer):
def __init__(self, new_server_cb, id, sock_location=None, host=None, port=None, ssl=None): def __init__(self, new_server_cb, id, sock_location=None, host=None, port=None, ssl=None):
self.id = id self.id = id
AbstractServer.__init__(self) super().__init__()
self.new_server_cb = new_server_cb self.new_server_cb = new_server_cb
self.sock_location = sock_location self.sock_location = sock_location
self.host = host self.host = host
@ -161,30 +163,31 @@ class SocketListener(AbstractServer):
@property @property
def connected(self): def closed(self):
"""Indicator of the connection aliveness""" """Indicator of the connection aliveness"""
return self.socket is not None return self.socket is None
def _open(self): def open(self):
import os import os
import socket import socket
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
if self.sock_location is not None: if self.sock_location is not None:
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try: try:
os.remove(self.sock_location) os.remove(self.sock_location)
except FileNotFoundError: except FileNotFoundError:
pass pass
self.socket.bind(self.sock_location) self.socket.bind(self.sock_location)
elif self.host is not None and self.port is not None: elif self.host is not None and self.port is not None:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((self.host, self.port)) self.socket.bind((self.host, self.port))
self.socket.listen(5) self.socket.listen(5)
return True return super().open()
def _close(self): def close(self):
import os import os
import socket import socket
@ -196,10 +199,13 @@ class SocketListener(AbstractServer):
except socket.error: except socket.error:
pass pass
return super().close()
# Read # Read
def read(self): def read(self):
if not self.connected: if self.closed:
return [] return []
conn, addr = self.socket.accept() conn, addr = self.socket.accept()