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
|
|
|
|
"""
|
|
|
|
|
|
|
|
def write_entire_config(self, data):
|
|
|
|
"""
|
|
|
|
write entire config
|
|
|
|
"""
|
|
|
|
self.config['NETWORK'] = {'#Network settings': None,
|
2022-09-20 10:23:28 +00:00
|
|
|
'TNCPORT': data[50]
|
2022-09-20 09:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.config['STATION'] = {'#Station settings': None,
|
2022-09-20 10:23:28 +00:00
|
|
|
'mycall': data[1],
|
2022-12-08 15:14:59 +00:00
|
|
|
'mygrid': data[2],
|
|
|
|
'ssid_list': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # list(data[26])
|
2022-09-20 09:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.config['AUDIO'] = {'#Audio settings': None,
|
|
|
|
'rx': data[3],
|
|
|
|
'tx': data[4],
|
2023-02-01 15:11:07 +00:00
|
|
|
'txaudiolevel': data[22],
|
|
|
|
'auto_tune': data[27]
|
2022-09-20 09:34:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
self.config['RADIO'] = {'#Radio settings': None,
|
|
|
|
'radiocontrol': data[13],
|
|
|
|
'rigctld_ip': data[14],
|
|
|
|
'rigctld_port': data[15]
|
|
|
|
}
|
|
|
|
self.config['TNC'] = {'#TNC settings': None,
|
|
|
|
'scatter': data[16],
|
|
|
|
'fft': data[17],
|
|
|
|
'narrowband': data[18],
|
|
|
|
'fmin': data[19],
|
|
|
|
'fmax': data[20],
|
|
|
|
'qrv': data[23],
|
2022-11-05 21:27:33 +00:00
|
|
|
'rxbuffersize': data[24],
|
2023-02-02 17:03:22 +00:00
|
|
|
'explorer': data[25],
|
|
|
|
'stats': data[28]
|
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
|
|
|
|
|
|
|
|
|
|
|
def read_config(self):
|
|
|
|
"""
|
|
|
|
read config file
|
|
|
|
"""
|
|
|
|
if self.config_exists():
|
|
|
|
#print(self.config.read(self.config_name))
|
|
|
|
#print(self.config.sections())
|
|
|
|
|
|
|
|
return self.config
|
|
|
|
|