From 5d1e98eee6125125d1ad3c8274fba91221281d55 Mon Sep 17 00:00:00 2001 From: Neal Probert Date: Thu, 18 Nov 2021 07:23:23 -0500 Subject: [PATCH] Added configuration file handler and updated apps --- classes/utils/configs.py | 39 +++++++++++++++++++++++++-------------- gps_tool/gps_tool.py | 12 ++++++------ gps_vcones/vcones_gui.py | 33 +++++++++++++++------------------ 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/classes/utils/configs.py b/classes/utils/configs.py index b915734..f2764bf 100644 --- a/classes/utils/configs.py +++ b/classes/utils/configs.py @@ -7,31 +7,42 @@ Created on Fri Jan 8 10:26:56 2021 """ import os -import system import yaml -class ArrowHandle(): - def __init__(self, icon): - self.pathfilename = "./" + "config.yaml" +class ConfigurationFile(): + def __init__(self, app_path): + 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 = [] - def find_config(self, path=".", file="config.yaml"): + def find_config(self): # local first - if path.isfile(file): - self.pathfilename = file + if os.path.isfile(self.cfg_file): + self.use_path = self.cfg_file return 1 - elif path.isfile(path + "/" + file): - self.pathfilename = path + "/" + file + elif os.path.isfile(self.cfg_path + "/" + self.cfg_file): + self.use_path = self.cfg_path + "/" + self.cfg_file return 1 else: - print("Config file not found:", file) + print("Config file not found:", self.cfg_file) return 0 - def read_config(self, path=".", file="config.yaml"): - if self.find_config(path, file): - with open(self.pathfilename) as f: + def read_config(self): + if self.find_config(): +# 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) f.close() else: - self.config= [] + 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() + \ No newline at end of file diff --git a/gps_tool/gps_tool.py b/gps_tool/gps_tool.py index 12cdb1f..1a18577 100755 --- a/gps_tool/gps_tool.py +++ b/gps_tool/gps_tool.py @@ -26,6 +26,8 @@ from MainWindow import Ui_MainWindow import maidenhead as mh +from utils.configs import * + ############################################################################## # log ############################################################################## @@ -41,7 +43,6 @@ fix_mode = [ '0=None', '1=No', '2=2D', '3=3D' ]; -config_file = "gps_tool.yaml" gpsd_host="127.0.0.1" gpsd_report = () @@ -290,11 +291,10 @@ class QtGPSWindow(MainWindow): self.log_enabled = 0 sys.exit(0) -if os.path.isfile(config_file): - with open(config_file) as f: - config = yaml.load(f, Loader=yaml.FullLoader) - if 'gpsd_host' in config: - gpsd_host = config['gpsd_host'] +cfg = ConfigurationFile(__file__) +config = cfg.read_config() +if 'gpsd_host' in config: + gpsd_host = config['gpsd_host'] if __name__ == '__main__': app = QApplication(sys.argv) diff --git a/gps_vcones/vcones_gui.py b/gps_vcones/vcones_gui.py index 5941556..cab0e51 100755 --- a/gps_vcones/vcones_gui.py +++ b/gps_vcones/vcones_gui.py @@ -19,7 +19,7 @@ from PySide2.QtCore import QObject, Slot, Signal, QThread, QDir from MainWindow import Ui_MainWindow -config_file = "vcones.yaml" +from utils.configs import * ############################################################################## # audio stuff @@ -67,6 +67,8 @@ def roydistance(lat1, lon1, lat2, lon2): # log ############################################################################## +cfg = ConfigurationFile(__file__) + if not os.path.isdir("logs"): mkdir("logs") @@ -558,17 +560,15 @@ class ConesWindow(MainWindow): self.save_list(5) def save_config(self): - with open(config_file, 'w') as f: - config = {} - if gpsd_host != "localhost": - config['gpsd_host'] = gpsd_host - config['lists'] = [] - for i in range(0, 6): - if self.labels[i].text() != "": - self.save_list(i) - config['lists'].append( self.labels[i].text() ) - f.write(yaml.dump(config)) - f.close() + config = {} + if gpsd_host != "localhost": + config['gpsd_host'] = gpsd_host + config['lists'] = [] + for i in range(0, 6): + if self.labels[i].text() != "": + self.save_list(i) + config['lists'].append( self.labels[i].text() ) + cfg.write_config(config) def save_log(self): if self.num_points: @@ -592,12 +592,9 @@ if __name__ == '__main__': window = ConesWindow() window.show() - config = [] - if os.path.isfile(config_file): - with open(config_file) as f: - config = yaml.load(f, Loader=yaml.FullLoader) - if 'gpsd_host' in config: - gpsd_host = config['gpsd_host'] + config = cfg.read_config() + if 'gpsd_host' in config: + gpsd_host = config['gpsd_host'] if len(sys.argv[1:]): window.load_cones(sys.argv[1:])