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 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()

View File

@ -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)

View File

@ -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:])