From 4f389723c01aabbe896f4f3865a43e664947c9ea Mon Sep 17 00:00:00 2001 From: Bastian Schroll Date: Mon, 11 Mar 2019 07:47:51 +0100 Subject: [PATCH] some refactorings --- boswatch/configYaml.py | 7 +++++ boswatch/packet.py | 32 ------------------- boswatch/{utils => }/timer.py | 0 boswatch/utils/misc.py | 54 ++++++++++++++++++++++++++++++++ boswatch/{utils => }/wildcard.py | 0 bw_client.py | 52 ++---------------------------- bw_server.py | 3 +- plugin/plugin.py | 2 +- test/boswatch/test_timer.py | 2 +- 9 files changed, 67 insertions(+), 85 deletions(-) rename boswatch/{utils => }/timer.py (100%) create mode 100644 boswatch/utils/misc.py rename boswatch/{utils => }/wildcard.py (100%) diff --git a/boswatch/configYaml.py b/boswatch/configYaml.py index 5e7239f..5a0cab2 100644 --- a/boswatch/configYaml.py +++ b/boswatch/configYaml.py @@ -34,6 +34,7 @@ class ConfigYAML: yield item def __str__(self): + """!Returns the string representation of the internal config dict""" return str(self._config) def loadConfigFile(self, configPath): @@ -54,6 +55,12 @@ class ConfigYAML: return False def get(self, *args, default=None): + """!Get a single value from the config + or a value set in a new configYAML class instance + + @param *args: Config section (one ore more strings) + @param default: Default value if section not found (None) + @return: A single value, a value set in an configYAML instance, the default value""" tmp = self._config try: for arg in args: diff --git a/boswatch/packet.py b/boswatch/packet.py index 185bed7..78d8802 100644 --- a/boswatch/packet.py +++ b/boswatch/packet.py @@ -58,38 +58,6 @@ class Packet: logging.warning("field not found: %s", fieldName) return None - def addClientData(self, config): - """!Add the client information to the decoded data - - This function adds the following data to the bwPacket: - - clientName - - clientVersion - - clientBuildDate - - clientBranch - - inputSource - - frequency""" - logging.debug("add client data to bwPacket") - 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.get("client", "inoutSource")) - self.set("frequency", config.get("inputSource", "sdr", "frequency")) - - def addServerData(self, config): - """!Add the server information to the decoded data - - This function adds the following data to the bwPacket: - - serverName - - serverVersion - - serverBuildDate - - serverBranch""" - logging.debug("add server data to bwPacket") - self.set("serverName", config.get("server", "name")) - self.set("serverVersion", version.server) - self.set("serverBuildDate", version.date) - self.set("serverBranch", version.branch) - def printInfo(self): """!Print a info message to the log on INFO level. Contains the most useful info about this packet. diff --git a/boswatch/utils/timer.py b/boswatch/timer.py similarity index 100% rename from boswatch/utils/timer.py rename to boswatch/timer.py diff --git a/boswatch/utils/misc.py b/boswatch/utils/misc.py new file mode 100644 index 0000000..95adea3 --- /dev/null +++ b/boswatch/utils/misc.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +"""! + ____ ____ ______ __ __ __ _____ + / __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ / + / __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ < + / /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ / +/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/ + German BOS Information Script + by Bastian Schroll + +@file: misc.py +@date: 11.03.2019 +@author: Bastian Schroll +@description: Some misc functions +""" +import logging +from boswatch import version + +logging.debug("- %s loaded", __name__) + + +def addClientDataToPacket(bwPacket, config): + """!Add the client information to the decoded data + + This function adds the following data to the bwPacket: + - clientName + - clientVersion + - clientBuildDate + - clientBranch + - inputSource + - frequency""" + logging.debug("add client data to bwPacket") + bwPacket.set("clientName", config.get("client", "name")) + bwPacket.set("clientVersion", version.client) + bwPacket.set("clientBuildDate", version.date) + bwPacket.set("clientBranch", version.branch) + bwPacket.set("inputSource", config.get("client", "inoutSource")) + bwPacket.set("frequency", config.get("inputSource", "sdr", "frequency")) + + +def addServerDataToPacket(bwPacket, config): + """!Add the server information to the decoded data + + This function adds the following data to the bwPacket: + - serverName + - serverVersion + - serverBuildDate + - serverBranch""" + logging.debug("add server data to bwPacket") + bwPacket.set("serverName", config.get("server", "name")) + bwPacket.set("serverVersion", version.server) + bwPacket.set("serverBuildDate", version.date) + bwPacket.set("serverBranch", version.branch) diff --git a/boswatch/utils/wildcard.py b/boswatch/wildcard.py similarity index 100% rename from boswatch/utils/wildcard.py rename to boswatch/wildcard.py diff --git a/bw_client.py b/bw_client.py index 2ae2162..a93d0ad 100644 --- a/bw_client.py +++ b/bw_client.py @@ -41,60 +41,12 @@ from boswatch.network.client import TCPClient from boswatch.network.broadcast import BroadcastClient from boswatch.decoder.decoder import Decoder from boswatch.utils import header -from boswatch.processManager import ProcessManager +from boswatch.utils import misc header.logoToLog() header.infoToLog() -# sox = ProcessManager("_bin/win/sox/sox.exe") -# rtl = ProcessManager("_bin/win/rtl_fm/rtl_fm.exe") -# multimon = ProcessManager("_bin/win/multimon/multimon-ng.exe", True) -# -# -# try: -# rtl.addArgument("-f 85.235MHz") -# rtl.addArgument("-d 1") -# rtl.addArgument("-M fm -s 22050 -g 100") -# rtl.setStderr(None) -# #rtl.start() -# -# sox.addArgument("-t ogg 88022.ogg -esigned-integer -b16 -r 22050 -t raw -") -# sox.setStderr(None) -# sox.start() -# -# -# #.\_bin\win\sox\sox.exe -t ogg -esigned-integer -b16 -r 22050 88022.ogg - -# -# multimon.addArgument("-a ZVEI1 -a FMSFSK") -# multimon.addArgument("-t raw") -# #multimon.addArgument("-v 3") -# multimon.addArgument("-") -# multimon.setStdin(sox.stdout) -# multimon.setStderr(None) -# -# multimon.start() -# -# logging.debug("go") -# -# while multimon.isRunning: -# data = multimon.readline() -# if data is not None: -# print(data) -# -# logging.debug("end") -# -# except: -# logging.exception("batsch") -# -# finally: -# -# sox.stop() -# multimon.stop() -# rtl.stop() -# -# exit() - logging.debug("parse args") # With -h or --help you get the Args help parser = argparse.ArgumentParser(prog="bw_client.py", @@ -140,7 +92,7 @@ try: if bwPacket: bwPacket.printInfo() - bwPacket.addClientData(bwConfig) + misc.addClientDataToPacket(bwPacket, bwConfig) bwClient.transmit(str(bwPacket)) # todo should we do this in an thread, to not block receiving ??? but then we should use transmit() and receive() with Lock() diff --git a/bw_server.py b/bw_server.py index aff6f89..c91cf0b 100644 --- a/bw_server.py +++ b/bw_server.py @@ -44,6 +44,7 @@ from boswatch.packet import Packet from boswatch.utils import header from boswatch.network.broadcast import BroadcastServer from boswatch.router.routerManager import RouterManager +from boswatch.utils import misc header.logoToLog() @@ -93,7 +94,7 @@ try: bwPacket = Packet((data[1])) bwPacket.set("clientIP", data[0]) - bwPacket.addServerData(bwConfig) + misc.addServerDataToPacket(bwPacket, bwConfig) bwRoutMan.runRouter(bwConfig.get("alarmRouter"), bwPacket) diff --git a/plugin/plugin.py b/plugin/plugin.py index d603a81..f0a6de6 100644 --- a/plugin/plugin.py +++ b/plugin/plugin.py @@ -17,7 +17,7 @@ import logging import time -from boswatch.utils import wildcard +from boswatch import wildcard logging.debug("- %s loaded", __name__) diff --git a/test/boswatch/test_timer.py b/test/boswatch/test_timer.py index 2fb52a1..64157d2 100644 --- a/test/boswatch/test_timer.py +++ b/test/boswatch/test_timer.py @@ -20,7 +20,7 @@ import logging import time import pytest -from boswatch.utils.timer import RepeatedTimer +from boswatch.timer import RepeatedTimer def setup_method(method):