Added configuration file handler and updated apps

This commit is contained in:
2021-11-18 07:23:23 -05:00
parent 19d85aebfe
commit 5d1e98eee6
3 changed files with 46 additions and 38 deletions

View File

@ -7,31 +7,42 @@ Created on Fri Jan 8 10:26:56 2021
""" """
import os import os
import system
import yaml import yaml
class ArrowHandle(): class ConfigurationFile():
def __init__(self, icon): def __init__(self, app_path):
self.pathfilename = "./" + "config.yaml" self.app_file = app_path
self.cfg_path = os.path.dirname(os.path.realpath(app_path))
(base, ext) = os.path.splitext(os.path.basename(app_path))
self.app_name = base
self.cfg_file = self.app_name + ".yaml"
self.use_path = ""
self.config = [] self.config = []
def find_config(self, path=".", file="config.yaml"): def find_config(self):
# local first # local first
if path.isfile(file): if os.path.isfile(self.cfg_file):
self.pathfilename = file self.use_path = self.cfg_file
return 1 return 1
elif path.isfile(path + "/" + file): elif os.path.isfile(self.cfg_path + "/" + self.cfg_file):
self.pathfilename = path + "/" + file self.use_path = self.cfg_path + "/" + self.cfg_file
return 1 return 1
else: else:
print("Config file not found:", file) print("Config file not found:", self.cfg_file)
return 0 return 0
def read_config(self, path=".", file="config.yaml"): def read_config(self):
if self.find_config(path, file): if self.find_config():
with open(self.pathfilename) as f: # print(self.app_file, " using configuration file: ", self.use_path)
with open(self.use_path) as f:
self.config = yaml.load(f, Loader=yaml.FullLoader) self.config = yaml.load(f, Loader=yaml.FullLoader)
f.close() f.close()
else: else:
self.config= [] self.config = []
return self.config return self.config
def write_config(self, config):
with open(self.use_path, 'w') as f:
f.write(yaml.dump(config))
f.close()

View File

@ -26,6 +26,8 @@ from MainWindow import Ui_MainWindow
import maidenhead as mh import maidenhead as mh
from utils.configs import *
############################################################################## ##############################################################################
# log # log
############################################################################## ##############################################################################
@ -41,7 +43,6 @@ fix_mode = [
'0=None', '1=No', '2=2D', '3=3D' '0=None', '1=No', '2=2D', '3=3D'
]; ];
config_file = "gps_tool.yaml"
gpsd_host="127.0.0.1" gpsd_host="127.0.0.1"
gpsd_report = () gpsd_report = ()
@ -290,11 +291,10 @@ class QtGPSWindow(MainWindow):
self.log_enabled = 0 self.log_enabled = 0
sys.exit(0) sys.exit(0)
if os.path.isfile(config_file): cfg = ConfigurationFile(__file__)
with open(config_file) as f: config = cfg.read_config()
config = yaml.load(f, Loader=yaml.FullLoader) if 'gpsd_host' in config:
if 'gpsd_host' in config: gpsd_host = config['gpsd_host']
gpsd_host = config['gpsd_host']
if __name__ == '__main__': if __name__ == '__main__':
app = QApplication(sys.argv) app = QApplication(sys.argv)

View File

@ -19,7 +19,7 @@ from PySide2.QtCore import QObject, Slot, Signal, QThread, QDir
from MainWindow import Ui_MainWindow from MainWindow import Ui_MainWindow
config_file = "vcones.yaml" from utils.configs import *
############################################################################## ##############################################################################
# audio stuff # audio stuff
@ -67,6 +67,8 @@ def roydistance(lat1, lon1, lat2, lon2):
# log # log
############################################################################## ##############################################################################
cfg = ConfigurationFile(__file__)
if not os.path.isdir("logs"): if not os.path.isdir("logs"):
mkdir("logs") mkdir("logs")
@ -558,17 +560,15 @@ class ConesWindow(MainWindow):
self.save_list(5) self.save_list(5)
def save_config(self): def save_config(self):
with open(config_file, 'w') as f: config = {}
config = {} if gpsd_host != "localhost":
if gpsd_host != "localhost": config['gpsd_host'] = gpsd_host
config['gpsd_host'] = gpsd_host config['lists'] = []
config['lists'] = [] for i in range(0, 6):
for i in range(0, 6): if self.labels[i].text() != "":
if self.labels[i].text() != "": self.save_list(i)
self.save_list(i) config['lists'].append( self.labels[i].text() )
config['lists'].append( self.labels[i].text() ) cfg.write_config(config)
f.write(yaml.dump(config))
f.close()
def save_log(self): def save_log(self):
if self.num_points: if self.num_points:
@ -592,12 +592,9 @@ if __name__ == '__main__':
window = ConesWindow() window = ConesWindow()
window.show() window.show()
config = [] config = cfg.read_config()
if os.path.isfile(config_file): if 'gpsd_host' in config:
with open(config_file) as f: gpsd_host = config['gpsd_host']
config = yaml.load(f, Loader=yaml.FullLoader)
if 'gpsd_host' in config:
gpsd_host = config['gpsd_host']
if len(sys.argv[1:]): if len(sys.argv[1:]):
window.load_cones(sys.argv[1:]) window.load_cones(sys.argv[1:])