2019-02-27 08:02:40 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""!
|
|
|
|
|
____ ____ ______ __ __ __ _____
|
|
|
|
|
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
|
|
|
|
|
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
|
|
|
|
|
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
|
|
|
|
|
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
|
|
|
|
|
German BOS Information Script
|
|
|
|
|
by Bastian Schroll
|
|
|
|
|
|
2019-02-27 08:39:54 +01:00
|
|
|
@file: configYaml.py
|
2019-02-27 08:02:40 +01:00
|
|
|
@date: 27.02.2019
|
|
|
|
|
@author: Bastian Schroll
|
2019-03-01 12:09:12 +01:00
|
|
|
@description: Module for the configuration in YAML format
|
2019-02-27 08:02:40 +01:00
|
|
|
"""
|
|
|
|
|
import logging
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
logging.debug("- %s loaded", __name__)
|
|
|
|
|
|
|
|
|
|
|
2019-03-01 12:09:12 +01:00
|
|
|
class ConfigYAML:
|
|
|
|
|
|
|
|
|
|
def __init__(self, config=None):
|
2019-03-01 22:12:29 +01:00
|
|
|
self._config = config
|
2019-03-01 12:09:12 +01:00
|
|
|
|
|
|
|
|
def __iter__(self):
|
2019-03-01 22:12:29 +01:00
|
|
|
for item in self._config:
|
2019-03-01 12:09:12 +01:00
|
|
|
if type(item) is list or type(item) is dict:
|
|
|
|
|
yield ConfigYAML(item)
|
|
|
|
|
else:
|
|
|
|
|
yield item
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-03-01 22:12:29 +01:00
|
|
|
return str(self._config)
|
2019-03-01 12:09:12 +01:00
|
|
|
|
|
|
|
|
def loadConfigFile(self, configPath):
|
|
|
|
|
"""!loads a given configuration
|
|
|
|
|
|
|
|
|
|
@param configPath: Path to the config file
|
|
|
|
|
@return True or False"""
|
|
|
|
|
logging.debug("load config file from: %s", configPath)
|
|
|
|
|
try:
|
|
|
|
|
with open(configPath) as file:
|
|
|
|
|
# use safe_load instead load
|
2019-03-01 22:12:29 +01:00
|
|
|
self._config = yaml.safe_load(file)
|
2019-03-01 12:09:12 +01:00
|
|
|
return True
|
|
|
|
|
except: # pragma: no cover
|
|
|
|
|
logging.exception("cannot load config file")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def get(self, *args, default=None):
|
2019-03-01 22:12:29 +01:00
|
|
|
tmp = self._config
|
2019-03-01 12:09:12 +01:00
|
|
|
try:
|
|
|
|
|
for arg in args:
|
|
|
|
|
tmp = tmp.get(arg, default)
|
|
|
|
|
if type(tmp) is list or type(tmp) is dict:
|
|
|
|
|
return ConfigYAML(tmp)
|
|
|
|
|
else:
|
|
|
|
|
return tmp
|
|
|
|
|
except AttributeError:
|
|
|
|
|
return default
|