From c764c20ae73e177c8890b52bcf2530bdcfdd33e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20Axelsson?= Date: Mon, 18 Dec 2017 12:58:51 -0800 Subject: [PATCH] Minor code tidying --- bumper/confserver.py | 6 +++--- bumper/xmppserver.py | 23 +++++++++-------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/bumper/confserver.py b/bumper/confserver.py index 1fa4357..dd31f8f 100644 --- a/bumper/confserver.py +++ b/bumper/confserver.py @@ -37,7 +37,7 @@ class RequestHandler(BaseHTTPRequestHandler): self.end_headers() self.wfile.write(body) except Exception as e: - logging.error(e) + logging.error('ConfServer: {}'.format(e)) class HTTPServerThread(HTTPServer, Thread): @@ -55,7 +55,7 @@ class HTTPServerThread(HTTPServer, Thread): while not self.exit_flag: self.handle_request() except Exception as e: - logging.error(e) + logging.error('ConfServer: {}'.format(e)) def disconnect(self): self.exit_flag = True # make a connection to @@ -79,7 +79,7 @@ class ConfServer(): except KeyboardInterrupt: self.disconnect() except Exception as e: - logging.error(e) + logging.error('ConfServer: {}'.format(e)) def disconnect(self): logging.info('ConfServer: shutting down...') self.server.disconnect() diff --git a/bumper/xmppserver.py b/bumper/xmppserver.py index 583b481..b13f243 100644 --- a/bumper/xmppserver.py +++ b/bumper/xmppserver.py @@ -31,7 +31,7 @@ class XMPPServer(): self.clients.append(client) self.socket.close() except Exception as e: - logging.error('e: ' + e) + logging.error('XMPPServer: {}'.format(e)) except KeyboardInterrupt: logging.debug('XMPPServer: Keyboard interrupt') finally: @@ -76,14 +76,14 @@ class Client(threading.Thread): self._set_state('DISCONNECT') def _tag_strip_uri(self, tag): - if tag[0] == "{": - uri, ignore, tag = tag[1:].partition("}") + if tag[0] == '{': + uri, ignore, tag = tag[1:].partition('}') return tag def _set_state(self, state): new_state = getattr(Client, state) if self.state > new_state: - raise Exception("{} illegal state change {}->{}".format(self.address, self.state, new_state)) + raise Exception('{} illegal state change {}->{}'.format(self.address, self.state, new_state)) logging.info('{} state: {}'.format(self.address, state)) self.state = new_state @@ -108,7 +108,6 @@ class Client(threading.Thread): # forward for client in XMPPServer.clients: if client.address != self.address and client.state == client.READY: - logging.debug('sending result: ' + data.decode('utf-8')) client.send(data.decode('utf-8')) def run(self): @@ -134,17 +133,13 @@ class Client(threading.Thread): child = self._tag_strip_uri(xml[0].tag) else: child = None - if xml.get('id'): - last_id = xml.get('id') - else: - last_id = '0' if xml.tag == 'iq': res = None if child == 'bind': - res = '{}'.format(last_id, XMPPServer.bot_id) + res = '{}'.format(xml.get('id'), XMPPServer.bot_id) self._set_state('BIND') elif child == 'session': - res = ''.format(last_id) + res = ''.format(xml.get('id')) self._set_state('READY') elif child == 'query': self._handle_ctl(xml, data) @@ -166,12 +161,12 @@ class Client(threading.Thread): self.type = self.CONTROLLER logging.info('{} type set to CONTROLLER (based on presence tag)'.format(self.address)) except ET.ParseError as e: - logging.debug("error: {}".format(e)) + logging.debug('error: {}'.format(e)) except Exception as e: - logging.error(e) + logging.error('XMPPServer: {}'.format(e)) self._set_state('DISCONNECT') except Exception as e: - logging.error(e) + logging.error('XMPPServer: {}'.format(e)) self._set_state('DISCONNECT') finally: self.disconnect()