48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Fri Jan 8 10:26:56 2021
|
|
|
|
@author: neal
|
|
"""
|
|
|
|
import os
|
|
import 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):
|
|
# local first
|
|
if os.path.isfile(self.cfg_file):
|
|
self.use_path = self.cfg_file
|
|
return 1
|
|
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:", self.cfg_file)
|
|
return 0
|
|
|
|
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 = []
|
|
return self.config
|
|
|
|
def write_config(self, config):
|
|
with open(self.use_path, 'w') as f:
|
|
f.write(yaml.dump(config))
|
|
f.close()
|
|
|