Added configuration file handler and updated apps
This commit is contained in:
@ -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()
|
||||
|
||||
Reference in New Issue
Block a user