FreeDATA/modem/config.py

167 lines
5.9 KiB
Python
Raw Normal View History

2022-09-20 09:34:28 +00:00
import configparser
import structlog
class CONFIG:
"""
CONFIG class for handling with config files
"""
2022-12-10 12:34:26 +00:00
def __init__(self, configfile: str):
2022-09-20 09:34:28 +00:00
# set up logger
self.log = structlog.get_logger("CONFIG")
# init configparser
self.config = configparser.ConfigParser(inline_comment_prefixes="#", allow_no_value=True)
2022-12-10 12:34:26 +00:00
try:
self.config_name = configfile
except Exception:
self.config_name = "config.ini"
2022-09-20 09:34:28 +00:00
self.log.info("[CFG] logfile init", file=self.config_name)
# check if log file exists
self.config_exists()
def config_exists(self):
"""
check if config file exists
"""
try:
return bool(self.config.read(self.config_name, None))
except Exception as configerror:
self.log.error("[CFG] logfile init error", e=configerror)
return False
def write_config(self, section: str, key: str, value):
"""
write values to config
"""
# Validates config data
def validate(self, data):
for section in data:
for setting in data[section]:
if section == 'NETWORK':
if setting == 'modemport' and int(data[section][setting]) == 0:
raise Exception("'modemport' should be an integer")
if section == 'STATION':
if setting == 'mycall' and len(data[section][setting]) <= 0:
raise Exception("'%s' can't be empty" % setting)
if setting == 'mygrid' and len(data[section][setting]) <= 0:
raise Exception("'%s' can't be empty" % setting)
if setting == 'ssid_list' and not isinstance(data[section][setting], list):
raise Exception("'%s' needs to be a list" % setting)
# TODO finish this for all config settings!
# Sets and writes config data from a dict containing data settings
def write(self, data):
# Validate config data before writing
self.validate(data)
for section in data:
# init section if it doesn't exist yet
if not section.upper() in self.config.keys():
self.config[section] = {}
for setting in data[section]:
self.config[section][setting] = data[section][setting]
# Write config data to file
try:
with open(self.config_name, 'w') as configfile:
self.config.write(configfile)
return self.config
except Exception as conferror:
self.log.error("[CFG] reading logfile", e=conferror)
return False
# TODO remove this method when ready
2022-09-20 09:34:28 +00:00
def write_entire_config(self, data):
"""
write entire config
"""
self.config['NETWORK'] = {'#Network settings': None,
'ModemPORT': data[50]
2022-09-20 09:34:28 +00:00
}
self.config['STATION'] = {'#Station settings': None,
'mycall': data[1],
'mygrid': data[2],
2023-02-02 22:51:22 +00:00
'ssid_list': list(data[18])# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # list(data[18])
2022-09-20 09:34:28 +00:00
}
self.config['AUDIO'] = {'#Audio settings': None,
'rx': data[3],
'tx': data[4],
2023-02-02 22:49:52 +00:00
'txaudiolevel': data[14],
2023-10-31 15:45:18 +00:00
'rxaudiolevel': data[25],
2023-02-02 22:49:52 +00:00
'auto_tune': data[19]
2022-09-20 09:34:28 +00:00
}
self.config['RADIO'] = {'#Radio settings': None,
2023-02-02 22:49:52 +00:00
'radiocontrol': data[5],
'rigctld_ip': data[6],
'rigctld_port': data[7]
2022-09-20 09:34:28 +00:00
}
self.config['Modem'] = {'#Modem settings': None,
2023-02-02 22:49:52 +00:00
'scatter': data[8],
'fft': data[9],
'narrowband': data[10],
'fmin': data[11],
'fmax': data[12],
'qrv': data[15],
2023-05-07 08:34:23 +00:00
'rx_buffer_size': data[16],
2023-02-02 22:49:52 +00:00
'explorer': data[17],
2023-03-04 09:59:43 +00:00
'stats': data[19],
2023-03-06 11:48:27 +00:00
'fsk': data[13],
2023-10-31 08:53:31 +00:00
'tx_delay': data[21],
2023-10-31 15:45:18 +00:00
'transmit_morse_identifier' : data[26]
2023-10-31 08:53:31 +00:00
}
2023-05-20 07:53:57 +00:00
self.config['TCI'] = {'#TCI settings': None,
'ip': data[22],
'port': data[23]
}
2023-10-31 08:53:31 +00:00
self.config['MESH'] = {'#Mesh settings': None,
2023-07-03 15:54:24 +00:00
'enable_protocol': data[24]
}
2022-09-20 09:34:28 +00:00
try:
with open(self.config_name, 'w') as configfile:
self.config.write(configfile)
except Exception as conferror:
2022-09-20 09:36:11 +00:00
self.log.error("[CFG] reading logfile", e=conferror)
2022-09-20 09:34:28 +00:00
2023-11-06 14:36:11 +00:00
def read(self):
2022-09-20 09:34:28 +00:00
"""
read config file
"""
2023-11-06 14:36:11 +00:00
if not self.config_exists():
return False
2022-09-20 09:34:28 +00:00
2023-11-06 14:36:11 +00:00
return {s:dict(self.config.items(s)) for s in self.config.sections()}
2022-09-20 09:34:28 +00:00
2023-02-18 13:18:39 +00:00
def get(self, area, key, default):
"""
read from config and add if not exists
"""
2023-02-18 13:51:26 +00:00
2023-02-18 18:57:04 +00:00
for _ in range(2):
2023-02-18 14:01:26 +00:00
try:
2023-02-18 18:57:04 +00:00
parameter = (
self.config[area][key] in ["True", "true", True]
if default in ["True", "true", True, "False", "false", False]
else self.config[area][key]
)
2023-02-18 14:01:26 +00:00
except KeyError:
self.config[area][key] = str(default)
2023-02-18 13:51:26 +00:00
self.log.info("[CFG] reading...", parameter=parameter, key=key)
return parameter