From 43a03eaf920c1371d033feaf6679771b6c3016a5 Mon Sep 17 00:00:00 2001 From: nico Date: Sat, 21 Dec 2019 15:49:54 +0100 Subject: readability improvements * update function names to indicate what they are doing * update docstrings to their correct format --- TSGroupAssigner/group_assign.py | 78 ++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 44 deletions(-) diff --git a/TSGroupAssigner/group_assign.py b/TSGroupAssigner/group_assign.py index 1f9fdbb..c3ae371 100644 --- a/TSGroupAssigner/group_assign.py +++ b/TSGroupAssigner/group_assign.py @@ -33,30 +33,18 @@ class GroupAssigner: self.delta = delta def __connect(self): - """ - establish query connection and return connection handler - """ - try: - # connect to the telnet interface - self.conn = ts3.query.TS3Connection(self.host, self.port) + """ establish query connection and return connection handler """ + # connect to the telnet interface + self.conn = ts3.query.TS3Connection(self.host, self.port) - # login - self.conn.login(client_login_name=self.user, client_login_password=self.pw) + # login + self.conn.login(client_login_name=self.user, client_login_password=self.pw) - # select specified sid - self.conn.use(sid=self.sid) - - # break if credentials are invalid - except ts3.query.TS3QueryError as TS3QueryError: - # log error line and reraise - logging.error(TS3QueryError) - raise TS3QueryError + # select specified sid + self.conn.use(sid=self.sid) def __disconnect(self): - """ - method to gracefully logout and disconnect the connection - this should only be called if the exit is intentional - """ + """ gracefully logout and disconnect the connection, this should only be called if the exit is intentional """ try: self.conn.logout() self.conn.quit() @@ -67,17 +55,14 @@ class GroupAssigner: # broad exception if something unexpected happens except ts3.TS3Error as TS3Error: - # log error and reraise exception + # log exception logging.error(TS3Error) - raise TS3Error # exit sys.exit() - def __datecheck(self): - """ - method to check if the current date is still in the configured date range - """ + def __checkdate(self): + """ method to check if the current date is still in the configured date range """ now = dt.date.today() # check if target date is in the configured range @@ -89,10 +74,8 @@ class GroupAssigner: logging.info('the current date exceeds the configured date range -- exiting') self.__disconnect() - def __sleepstart(self): - """ - method to check if the process is eligible to sleepstart - """ + def __start_sleepstart(self): + """ method to check if the process is eligible to sleepstart """ now = dt.date.today() # start date already reached proceed @@ -115,6 +98,10 @@ class GroupAssigner: raise DateException('target date is too far in the future') def __notifycliententerview(self, data: dict): + """ + event thrown if a client connects to the server + :param data: dictionary containing users info + """ # return all non voice clients without reasonid 0 if data['client_type'] != '0' or data['reasonid'] != '0': return @@ -142,34 +129,37 @@ class GroupAssigner: except KeyError as err: logging.error([err, data]) - def __eventhandler(self, event: str, data: dict): - """ - event handler which separates events to their specific handlers - """ + def __handle_event(self, event: str, data: dict): + """ event handler which separates events to their specific handlers """ # check if event is still eligible - self.__datecheck() + self.__checkdate() # client enter events if event == "notifycliententerview": self.__notifycliententerview(data) - # all other events return to main - else: - return - def start(self): + """ main entry point to start the bot """ # eol to start process ahead of time - self.__sleepstart() + self.__start_sleepstart() # proceed only if target date is inside the date range - if self.__datecheck(): - # init connection - self.__connect() + if self.__checkdate(): + try: + # init connection + self.__connect() + + # break if credentials are invalid + except ts3.query.TS3QueryError as err: + # log error + logging.error(err) + self.__disconnect() # start processing self.__main() def __main(self): + """ bots main loop """ # register for "server" notify event self.conn.servernotifyregister(event="server") @@ -183,4 +173,4 @@ class GroupAssigner: event = self.conn.wait_for_event(timeout=60) # handover event to eventhandler - self.__eventhandler(event.event, event.parsed[0]) + self.__handle_event(event.event, event.parsed[0]) -- cgit v1.2.3-18-g5258