Added/Improved docstrings

This commit is contained in:
gpkvt 2022-08-27 15:44:57 +02:00
parent 1cf77afbcc
commit 41a7269855
1 changed files with 75 additions and 28 deletions

103
tts.py
View File

@ -131,7 +131,10 @@ class IRC:
sys.exit(251) sys.exit(251)
def resp_clearmsg(self, resp): def resp_clearmsg(self, resp):
""" Respond to CLEARMSG """ """ Respond to CLEARMSG
:param str resp: IRC Server message
"""
logging.info('CLEARMSG received') logging.info('CLEARMSG received')
msgid = False msgid = False
@ -154,7 +157,10 @@ class IRC:
MSG_QUEUE_RAW = filtered_msg_queue MSG_QUEUE_RAW = filtered_msg_queue
def resp_privmsg(self, resp): def resp_privmsg(self, resp):
""" Respond to PRIVMSG """ """ Respond to PRIVMSG
:param str resp: IRC Server message
"""
logging.debug('PRIVMSG received') logging.debug('PRIVMSG received')
tags = self.get_tags(resp) tags = self.get_tags(resp)
@ -207,7 +213,12 @@ class IRC:
return return
def get_tags(self, resp): def get_tags(self, resp):
""" Strip tags from response """ """ Strip tags from response
:param str resp: IRC Server message
:return dict tags: Message metadata (tags)
"""
tags = resp.split(';') tags = resp.split(';')
for tag in tags: for tag in tags:
@ -232,7 +243,12 @@ class IRC:
return tags return tags
def get_message(self, resp): def get_message(self, resp):
""" Process message """ """ Transform IRC server message and determine length
:param str resp: IRC Server message
:return dict msg: Processed message
"""
msg = {} msg = {}
msg['message'] = resp.rsplit('PRIVMSG #',1)[1].split(':',1)[1].replace('\r\n','') msg['message'] = resp.rsplit('PRIVMSG #',1)[1].split(':',1)[1].replace('\r\n','')
@ -241,7 +257,11 @@ class IRC:
return msg return msg
def priviledged_commands(self, message, tags): def priviledged_commands(self, message, tags):
""" Process priviledged commands """ """ Process priviledged commands
:param dict message: Message
:param dict tags: Message metadata (tags)
"""
msg = message['message'] msg = message['message']
badges = tags['badges'] badges = tags['badges']
@ -299,7 +319,12 @@ class IRC:
self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['TON']) self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['TON'])
def check_subonly(self, tags): def check_subonly(self, tags):
""" subonly """ """ Check if subonly mode is enabled and sender is sub
:param dict tags: Message metadata (tags)
:return bool:
"""
if not CONF['IRC_SUBONLY']: if not CONF['IRC_SUBONLY']:
return False return False
@ -317,7 +342,12 @@ class IRC:
return True return True
def check_modonly(self, tags): def check_modonly(self, tags):
""" modonly """ """ Check if modonly mode is enabled and sender is mod
:param dict tags: Message metadata (tags)
:return bool:
"""
if not CONF['IRC_MODONLY']: if not CONF['IRC_MODONLY']:
return False return False
@ -334,7 +364,12 @@ class IRC:
return True return True
def check_tts_disabled(self, user): def check_tts_disabled(self, user):
""" Check if TTS is disabled """ """ Check if TTS is disabled
:param str user: Username
:return bool:
"""
if not self.tts_status: if not self.tts_status:
self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['DISABLED']) self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['DISABLED'])
return True return True
@ -343,7 +378,13 @@ class IRC:
return False return False
def check_msg_too_long(self, message, user): def check_msg_too_long(self, message, user):
""" Check if message is too long """ """ Check if message is too long
:param dict message: Message
:param str user: Username
:return bool:
"""
if message['length'] > CONF['IRC_TTS_LEN']: if message['length'] > CONF['IRC_TTS_LEN']:
self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['TOO_LONG']) self.sendmsg(CONF['IRC_CHANNEL'], "@"+str(user), CONF['MESSAGE']['TOO_LONG'])
@ -358,6 +399,8 @@ class IRC:
Check if the given user is on the TTS blacklist Check if the given user is on the TTS blacklist
:param str user: Username :param str user: Username
:return bool:
""" """
if user in self.tts_denied: if user in self.tts_denied:
@ -374,6 +417,8 @@ class IRC:
Checks if the given user is on the TTS whitelist Checks if the given user is on the TTS whitelist
:param str user: Username :param str user: Username
:return bool:
""" """
if CONF['WHITELIST']: if CONF['WHITELIST']:
@ -423,11 +468,10 @@ class IRC:
MSG_QUEUE_RAW.append(msg) MSG_QUEUE_RAW.append(msg)
def get_response(self): def get_response(self):
"""Get and process response from IRC""" """ Get and process response from IRC """
try: try:
resp = self.irc.recv(2048).decode("UTF-8") resp = self.irc.recv(2048).decode("UTF-8")
logging.debug('resp:') logging.debug('resp: %s', resp)
logging.debug(resp)
except socket.timeout: except socket.timeout:
return return
except Exception: except Exception:
@ -731,7 +775,7 @@ class IRC:
def usermap(self, msg): def usermap(self, msg):
""" !usermap command """ !usermap command
Adds new entries to usermapping in config.yml Adds a new entry to usermapping in config.yml
:param str msg: The IRC message triggering the command :param str msg: The IRC message triggering the command
""" """
@ -756,7 +800,13 @@ class IRC:
load_config() load_config()
def pick(self, msg): def pick(self, msg):
""" !pick command """ """ !pick command
Pick a number of users who typed #pickme in the chat
after the pick command was started.
:param str msg: Number of users to pick
"""
if self.pick_status: if self.pick_status:
logging.info('Pick stopped') logging.info('Pick stopped')
@ -918,9 +968,8 @@ class IRC:
If no file is given in msg a standard file will be used If no file is given in msg a standard file will be used
:param str msg: The IRC message triggering the command :param str msg: The IRC message triggering the command
:raise: FileNotFoundError if randomfile does not exists
:return: True if line was successfully read and added to msg_queue :return bool:
:rtype: bool
""" """
randomfile = msg.replace('!random', '').strip().lower() randomfile = msg.replace('!random', '').strip().lower()
@ -974,8 +1023,6 @@ class IRC:
logging.info("Removing %s from deny list", user) logging.info("Removing %s from deny list", user)
self.tts_denied.remove(user) self.tts_denied.remove(user)
return
def dtts(self, msg): def dtts(self, msg):
""" !dtts command """ !dtts command
@ -998,20 +1045,18 @@ class IRC:
logging.info("Removing %s from allowed list", user) logging.info("Removing %s from allowed list", user)
self.tts_allowed.remove(user) self.tts_allowed.remove(user)
return
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer): class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
""" Threaded HTTP Server """ """ Threaded HTTP Server """
class HTTPserv(BaseHTTPRequestHandler): class HTTPserv(BaseHTTPRequestHandler):
"""Simple HTTP Server""" """ Simple HTTP Server """
def log_message(self, format, *args): # pylint: disable=redefined-builtin def log_message(self, format, *args): # pylint: disable=redefined-builtin
"""Suppress HTTP log messages""" """ Suppress HTTP log messages """
return return
def do_GET(self): # pylint: disable=invalid-name def do_GET(self): # pylint: disable=invalid-name
"""Process GET requests""" """ Process GET requests """
if self.path == '/': if self.path == '/':
self.send_response(200) self.send_response(200)
self.send_header('Content-type', 'text/html') self.send_header('Content-type', 'text/html')
@ -1132,14 +1177,14 @@ class HTTPserv(BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
self.wfile.write(bytes("File not found.\n", "utf-8")) self.wfile.write(bytes("File not found.\n", "utf-8"))
return return True
def http_serve_forever(httpd): def http_serve_forever(httpd):
"""httpd loop""" """ httpd loop """
httpd.serve_forever() httpd.serve_forever()
def load_config(): def load_config():
"""Loading config variables""" """ Loading config variables """
logging.info("Loading configfile") logging.info("Loading configfile")
@ -1253,6 +1298,7 @@ def send_tts_queue():
def get_url(path=False): def get_url(path=False):
""" Generate a valid URL from config values """ """ Generate a valid URL from config values """
if CONF['HTTP_BIND'] == "0.0.0.0": if CONF['HTTP_BIND'] == "0.0.0.0":
url = "localhost" url = "localhost"
else: else:
@ -1267,6 +1313,7 @@ def get_url(path=False):
def check_oauth_token(): def check_oauth_token():
""" Check for valid authentication via Twitch API """ """ Check for valid authentication via Twitch API """
global CONF global CONF
logging.debug('Checking OAuth Token') logging.debug('Checking OAuth Token')
@ -1291,7 +1338,7 @@ def check_oauth_token():
return CONF return CONF
def main(): def main():
"""Main loop""" """ Main loop """
global CONF global CONF
CONF = load_config() CONF = load_config()