{IrcMiRCARTBot,MiRCART}.py: differentiate private class member functions.

This commit is contained in:
Lucio Andrés Illanes Albornoz 2018-01-03 03:33:57 +01:00
parent 9896108028
commit e182f0ffa9
3 changed files with 102 additions and 102 deletions

View File

@ -32,6 +32,12 @@ class IrcClient:
clientSocket = clientSocketFile = None; clientSocket = clientSocketFile = None;
clientNextTimeout = None clientNextTimeout = None
# {{{ close(): Close connection to server
def close(self):
if self.clientSocket != None:
self.clientSocket.close()
self.clientSocket = self.clientSocketFile = None;
# }}}
# {{{ connect(): Connect to server and register w/ optional timeout # {{{ connect(): Connect to server and register w/ optional timeout
def connect(self, timeout=None): def connect(self, timeout=None):
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -51,12 +57,6 @@ class IrcClient:
self.sendline("USER", self.clientIdent, "0", "0", self.clientGecos) self.sendline("USER", self.clientIdent, "0", "0", self.clientGecos)
return True return True
# }}} # }}}
# {{{ close(): Close connection to server
def close(self):
if self.clientSocket != None:
self.clientSocket.close()
self.clientSocket = self.clientSocketFile = None;
# }}}
# {{{ readline(): Read and parse single line from server into canonicalised list, honouring timers # {{{ readline(): Read and parse single line from server into canonicalised list, honouring timers
def readline(self): def readline(self):
if self.clientNextTimeout: if self.clientNextTimeout:

View File

@ -33,31 +33,14 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
clientChannelLastMessage = clientChannelOps = clientChannel = None clientChannelLastMessage = clientChannelOps = clientChannel = None
clientChannelRejoin = None clientChannelRejoin = None
# {{{ connect(): Connect to server and (re)initialise w/ optional timeout # {{{ _dispatch001(): Dispatch single 001 (RPL_WELCOME)
def connect(self, timeout=None): def _dispatch001(self, message):
self._log("Connecting to {}:{}...".format(self.serverHname, self.serverPort))
if super().connect(timeout):
self._log("Connected to {}:{}.".format(self.serverHname, self.serverPort))
self._log("Registering on {}:{} as {}, {}, {}...".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
self.clientLastMessage = 0; self.clientChannelOps = [];
self.clientChannelRejoin = False
return True
else:
return False
# }}}
# {{{ dispatchNone(): Dispatch None message from server
def dispatchNone(self):
self._log("Disconnected from {}:{}.".format(self.serverHname, self.serverPort))
self.close()
# }}}
# {{{ dispatch001(): Dispatch single 001 (RPL_WELCOME)
def dispatch001(self, message):
self._log("Registered on {}:{} as {}, {}, {}.".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos)) self._log("Registered on {}:{} as {}, {}, {}.".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
self._log("Attempting to join {} on {}:{}...".format(self.clientChannel, self.serverHname, self.serverPort)) self._log("Attempting to join {} on {}:{}...".format(self.clientChannel, self.serverHname, self.serverPort))
self.sendline("JOIN", self.clientChannel) self.sendline("JOIN", self.clientChannel)
# }}} # }}}
# {{{ dispatch353(): Dispatch single 353 (RPL_NAMREPLY) # {{{ _dispatch353(): Dispatch single 353 (RPL_NAMREPLY)
def dispatch353(self, message): def _dispatch353(self, message):
if message[4].lower() == self.clientChannel.lower(): if message[4].lower() == self.clientChannel.lower():
for channelNickSpec in message[5].split(" "): for channelNickSpec in message[5].split(" "):
if len(channelNickSpec) \ if len(channelNickSpec) \
@ -66,20 +49,20 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
self.clientChannelOps.append(channelNickSpec[1:].lower()) self.clientChannelOps.append(channelNickSpec[1:].lower())
self._log("Authorising {} on {}".format(channelNickSpec[1:].lower(), message[4].lower())) self._log("Authorising {} on {}".format(channelNickSpec[1:].lower(), message[4].lower()))
# }}} # }}}
# {{{ dispatchJoin(): Dispatch single JOIN message from server # {{{ _dispatchJoin(): Dispatch single JOIN message from server
def dispatchJoin(self, message): def _dispatchJoin(self, message):
self._log("Joined {} on {}:{}.".format(message[2].lower(), self.serverHname, self.serverPort)) self._log("Joined {} on {}:{}.".format(message[2].lower(), self.serverHname, self.serverPort))
self.clientNextTimeout = None; self.clientChannelRejoin = False; self.clientNextTimeout = None; self.clientChannelRejoin = False;
# }}} # }}}
# {{{ dispatchKick(): Dispatch single KICK message from server # {{{ _dispatchKick(): Dispatch single KICK message from server
def dispatchKick(self, message): def _dispatchKick(self, message):
if message[2].lower() == self.clientChannel.lower() \ if message[2].lower() == self.clientChannel.lower() \
and message[3].lower() == self.clientNick.lower(): and message[3].lower() == self.clientNick.lower():
self._log("Kicked from {} by {}, rejoining in 15 seconds".format(message[2].lower(), message[0])) self._log("Kicked from {} by {}, rejoining in 15 seconds".format(message[2].lower(), message[0]))
self.clientNextTimeout = time.time() + 15; self.clientChannelRejoin = True; self.clientNextTimeout = time.time() + 15; self.clientChannelRejoin = True;
# }}} # }}}
# {{{ dispatchMode(): Dispatch single MODE message from server # {{{ _dispatchMode(): Dispatch single MODE message from server
def dispatchMode(self, message): def _dispatchMode(self, message):
if message[2].lower() == self.clientChannel.lower(): if message[2].lower() == self.clientChannel.lower():
channelModeType = "+"; channelModeArg = 4; channelModeType = "+"; channelModeArg = 4;
channelAuthAdd = ""; channelAuthDel = ""; channelAuthAdd = ""; channelAuthDel = "";
@ -106,12 +89,17 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
self._log("Deauthorising {} on {}".format(channelAuthDel, message[2].lower())) self._log("Deauthorising {} on {}".format(channelAuthDel, message[2].lower()))
self.clientChannelOps.remove(channelAuthDel) self.clientChannelOps.remove(channelAuthDel)
# }}} # }}}
# {{{ dispatchPing(): Dispatch single PING message from server # {{{ _dispatchNone(): Dispatch None message from server
def dispatchPing(self, message): def _dispatchNone(self):
self._log("Disconnected from {}:{}.".format(self.serverHname, self.serverPort))
self.close()
# }}}
# {{{ _dispatchPing(): Dispatch single PING message from server
def _dispatchPing(self, message):
self.sendline("PONG", message[2]) self.sendline("PONG", message[2])
# }}} # }}}
# {{{ dispatchPrivmsg(): Dispatch single PRIVMSG message from server # {{{ _dispatchPrivmsg(): Dispatch single PRIVMSG message from server
def dispatchPrivmsg(self, message): def _dispatchPrivmsg(self, message):
if message[2].lower() == self.clientChannel.lower() \ if message[2].lower() == self.clientChannel.lower() \
and message[3].startswith("!pngbot "): and message[3].startswith("!pngbot "):
if (int(time.time()) - self.clientLastMessage) < 45: if (int(time.time()) - self.clientLastMessage) < 45:
@ -131,7 +119,7 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
os.remove(imgTmpFilePath) os.remove(imgTmpFilePath)
urllib.request.urlretrieve(asciiUrl, asciiTmpFilePath) urllib.request.urlretrieve(asciiUrl, asciiTmpFilePath)
_MiRCART = mirc2png.MiRCART(asciiTmpFilePath, imgTmpFilePath, "DejaVuSansMono.ttf", 11) _MiRCART = mirc2png.MiRCART(asciiTmpFilePath, imgTmpFilePath, "DejaVuSansMono.ttf", 11)
imgurResponse = self.uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", "c9a6efb3d7932fd") imgurResponse = self._uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", "c9a6efb3d7932fd")
if imgurResponse[0] == 200: if imgurResponse[0] == 200:
self._log("Uploaded as: {}".format(imgurResponse[1])) self._log("Uploaded as: {}".format(imgurResponse[1]))
self.sendline("PRIVMSG", message[2], "8/!\\ Uploaded as: {}".format(imgurResponse[1])) self.sendline("PRIVMSG", message[2], "8/!\\ Uploaded as: {}".format(imgurResponse[1]))
@ -143,46 +131,19 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
if os.path.isfile(imgTmpFilePath): if os.path.isfile(imgTmpFilePath):
os.remove(imgTmpFilePath) os.remove(imgTmpFilePath)
# }}} # }}}
# {{{ dispatchTimer(): Dispatch single client timer expiration # {{{ _dispatchTimer(): Dispatch single client timer expiration
def dispatchTimer(self): def _dispatchTimer(self):
if self.clientChannelRejoin: if self.clientChannelRejoin:
self._log("Attempting to join {} on {}:{}...".format(self.clientChannel, self.serverHname, self.serverPort)) self._log("Attempting to join {} on {}:{}...".format(self.clientChannel, self.serverHname, self.serverPort))
self.sendline("JOIN", self.clientChannel) self.sendline("JOIN", self.clientChannel)
self.clientNextTimeout = time.time() + 15; self.clientChannelRejoin = True; self.clientNextTimeout = time.time() + 15; self.clientChannelRejoin = True;
# }}} # }}}
# {{{ dispatch(): Read, parse, and dispatch single line from server # {{{ _log(): Log single message to stdout w/ timestamp
def dispatch(self):
while True:
if self.clientNextTimeout:
timeNow = time.time()
if self.clientNextTimeout <= timeNow:
self.dispatchTimer()
serverMessage = self.readline()
if serverMessage == None:
self.dispatchNone(); break;
elif serverMessage == "":
continue
elif serverMessage[1] == "001":
self.dispatch001(serverMessage)
elif serverMessage[1] == "353":
self.dispatch353(serverMessage)
elif serverMessage[1] == "JOIN":
self.dispatchJoin(serverMessage)
elif serverMessage[1] == "KICK":
self.dispatchKick(serverMessage)
elif serverMessage[1] == "MODE":
self.dispatchMode(serverMessage)
elif serverMessage[1] == "PING":
self.dispatchPing(serverMessage)
elif serverMessage[1] == "PRIVMSG":
self.dispatchPrivmsg(serverMessage)
# }}}
# {{{ self._log(): Log single message to stdout w/ timestamp
def _log(self, msg): def _log(self, msg):
print(time.strftime("%Y/%m/%d %H:%M:%S") + " " + msg) print(time.strftime("%Y/%m/%d %H:%M:%S") + " " + msg)
# }}} # }}}
# {{{ uploadToImgur(): Upload single file to Imgur # {{{ _uploadToImgur(): Upload single file to Imgur
def uploadToImgur(self, imgFilePath, imgName, imgTitle, apiKey): def _uploadToImgur(self, imgFilePath, imgName, imgTitle, apiKey):
requestImageData = open(imgFilePath, "rb").read() requestImageData = open(imgFilePath, "rb").read()
requestData = { \ requestData = { \
"image": base64.b64encode(requestImageData), \ "image": base64.b64encode(requestImageData), \
@ -199,6 +160,45 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
else: else:
return [responseHttp.status_code] return [responseHttp.status_code]
# }}} # }}}
# {{{ connect(): Connect to server and (re)initialise w/ optional timeout
def connect(self, timeout=None):
self._log("Connecting to {}:{}...".format(self.serverHname, self.serverPort))
if super().connect(timeout):
self._log("Connected to {}:{}.".format(self.serverHname, self.serverPort))
self._log("Registering on {}:{} as {}, {}, {}...".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
self.clientLastMessage = 0; self.clientChannelOps = [];
self.clientChannelRejoin = False
return True
else:
return False
# }}}
# {{{ dispatch(): Read, parse, and dispatch single line from server
def dispatch(self):
while True:
if self.clientNextTimeout:
timeNow = time.time()
if self.clientNextTimeout <= timeNow:
self._dispatchTimer()
serverMessage = self.readline()
if serverMessage == None:
self._dispatchNone(); break;
elif serverMessage == "":
continue
elif serverMessage[1] == "001":
self._dispatch001(serverMessage)
elif serverMessage[1] == "353":
self._dispatch353(serverMessage)
elif serverMessage[1] == "JOIN":
self._dispatchJoin(serverMessage)
elif serverMessage[1] == "KICK":
self._dispatchKick(serverMessage)
elif serverMessage[1] == "MODE":
self._dispatchMode(serverMessage)
elif serverMessage[1] == "PING":
self._dispatchPing(serverMessage)
elif serverMessage[1] == "PRIVMSG":
self._dispatchPrivmsg(serverMessage)
# }}}
# #
# Initialisation method # Initialisation method

View File

@ -41,8 +41,8 @@ class MiRCART:
state = None; state = None;
inCurCol = None; inCurCol = None;
# {{{ ColourMapBold: mIRC colour number to RGBA map given ^B (bold) # {{{ _ColourMapBold: mIRC colour number to RGBA map given ^B (bold)
ColourMapBold = [ _ColourMapBold = [
(255, 255, 255, 255), # White (255, 255, 255, 255), # White
(85, 85, 85, 255), # Grey (85, 85, 85, 255), # Grey
(85, 85, 255, 255), # Light Blue (85, 85, 255, 255), # Light Blue
@ -61,8 +61,8 @@ class MiRCART:
(255, 255, 255, 255), # White (255, 255, 255, 255), # White
] ]
# }}} # }}}
# {{{ ColourMapNormal: mIRC colour number to RGBA map given none of ^[BFV_] (bold, italic, reverse, underline) # {{{ _ColourMapNormal: mIRC colour number to RGBA map given none of ^[BFV_] (bold, italic, reverse, underline)
ColourMapNormal = [ _ColourMapNormal = [
(255, 255, 255, 255), # White (255, 255, 255, 255), # White
(0, 0, 0, 255), # Black (0, 0, 0, 255), # Black
(0, 0, 187, 255), # Blue (0, 0, 187, 255), # Blue
@ -81,41 +81,41 @@ class MiRCART:
(187, 187, 187, 255), # Light Grey (187, 187, 187, 255), # Light Grey
] ]
# }}} # }}}
# {{{ State: Parsing loop state # {{{ _State: Parsing loop state
class State(Enum): class _State(Enum):
STATE_CHAR = 1 STATE_CHAR = 1
STATE_COLOUR_SPEC = 2 STATE_COLOUR_SPEC = 2
# }}} # }}}
# {{{ getMaxCols(): Calculate widest row in lines, ignoring non-printable & mIRC control code sequences # {{{ _getMaxCols(): Calculate widest row in lines, ignoring non-printable & mIRC control code sequences
def getMaxCols(self, lines): def _getMaxCols(self, lines):
maxCols = 0; maxCols = 0;
for curRow in range(0, len(lines)): for curRow in range(0, len(lines)):
curRowCols = 0; curState = self.State.STATE_CHAR; curRowCols = 0; curState = self._State.STATE_CHAR;
curCol = 0; curColLen = len(lines[curRow]); curCol = 0; curColLen = len(lines[curRow]);
while curCol < curColLen: while curCol < curColLen:
curChar = lines[curRow][curCol] curChar = lines[curRow][curCol]
if curState == self.State.STATE_CHAR: if curState == self._State.STATE_CHAR:
if curChar == "": if curChar == "":
curState = self.State.STATE_COLOUR_SPEC; curCol += 1; curState = self._State.STATE_COLOUR_SPEC; curCol += 1;
elif curChar in string.printable: elif curChar in string.printable:
curRowCols += 1; curCol += 1; curRowCols += 1; curCol += 1;
else: else:
curCol += 1; curCol += 1;
elif curState == self.State.STATE_COLOUR_SPEC: elif curState == self._State.STATE_COLOUR_SPEC:
if curChar in set(",0123456789"): if curChar in set(",0123456789"):
curCol += 1; curCol += 1;
else: else:
curState = self.State.STATE_CHAR; curState = self._State.STATE_CHAR;
maxCols = max(maxCols, curRowCols) maxCols = max(maxCols, curRowCols)
return maxCols return maxCols
# }}} # }}}
# {{{ parseAsChar(): Parse single character as regular character and mutate state # {{{ _parseAsChar(): Parse single character as regular character and mutate state
def parseAsChar(self, char): def _parseAsChar(self, char):
if char == "": if char == "":
self.inCurCol += 1; self.inCurBold = 0 if self.inCurBold else 1; self.inCurCol += 1; self.inCurBold = 0 if self.inCurBold else 1;
elif char == "": elif char == "":
self.state = self.State.STATE_COLOUR_SPEC; self.inCurCol += 1; self._State = self._State.STATE_COLOUR_SPEC; self.inCurCol += 1;
elif char == "": elif char == "":
self.inCurCol += 1; self.inCurItalic = 0 if self.inCurItalic else 1; self.inCurCol += 1; self.inCurItalic = 0 if self.inCurItalic else 1;
elif char == "": elif char == "":
@ -129,20 +129,20 @@ class MiRCART:
self.inCurCol += 1; self.inCurUnderline = 0 if self.inCurUnderline else 1; self.inCurCol += 1; self.inCurUnderline = 0 if self.inCurUnderline else 1;
elif char == " ": elif char == " ":
if self.inCurBold: if self.inCurBold:
colourBg = self.ColourMapBold[self.outCurColourBg] colourBg = self._ColourMapBold[self.outCurColourBg]
else: else:
colourBg = self.ColourMapNormal[self.outCurColourBg] colourBg = self._ColourMapNormal[self.outCurColourBg]
self.outImgDraw.rectangle(((self.outCurX, self.outCurY), (self.outCurX + 7, self.outCurY + 14)), fill=colourBg) self.outImgDraw.rectangle(((self.outCurX, self.outCurY), (self.outCurX + 7, self.outCurY + 14)), fill=colourBg)
if self.inCurUnderline: if self.inCurUnderline:
self.outImgDraw.line((self.outCurX, self.outCurY + 11, self.outCurX + 7, self.outCurY + 11), fill=colourFg) self.outImgDraw.line((self.outCurX, self.outCurY + 11, self.outCurX + 7, self.outCurY + 11), fill=colourFg)
self.outCurX += 7; self.inCurCol += 1; self.outCurX += 7; self.inCurCol += 1;
else: else:
if self.inCurBold: if self.inCurBold:
colourBg = self.ColourMapBold[self.outCurColourBg] colourBg = self._ColourMapBold[self.outCurColourBg]
colourFg = self.ColourMapBold[self.outCurColourFg] colourFg = self._ColourMapBold[self.outCurColourFg]
else: else:
colourBg = self.ColourMapNormal[self.outCurColourBg] colourBg = self._ColourMapNormal[self.outCurColourBg]
colourFg = self.ColourMapNormal[self.outCurColourFg] colourFg = self._ColourMapNormal[self.outCurColourFg]
self.outImgDraw.rectangle(((self.outCurX, self.outCurY), (self.outCurX + 7, self.outCurY + 14)), fill=colourBg) self.outImgDraw.rectangle(((self.outCurX, self.outCurY), (self.outCurX + 7, self.outCurY + 14)), fill=colourBg)
# XXX implement italic # XXX implement italic
self.outImgDraw.text((self.outCurX, self.outCurY), char, colourFg, self.outImgFont) self.outImgDraw.text((self.outCurX, self.outCurY), char, colourFg, self.outImgFont)
@ -150,8 +150,8 @@ class MiRCART:
self.outImgDraw.line((self.outCurX, self.outCurY + 11, self.outCurX + 7, self.outCurY + 11), fill=colourFg) self.outImgDraw.line((self.outCurX, self.outCurY + 11, self.outCurX + 7, self.outCurY + 11), fill=colourFg)
self.outCurX += 7; self.inCurCol += 1; self.outCurX += 7; self.inCurCol += 1;
# }}} # }}}
# {{{ parseAsColourSpec(): Parse single character as mIRC colour control code sequence and mutate state # {{{ _parseAsColourSpec(): Parse single character as mIRC colour control code sequence and mutate state
def parseAsColourSpec(self, char): def _parseAsColourSpec(self, char):
if char in set(",0123456789"): if char in set(",0123456789"):
self.inCurColourSpec += char; self.inCurCol += 1; self.inCurColourSpec += char; self.inCurCol += 1;
else: else:
@ -163,7 +163,7 @@ class MiRCART:
self.outCurColourFg = int(self.inCurColourSpec[0]) self.outCurColourFg = int(self.inCurColourSpec[0])
else: else:
self.outCurColourBg = 1; self.outCurColourFg = 15; self.outCurColourBg = 1; self.outCurColourFg = 15;
self.inCurColourSpec = ""; self.state = self.State.STATE_CHAR; self.inCurColourSpec = ""; self._State = self._State.STATE_CHAR;
# }}} # }}}
# #
@ -171,23 +171,23 @@ class MiRCART:
def __init__(self, inFilePath, imgFilePath, fontFilePath="DejaVuSansMono.ttf", fontSize=11): def __init__(self, inFilePath, imgFilePath, fontFilePath="DejaVuSansMono.ttf", fontSize=11):
self.inFilePath = inFilePath; self.inFile = open(inFilePath, "r"); self.inFilePath = inFilePath; self.inFile = open(inFilePath, "r");
self.inLines = self.inFile.readlines() self.inLines = self.inFile.readlines()
self.inColsMax = self.getMaxCols(self.inLines) self.inColsMax = self._getMaxCols(self.inLines)
self.inRows = len(self.inLines) self.inRows = len(self.inLines)
self.outFontFilePath = fontFilePath; self.outFontSize = int(fontSize); self.outFontFilePath = fontFilePath; self.outFontSize = int(fontSize);
self.outImg = Image.new("RGBA", (self.inColsMax * 7, self.inRows * 14), self.ColourMapNormal[1]) self.outImg = Image.new("RGBA", (self.inColsMax * 7, self.inRows * 14), self._ColourMapNormal[1])
self.outImgDraw = ImageDraw.Draw(self.outImg) self.outImgDraw = ImageDraw.Draw(self.outImg)
self.outImgFont = ImageFont.truetype(self.outFontFilePath, self.outFontSize) self.outImgFont = ImageFont.truetype(self.outFontFilePath, self.outFontSize)
self.outCurColourBg = 1; self.outCurColourFg = 15; self.outCurColourBg = 1; self.outCurColourFg = 15;
self.outCurX = 0; self.outCurY = 0; self.outCurX = 0; self.outCurY = 0;
for inCurRow in range(0, len(self.inLines)): for inCurRow in range(0, len(self.inLines)):
self.inCurBold = 0; self.inCurItalic = 0; self.inCurUnderline = 0; self.inCurBold = 0; self.inCurItalic = 0; self.inCurUnderline = 0;
self.inCurColourSpec = ""; self.state = self.State.STATE_CHAR; self.inCurColourSpec = ""; self._State = self._State.STATE_CHAR;
self.inCurCol = 0; self.inCurCol = 0;
while self.inCurCol < len(self.inLines[inCurRow]): while self.inCurCol < len(self.inLines[inCurRow]):
if self.state == self.State.STATE_CHAR: if self._State == self._State.STATE_CHAR:
self.parseAsChar(self.inLines[inCurRow][self.inCurCol]) self._parseAsChar(self.inLines[inCurRow][self.inCurCol])
elif self.state == self.State.STATE_COLOUR_SPEC: elif self._State == self._State.STATE_COLOUR_SPEC:
self.parseAsColourSpec(self.inLines[inCurRow][self.inCurCol]) self._parseAsColourSpec(self.inLines[inCurRow][self.inCurCol])
self.outCurX = 0; self.outCurY += 13; self.outCurX = 0; self.outCurY += 13;
self.inFile.close(); self.inFile.close();
self.outImg.save(imgFilePath); self.outImg.save(imgFilePath);