1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 12:53:00 +00:00
kiwi-scp/src/kiwi/config.py

95 lines
2.4 KiB
Python
Raw Normal View History

2020-08-11 15:23:24 +00:00
import copy
2020-08-06 11:43:45 +00:00
import logging
2020-08-06 01:45:12 +00:00
import re
2020-08-06 12:34:25 +00:00
import os
2020-08-04 18:24:19 +00:00
import yaml
2020-08-11 12:03:00 +00:00
from ._constants import KIWI_ROOT, KIWI_CONF_NAME
2020-08-04 18:24:19 +00:00
2020-08-06 11:43:45 +00:00
###########
# CONSTANTS
2020-08-04 14:52:30 +00:00
2020-08-11 12:03:00 +00:00
HEADER_KIWI_CONF_NAME = f"{KIWI_ROOT}/kiwi_header.yml"
DEFAULT_KIWI_CONF_NAME = f"{KIWI_ROOT}/kiwi_default.yml"
2020-08-10 15:39:33 +00:00
VERSION_TAG_NAME = f"{KIWI_ROOT}/version-tag"
2020-08-06 01:45:12 +00:00
2020-08-04 14:52:30 +00:00
2020-08-06 11:43:45 +00:00
class Config:
__yml_content = {}
2020-08-06 01:45:12 +00:00
2020-08-06 11:43:45 +00:00
def __key_resolve(self, key):
2020-08-05 00:08:55 +00:00
# "a:b:c" => path = ['a', 'b'], key = 'c'
path = key.split(':')
2020-08-06 01:45:12 +00:00
path, key = path[:-1], path[-1]
2020-08-04 18:24:19 +00:00
2020-08-05 00:08:55 +00:00
# resolve path
2020-08-06 11:43:45 +00:00
container = self.__yml_content
2020-08-04 18:24:19 +00:00
for step in path:
2020-08-06 11:43:45 +00:00
container = container[step]
2020-08-04 18:24:19 +00:00
2020-08-06 11:43:45 +00:00
return container, key
2020-08-06 01:45:12 +00:00
2020-08-06 12:34:25 +00:00
def __getitem__(self, key):
container, key = self.__key_resolve(key)
return container[key]
2020-08-06 11:43:45 +00:00
def __setitem__(self, key, value):
container, key = self.__key_resolve(key)
container[key] = value
2020-08-06 01:45:12 +00:00
2020-08-06 12:34:25 +00:00
def __str__(self):
# dump yml content
yml_string = yaml.dump(self.__yml_content, default_flow_style=False, sort_keys=False)
# insert newline before every main key
yml_string = re.sub(r'^(\S)', r'\n\1', yml_string, flags=re.MULTILINE)
2020-08-11 12:03:00 +00:00
# load header comment from file
with open(HEADER_KIWI_CONF_NAME, 'r') as stream:
yml_string = stream.read() + yml_string
2020-08-06 12:34:25 +00:00
return yml_string
2020-08-06 01:45:12 +00:00
def _update_from_file(self, filename):
2020-08-06 11:43:45 +00:00
with open(filename, 'r') as stream:
try:
self.__yml_content.update(yaml.safe_load(stream))
except yaml.YAMLError as exc:
logging.error(exc)
2020-08-04 18:24:19 +00:00
2020-08-11 15:23:24 +00:00
def clone(self):
result = Config()
result.__yml_content = copy.deepcopy(self.__yml_content)
return result
def save(self):
with open(KIWI_CONF_NAME, 'w') as stream:
2020-08-06 12:34:25 +00:00
stream.write(str(self))
class DefaultConfig(Config):
__instance = None
2020-08-06 11:43:45 +00:00
@classmethod
def get(cls):
if cls.__instance is None:
cls.__instance = cls()
cls.__instance._update_from_file(DEFAULT_KIWI_CONF_NAME)
2020-08-06 01:45:12 +00:00
with open(VERSION_TAG_NAME, 'r') as stream:
cls.__instance["version"] = stream.read().strip()
2020-08-06 01:45:12 +00:00
return cls.__instance
2020-08-06 01:45:12 +00:00
class LoadedConfig(Config):
2020-08-06 11:43:45 +00:00
@classmethod
def get(cls):
2020-08-11 15:23:24 +00:00
result = DefaultConfig.get().clone()
2020-08-06 12:34:25 +00:00
if os.path.isfile(KIWI_CONF_NAME):
result._update_from_file(KIWI_CONF_NAME)
2020-08-04 18:24:19 +00:00
2020-08-06 11:43:45 +00:00
return result