BW3-Core/boswatch/config.py

130 lines
4.2 KiB
Python
Raw Normal View History

2018-01-07 21:52:53 +01:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: config.py
@date: 25.12.2017
@author: Bastian Schroll
@description: Module for the configuration
"""
import logging
import configparser
logging.debug("- %s loaded", __name__)
_configFile = configparser.ConfigParser()
def loadConfig(configFile):
"""!loads a given configuration in the class wide config variable
@param configFile: Path to the config file
@return status of loading"""
logging.debug("load config file from: %s", configFile)
try:
_configFile.read(configFile, "utf-8")
return True
except: # pragma: no cover
logging.exception("cannot load config file")
return False
def getConfig(section, key):
"""!Method to read a single config entry
@param section: Section to read from
@param key: Value to read
@return The value from config file"""
try:
return _configFile.get(section, key)
except: # pragma: no cover
logging.exception("Error while reading a config entry")
return None
2018-01-08 12:34:09 +01:00
#
#
#
#
class Config:
_sharedConfig = {}
def __init__(self, configPath="", shareName=""):
"""!Create a new config object and load the ini file directly
2018-01-08 14:35:54 +01:00
@param configPath: If you like to load a ini file
@param shareName: If you like to share the config"""
self._config = configparser.ConfigParser()
if configPath:
self._loadConfigFile(configPath)
if shareName:
self._shareConfig(shareName)
2018-01-08 12:34:09 +01:00
def _loadConfigFile(self, configPath):
"""!loads a given configuration in the class wide config variable
@param configPath: Path to the config file
2018-01-08 14:35:54 +01:00
@return True or False"""
2018-01-08 12:34:09 +01:00
logging.debug("load config file from: %s", configPath)
try:
self._config.read(configPath, "utf-8")
return True
except: # pragma: no cover
logging.exception("cannot load config file")
return False
def _shareConfig(self, shareName):
"""!Shares the configuration
Shares the local _config to teh class wide global _sharedConfig
2018-01-08 14:35:54 +01:00
@param shareName: Name of the global share point
@return True or False"""
2018-01-08 12:34:09 +01:00
try:
bool(self._sharedConfig[shareName])
2018-01-08 14:35:54 +01:00
logging.error("cannot share config - name is always in use: %s", shareName)
return False
except:
2018-01-08 12:34:09 +01:00
self._sharedConfig[shareName] = self._config
logging.debug("shared configuration as: %s", shareName)
2018-01-08 14:35:54 +01:00
return True
2018-01-08 12:34:09 +01:00
def getConfig(self, section, key, shareName=""):
"""!Method to read a single config entry
@param section: Section to read from
@param key: Value to read
@param shareName: Name of the global config share (empty is only local)
2018-01-08 14:35:54 +01:00
@return The value or None"""
if shareName:
try:
2018-01-08 12:34:09 +01:00
return self._sharedConfig[shareName].get(section, key)
2018-01-08 14:35:54 +01:00
except KeyError:
logging.error("no shared config named: %s", shareName)
except configparser.NoSectionError:
logging.error("no shared config section: %s", section)
except configparser.NoOptionError:
logging.error("no shared config option: %s", key)
except: # pragma: no cover
logging.exception("error while reading shared config")
return None
else:
try:
2018-01-08 12:34:09 +01:00
return self._config.get(section, key)
2018-01-08 14:35:54 +01:00
except configparser.NoSectionError:
logging.error("no local config section: %s", section)
except configparser.NoOptionError:
logging.error("no local config option: %s", key)
except: # pragma: no cover
logging.exception("error while reading local config")
2018-01-08 12:34:09 +01:00
return None