diff --git a/boswatch/configYaml.py b/boswatch/configYaml.py index 08a4f39..625acff 100644 --- a/boswatch/configYaml.py +++ b/boswatch/configYaml.py @@ -36,7 +36,7 @@ class ConfigYAML: return str(self._config) def loadConfigFile(self, configPath): - """!loads a given configuration + """!loads a given configuration file @param configPath: Path to the config file @return True or False""" diff --git a/boswatch/network/broadcast.py b/boswatch/network/broadcast.py index 6684843..f18716f 100644 --- a/boswatch/network/broadcast.py +++ b/boswatch/network/broadcast.py @@ -68,8 +68,6 @@ class BroadcastClient: return True except socket.timeout: # nothing received - retry logging.debug("no magic packet received") - except: # pragma: no cover - logging.exception("error on getting connection info") logging.warning("cannot fetch connection info after %d tries", sendPackages) return False @@ -135,17 +133,16 @@ class BroadcastServer: But function returns immediately. @return True or False""" - try: - if self.isRunning: - logging.debug("stop udp broadcast server") - self._serverShutdown = True - return True - else: - logging.warning("udp broadcast server always stopped") - return True - except: # pragma: no cover - logging.exception("cannot stop udp broadcast server thread") - return False + + if self.isRunning: + logging.debug("stop udp broadcast server") + self._serverShutdown = True + return True + else: + logging.warning("udp broadcast server always stopped") + return True + + return False def _listen(self): """!Broadcast server worker thread @@ -166,8 +163,6 @@ class BroadcastServer: self._socket.sendto(";".encode() + str(self._servePort).encode(), address) except socket.timeout: continue # timeout is accepted (not block at recvfrom()) - except: # pragma: no cover - logging.exception("error while listening for clients") self._serverThread = None logging.debug("udp broadcast server stopped") diff --git a/boswatch/network/client.py b/boswatch/network/client.py index 7e73439..2cb0d94 100644 --- a/boswatch/network/client.py +++ b/boswatch/network/client.py @@ -27,11 +27,8 @@ class TCPClient: """!Create a new instance @param timeout: timeout for the client in sec. (3)""" - try: - self._sock = None - self._timeout = timeout - except: # pragma: no cover - logging.exception("cannot create a TCPClient") + self._sock = None + self._timeout = timeout def connect(self, host="localhost", port=8080): """!Connect to the server @@ -51,13 +48,9 @@ class TCPClient: return True except ConnectionRefusedError: logging.error("cannot connect to %s:%s - connection refused", host, port) - return False except socket.timeout: # pragma: no cover logging.warning("cannot connect to %s:%s - timeout after %s sec", host, port, self._timeout) - return False - except: # pragma: no cover - logging.exception("cannot connect to %s:%s", host, port) - return False + return False def disconnect(self): """!Disconnect from the server @@ -74,10 +67,7 @@ class TCPClient: return True except AttributeError: logging.error("cannot disconnect - no connection established") - return False - except: # pragma: no cover - logging.exception("error while disconnecting") - return False + return False def transmit(self, data): """!Send a data packet to the server @@ -91,13 +81,9 @@ class TCPClient: return True except AttributeError: logging.error("cannot transmit - no connection established") - return False except ConnectionResetError: logging.error("cannot transmit - host closed connection") - return False - except: # pragma: no cover - logging.exception("error while transmitting") - return False + return False def receive(self): """!Receive data from the server @@ -109,16 +95,11 @@ class TCPClient: return received except AttributeError: logging.error("cannot receive - no connection established") - return False except ConnectionResetError: logging.error("cannot receive - host closed connection") - return False except socket.timeout: # pragma: no cover logging.warning("cannot receive - timeout after %s sec", self._timeout) - return False - except: # pragma: no cover - logging.exception("error while receiving") - return False + return False @property def isConnected(self): diff --git a/boswatch/network/netCheck.py b/boswatch/network/netCheck.py index c71d0ba..5a4f3b8 100644 --- a/boswatch/network/netCheck.py +++ b/boswatch/network/netCheck.py @@ -42,7 +42,7 @@ class NetCheck: logging.debug("%s is reachable", self._hostname) self._connectionState = True return True - except: + except: # todo find right exception type logging.warning("%s is not reachable", self._hostname) self._connectionState = False return False diff --git a/boswatch/network/server.py b/boswatch/network/server.py index f83dc80..c802ae8 100644 --- a/boswatch/network/server.py +++ b/boswatch/network/server.py @@ -53,8 +53,6 @@ class _ThreadedTCPRequestHandler(socketserver.ThreadingMixIn, socketserver.BaseR except (ConnectionResetError, ConnectionAbortedError): # pragma: no cover logging.debug("%s connection closed", req_name) - except: # pragma: no cover - logging.exception("%s error while receiving", req_name) finally: del self.server.clientsConnected[threading.current_thread().name] logging.info("Client disconnected: %s", self.client_address[0]) @@ -96,47 +94,39 @@ class TCPServer: @param port: Server Port (8080) @return True or False""" - try: - if not self.isRunning: - self._server = _ThreadedTCPServer(("", port), _ThreadedTCPRequestHandler) - self._server.timeout = self._timeout - self._server.alarmQueue = self._alarmQueue + if not self.isRunning: + self._server = _ThreadedTCPServer(("", port), _ThreadedTCPRequestHandler) + self._server.timeout = self._timeout + self._server.alarmQueue = self._alarmQueue - self._server.clientsConnctedLock = self._clientsConnectedLock - self._server.clientsConnected = self._clientsConnected + self._server.clientsConnctedLock = self._clientsConnectedLock + self._server.clientsConnected = self._clientsConnected - self._server_thread = threading.Thread(target=self._server.serve_forever) - self._server_thread.name = "Thread-BWServer" - self._server_thread.daemon = True - self._server_thread.start() - logging.debug("TCPServer started in Thread: %s", self._server_thread.name) - return True - else: - logging.warning("server always started") - return True - except: # pragma: no cover - logging.exception("cannot start the server") - return False + self._server_thread = threading.Thread(target=self._server.serve_forever) + self._server_thread.name = "Thread-BWServer" + self._server_thread.daemon = True + self._server_thread.start() + logging.debug("TCPServer started in Thread: %s", self._server_thread.name) + return True + else: + logging.warning("server always started") + return True def stop(self): """!Stops the TCP socket server @return True or False""" - try: - if self.isRunning: - self._server.shutdown() - self._server_thread.join() - self._server_thread = None - self._server.socket.close() - self._server = None - logging.debug("TCPServer stopped") - return True - else: - logging.warning("server always stopped") - return True - except: # pragma: no cover - logging.exception("cannot stop the server") - return False + if self.isRunning: + self._server.shutdown() + self._server_thread.join() + self._server_thread = None + self._server.socket.close() + self._server = None + logging.debug("TCPServer stopped") + return True + else: + logging.warning("server always stopped") + return True def countClientsConnected(self): """!Number of currently connected Clients diff --git a/boswatch/packet.py b/boswatch/packet.py index 600588c..cc2c631 100644 --- a/boswatch/packet.py +++ b/boswatch/packet.py @@ -34,11 +34,7 @@ class Packet: self._packet = {"timestamp": time.time()} else: logging.debug("create bwPacket from string") - try: - self._packet = eval(str(bwPacket.strip())) - except: # pragma: no cover - # todo can we repair the packet anyway? - logging.exception("error while create packet from string") + self._packet = eval(str(bwPacket.strip())) def __str__(self): """!Return the intern _packet dict as string""" @@ -63,7 +59,7 @@ class Packet: logging.warning("field not found: %s", fieldName) return None - def addClientData(self): + def addClientData(self, config): """!Add the client information to the decoded data This function adds the following data to the bwPacket: @@ -73,16 +69,15 @@ class Packet: - clientBranch - inputSource - frequency""" - config = configYaml.loadConfigSharepoint("clientConfig") logging.debug("add client data to bwPacket") - self.set("clientName", config["client"]["name"]) + self.set("clientName", config.get("client", "name")) self.set("clientVersion", version.client) self.set("clientBuildDate", version.date) self.set("clientBranch", version.branch) - self.set("inputSource", config["client"]["inputSource"]) - self.set("frequency", config["inputSource"]["sdr"]["frequency"]) + self.set("inputSource", config.get("client", "inoutSource")) + self.set("frequency", config.get("inputSource", "sdr", "frequency")) - def addServerData(self): + def addServerData(self, config): """!Add the server information to the decoded data This function adds the following data to the bwPacket: @@ -90,9 +85,8 @@ class Packet: - serverVersion - serverBuildDate - serverBranch""" - config = configYaml.loadConfigSharepoint("serverConfig") logging.debug("add server data to bwPacket") - self.set("serverName", config["server"]["name"]) + self.set("serverName", config.get("server", "name")) self.set("serverVersion", version.server) self.set("serverBuildDate", version.date) self.set("serverBranch", version.branch) diff --git a/boswatch/utils/header.py b/boswatch/utils/header.py index 201beb9..38cbc90 100644 --- a/boswatch/utils/header.py +++ b/boswatch/utils/header.py @@ -26,66 +26,39 @@ def logoToLog(): """!Prints the BOSWatch logo to the log at debug level @return True or False on error""" - try: - logging.debug(" ____ ____ ______ __ __ __ _____ ") - logging.debug(" / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ / ") - logging.debug(" / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ < ") - logging.debug(" / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ / ") - logging.debug("/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/ ") - logging.debug(" German BOS Information Script ") - logging.debug(" by Bastian Schroll ") - logging.debug("") - return True - except: # pragma: no cover - logging.exception("cannot display logo in log") - return False + logging.debug(" ____ ____ ______ __ __ __ _____ ") + logging.debug(" / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ / ") + logging.debug(" / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ < ") + logging.debug(" / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ / ") + logging.debug("/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/ ") + logging.debug(" German BOS Information Script ") + logging.debug(" by Bastian Schroll ") + logging.debug("") + return True def infoToLog(): """!Prints the BOSWatch and OS information to log at debug level @return True or False on error""" - try: - logging.debug("BOSWatch and environment information") - logging.debug("- Client version: %d.%d.%d", - boswatch.version.client["major"], - boswatch.version.client["minor"], - boswatch.version.client["patch"]) - logging.debug("- Server version: %d.%d.%d", - boswatch.version.server["major"], - boswatch.version.server["minor"], - boswatch.version.server["patch"]) - logging.debug("- Branch: %s", - boswatch.version.branch) - logging.debug("- Release date: %02d.%02d.%4d", - boswatch.version.date["day"], - boswatch.version.date["month"], - boswatch.version.date["year"]) - logging.debug("- Python version: %s", platform.python_version()) - logging.debug("- Python build: %s", platform.python_build()) - logging.debug("- System: %s", platform.system()) - logging.debug("- OS Version: %s", platform.platform()) - logging.debug("") - return True - except: # pragma: no cover - logging.exception("cannot display OS information") - return False - - -def logoToScreen(): - """!Prints the BOSWatch logo to the screen at debug level - - @return True or False on error""" - try: - print(" ____ ____ ______ __ __ __ _____ ") - print(" / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ / ") - print(" / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ < ") - print(" / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ / ") - print("/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/ ") - print(" German BOS Information Script ") - print(" by Bastian Schroll ") - print("") - return True - except: # pragma: no cover - logging.exception("cannot display logo on screen") - return False + logging.debug("BOSWatch and environment information") + logging.debug("- Client version: %d.%d.%d", + boswatch.version.client["major"], + boswatch.version.client["minor"], + boswatch.version.client["patch"]) + logging.debug("- Server version: %d.%d.%d", + boswatch.version.server["major"], + boswatch.version.server["minor"], + boswatch.version.server["patch"]) + logging.debug("- Branch: %s", + boswatch.version.branch) + logging.debug("- Release date: %02d.%02d.%4d", + boswatch.version.date["day"], + boswatch.version.date["month"], + boswatch.version.date["year"]) + logging.debug("- Python version: %s", platform.python_version()) + logging.debug("- Python build: %s", platform.python_build()) + logging.debug("- System: %s", platform.system()) + logging.debug("- OS Version: %s", platform.platform()) + logging.debug("") + return True diff --git a/boswatch/utils/paths.py b/boswatch/utils/paths.py index c20d1e1..146a260 100644 --- a/boswatch/utils/paths.py +++ b/boswatch/utils/paths.py @@ -49,11 +49,7 @@ def makeDirIfNotExist(dirPath): @param dirPath: Path of the directory @return Path of the directory or False""" - try: - if not os.path.exists(dirPath): - os.mkdir(dirPath) - logging.debug("directory created: %s", dirPath) - return dirPath - except: # pragma: no cover - logging.exception("error by creating a directory: %s", dirPath) - return False + if not os.path.exists(dirPath): + os.mkdir(dirPath) + logging.debug("directory created: %s", dirPath) + return dirPath diff --git a/boswatch/utils/timer.py b/boswatch/utils/timer.py index 449864d..b58acc8 100644 --- a/boswatch/utils/timer.py +++ b/boswatch/utils/timer.py @@ -46,37 +46,29 @@ class RepeatedTimer: """!Start a new timer worker thread @return True or False""" - try: - if self._thread is None: - self._event.clear() - self._thread = Thread(target=self._target) - self._thread.name = "RepTim(" + str(self._interval) + ")" - self._thread.daemon = True # start as daemon (thread dies if main program ends) - self._thread.start() - logging.debug("start repeatedTimer: %s", self._thread.name) - return True - else: - logging.debug("repeatedTimer always started") - return True - except: # pragma: no cover - logging.exception("cannot start timer worker thread") - return False + if self._thread is None: + self._event.clear() + self._thread = Thread(target=self._target) + self._thread.name = "RepTim(" + str(self._interval) + ")" + self._thread.daemon = True # start as daemon (thread dies if main program ends) + self._thread.start() + logging.debug("start repeatedTimer: %s", self._thread.name) + return True + else: + logging.debug("repeatedTimer always started") + return True def stop(self): """!Stop the timer worker thread @return True or False""" - try: - self._event.set() - if self._thread is not None: - logging.debug("stop repeatedTimer: %s", self._thread.name) - self._thread.join() - return True - logging.warning("repeatedTimer always stopped") + self._event.set() + if self._thread is not None: + logging.debug("stop repeatedTimer: %s", self._thread.name) + self._thread.join() return True - except: # pragma: no cover - logging.exception("cannot stop repeatedTimer") - return False + logging.warning("repeatedTimer always stopped") + return True def _target(self): """!Runs the target function with his arguments in own thread""" diff --git a/boswatch/utils/wildcard.py b/boswatch/utils/wildcard.py index dc0f746..161019a 100644 --- a/boswatch/utils/wildcard.py +++ b/boswatch/utils/wildcard.py @@ -33,10 +33,12 @@ def replaceWildcards(message, bwPacket): "{TIME}": time.time(), # info wildcards + # server "{SNAME}": bwPacket.getField("serverName"), "{SVERS}": bwPacket.getField("serverVersion"), "{SDATE}": bwPacket.getField("serverBuildDate"), "{SBRCH}": bwPacket.getField("serverBranch"), + # client "{CNAME}": bwPacket.getField("clientName"), "{CIP}": bwPacket.getField("clientIP"), "{CVERS}": bwPacket.getField("clientVersion"), @@ -75,9 +77,6 @@ def replaceWildcards(message, bwPacket): # message for MSG packet is done in poc } for wildcard in _wildcards: - try: - message = message.replace(wildcard, _wildcards[wildcard]) - except: - logging.exception("error in wildcard replacement") + message = message.replace(wildcard, _wildcards.get(wildcard)) return message diff --git a/boswatch/version.py b/boswatch/version.py index 7e2203e..512be68 100644 --- a/boswatch/version.py +++ b/boswatch/version.py @@ -20,5 +20,5 @@ logging.debug("- %s loaded", __name__) client = {"major": 3, "minor": 0, "patch": 0} server = {"major": 3, "minor": 0, "patch": 0} -date = {"day": 1, "month": 1, "year": 2018} +date = {"day": 1, "month": 1, "year": 2019} branch = "develop" diff --git a/router_test.py b/router_test.py deleted file mode 100644 index b1a02e2..0000000 --- a/router_test.py +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -from boswatch.configYaml import ConfigYAML -from boswatch.packet import Packet -from boswatch.router import RouterManager - -config = ConfigYAML() -config.loadConfigFile("config/server.yaml") - -bwPack = Packet("{'timestamp': 1551421020.9004176, 'mode': 'zvei', 'zvei': '12345'}") - -print() - -routMan = RouterManager() - -routMan.buildRouter(config) - -print() -routMan.runRouter(config.get("alarmRouter"), bwPack) - -print() -routMan.runRouter("Router 2", bwPack) - - -print() -exit()