Test mode for local testing without altering config

pull/4/head
Piotr Falkowski 2023-10-10 23:27:18 +02:00
rodzic 360a5eac5b
commit c907dc599d
2 zmienionych plików z 12 dodań i 7 usunięć

Wyświetl plik

@ -4,7 +4,6 @@
import urllib.request, urllib.error, urllib.parse import urllib.request, urllib.error, urllib.parse
import re import re
import logging import logging
import pytz
import socket import socket
from datetime import datetime from datetime import datetime

Wyświetl plik

@ -71,6 +71,7 @@ You can find full list of contributors on github.com/sq6jnx/sr0wx.py
# #
# SR0WX (core) requires the following packages: # SR0WX (core) requires the following packages:
import contextlib
import getopt import getopt
import os import os
import pygame import pygame
@ -78,6 +79,7 @@ import sys
import logging, logging.handlers import logging, logging.handlers
import numpy import numpy
import urllib.request, urllib.error, urllib.parse import urllib.request, urllib.error, urllib.parse
import socket
# ``os``, ``sys`` and ``time`` doesn't need further explanation, these are # ``os``, ``sys`` and ``time`` doesn't need further explanation, these are
# syandard Python packages. # syandard Python packages.
@ -137,8 +139,9 @@ message = " "
# Modules may be also given in commandline, separated by a comma. # Modules may be also given in commandline, separated by a comma.
config = None config = None
test_mode = False
try: try:
opts, args = getopt.getopt(sys.argv[1:], "c:", ["config="]) opts, args = getopt.getopt(sys.argv[1:], "c:t", ["config=", "test"])
except getopt.GetoptError: except getopt.GetoptError:
pass pass
for opt, arg in opts: for opt, arg in opts:
@ -146,6 +149,8 @@ for opt, arg in opts:
if arg[-3:] == '.py': if arg[-3:] == '.py':
arg = arg[:-3] arg = arg[:-3]
config = __import__(arg) config = __import__(arg)
if opt in ("-t", "--test"):
test_mode = True
if config is None: if config is None:
import config import config
@ -154,6 +159,8 @@ logger = setup_logging(config)
logger.info(COLOR_WARNING + "sr0wx.py started" + COLOR_ENDC) logger.info(COLOR_WARNING + "sr0wx.py started" + COLOR_ENDC)
logger.info(LICENSE) logger.info(LICENSE)
if test_mode:
logger.info("Test mode enabled, skipping serial port usage")
if len(args) > 0: if len(args) > 0:
@ -252,7 +259,7 @@ for el in message:
# Program should be able to "press PTT" via RSS232. See ``config`` for # Program should be able to "press PTT" via RSS232. See ``config`` for
# details. # details.
if config.serial_port is not None: if not test_mode and config.serial_port is not None:
import serial import serial
try: try:
@ -287,10 +294,9 @@ for el in message:
pygame.time.wait(500) pygame.time.wait(500)
else: else:
if "upper" in dir(el): if "upper" in dir(el):
try: with contextlib.suppress(Exception):
voice_channel = sound_samples[el].play() voice_channel = sound_samples[el].play()
except:
a=1
elif "upper" not in dir(el): elif "upper" not in dir(el):
sound = pygame.sndarray.make_sound(el) sound = pygame.sndarray.make_sound(el)
@ -313,7 +319,7 @@ pygame.time.delay(1000)
# If we've opened serial it's now time to close it. # If we've opened serial it's now time to close it.
try: try:
if config.serial_port is not None: if not test_mode and config.serial_port is not None:
ser.close() ser.close()
logger.info(COLOR_OKGREEN + "RTS/PTT set to OFF\n" + COLOR_ENDC) logger.info(COLOR_OKGREEN + "RTS/PTT set to OFF\n" + COLOR_ENDC)
except NameError: except NameError: