mirror of
https://github.com/BOSWatch/BW3-Core.git
synced 2025-12-06 07:12:04 +01:00
edit config class and test
This commit is contained in:
parent
def65fe5a9
commit
e29628fb7c
|
|
@ -19,96 +19,58 @@ 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
|
||||
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
|
||||
|
||||
class Config:
|
||||
|
||||
_sharedConfig = {}
|
||||
_sharePoints = {}
|
||||
|
||||
def __init__(self, configPath="", shareName=""):
|
||||
"""!Create a new config object and load the ini file directly
|
||||
|
||||
@param configPath: If you like to load a ini file
|
||||
@param shareName: If you like to share the config"""
|
||||
def __init__(self):
|
||||
"""!Create a new config object and load the ini file directly"""
|
||||
self._config = configparser.ConfigParser()
|
||||
if configPath:
|
||||
self._loadConfigFile(configPath)
|
||||
if shareName:
|
||||
self._shareConfig(shareName)
|
||||
|
||||
def _loadConfigFile(self, configPath):
|
||||
def loadConfigFile(self, configPath, sharePoint=""):
|
||||
"""!loads a given configuration in the class wide config variable
|
||||
|
||||
@param configPath: Path to the config file
|
||||
@param sharePoint: If you like to share the config
|
||||
@return True or False"""
|
||||
logging.debug("load config file from: %s", configPath)
|
||||
try:
|
||||
self._config.read(configPath, "utf-8")
|
||||
if sharePoint:
|
||||
self._shareConfig(sharePoint)
|
||||
return True
|
||||
except: # pragma: no cover
|
||||
logging.exception("cannot load config file")
|
||||
return False
|
||||
|
||||
def _shareConfig(self, shareName):
|
||||
def _shareConfig(self, sharePoint):
|
||||
"""!Shares the configuration
|
||||
|
||||
Shares the local _config to teh class wide global _sharedConfig
|
||||
@param shareName: Name of the global share point
|
||||
@param sharePoint: Name of the global share point
|
||||
@return True or False"""
|
||||
try:
|
||||
bool(self._sharedConfig[shareName])
|
||||
logging.error("cannot share config - name is always in use: %s", shareName)
|
||||
bool(self._sharePoints[sharePoint])
|
||||
logging.error("cannot share config - name is always in use: %s", sharePoint)
|
||||
return False
|
||||
except:
|
||||
self._sharedConfig[shareName] = self._config
|
||||
logging.debug("shared configuration as: %s", shareName)
|
||||
self._sharePoints[sharePoint] = self._config
|
||||
logging.debug("shared configuration as: %s", sharePoint)
|
||||
return True
|
||||
|
||||
def getConfig(self, section, key, shareName=""):
|
||||
def getConfig(self, section, key, sharePoint=""):
|
||||
"""!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)
|
||||
@param sharePoint: Name of the global config share (empty is only local)
|
||||
@return The value or None"""
|
||||
if shareName:
|
||||
if sharePoint:
|
||||
try:
|
||||
return self._sharedConfig[shareName].get(section, key)
|
||||
return self._sharePoints[sharePoint].get(section, key)
|
||||
except KeyError:
|
||||
logging.error("no shared config named: %s", shareName)
|
||||
logging.error("no shared config named: %s", sharePoint)
|
||||
except configparser.NoSectionError:
|
||||
logging.error("no shared config section: %s", section)
|
||||
except configparser.NoOptionError:
|
||||
|
|
|
|||
|
|
@ -26,44 +26,53 @@ class Test_Config:
|
|||
|
||||
def test_loadLocalConfig(self):
|
||||
"""!load a local config file"""
|
||||
bwConfig = Config(paths.TEST_PATH + "test.ini")
|
||||
bwConfig = Config()
|
||||
bwConfig.loadConfigFile(paths.TEST_PATH + "test.ini")
|
||||
assert bwConfig._config is not None
|
||||
|
||||
def test_getLocalConfig(self):
|
||||
"""!get values from local config file"""
|
||||
bwConfig = Config(paths.TEST_PATH + "test.ini")
|
||||
bwConfig = Config()
|
||||
bwConfig.loadConfigFile(paths.TEST_PATH + "test.ini")
|
||||
assert bwConfig.getConfig("test", "one") == "1"
|
||||
assert bwConfig.getConfig("test", "two") == "two"
|
||||
assert bwConfig.getConfig("testcase", "test") == "ok"
|
||||
|
||||
def test_getLocalConfigFailed(self):
|
||||
"""!fail while get values from local config file"""
|
||||
bwConfig = Config(paths.TEST_PATH + "test.ini")
|
||||
bwConfig = Config()
|
||||
bwConfig.loadConfigFile(paths.TEST_PATH + "test.ini")
|
||||
assert bwConfig.getConfig("test", "abc") is None
|
||||
assert bwConfig.getConfig("abc", "test") is None
|
||||
|
||||
def test_shareConfig(self):
|
||||
"""!load local config file and share it"""
|
||||
bwConfig = Config(paths.TEST_PATH + "test.ini", "test_shareConfig")
|
||||
assert bwConfig._sharedConfig["test_shareConfig"] is not None
|
||||
bwConfig = Config()
|
||||
bwConfig.loadConfigFile(paths.TEST_PATH + "test.ini", "test_shareConfig")
|
||||
assert bwConfig._sharePoints["test_shareConfig"] is not None
|
||||
|
||||
def test_shareConfigUsed(self):
|
||||
"""!load local config file and tr to share it twice with same name"""
|
||||
bwConfig1 = Config(paths.TEST_PATH + "test.ini", "test_shareConfigUsed")
|
||||
assert bwConfig1._sharedConfig["test_shareConfigUsed"] is not None
|
||||
bwConfig2 = Config(paths.TEST_PATH + "test.ini")
|
||||
bwConfig1 = Config()
|
||||
bwConfig1.loadConfigFile(paths.TEST_PATH + "test.ini", "test_shareConfigUsed")
|
||||
assert bwConfig1._sharePoints["test_shareConfigUsed"] is not None
|
||||
|
||||
bwConfig2 = Config()
|
||||
bwConfig2.loadConfigFile(paths.TEST_PATH + "test.ini")
|
||||
assert bwConfig2._shareConfig("test_shareConfigUsed") is False
|
||||
|
||||
def test_getNotSetSharedConfig(self):
|
||||
"""!try to get values from shared config where not exists"""
|
||||
bwConfig = Config(paths.TEST_PATH + "test.ini")
|
||||
bwConfig = Config()
|
||||
bwConfig.loadConfigFile(paths.TEST_PATH + "test.ini")
|
||||
assert bwConfig.getConfig("test", "one") == "1"
|
||||
assert bwConfig.getConfig("test", "one", "NotSetSharedConfig") is None
|
||||
|
||||
def test_getSharedConfig(self):
|
||||
"""!get values from shared config file"""
|
||||
bwConfig1 = Config(paths.TEST_PATH + "test.ini", "test_getSharedConfig")
|
||||
assert bwConfig1._sharedConfig["test_getSharedConfig"] is not None
|
||||
bwConfig1 = Config()
|
||||
bwConfig1.loadConfigFile(paths.TEST_PATH + "test.ini", "test_getSharedConfig")
|
||||
assert bwConfig1._sharePoints["test_getSharedConfig"] is not None
|
||||
|
||||
bwConfig2 = Config()
|
||||
assert bwConfig2.getConfig("test", "one") is None
|
||||
|
|
@ -71,8 +80,9 @@ class Test_Config:
|
|||
|
||||
def test_getSharedConfigFailed(self):
|
||||
"""!fail while get values from shared config file"""
|
||||
bwConfig1 = Config(paths.TEST_PATH + "test.ini", "test_getSharedConfigFailed")
|
||||
assert bwConfig1._sharedConfig["test_getSharedConfigFailed"] is not None
|
||||
bwConfig1 = Config()
|
||||
bwConfig1.loadConfigFile(paths.TEST_PATH + "test.ini", "test_getSharedConfigFailed")
|
||||
assert bwConfig1._sharePoints["test_getSharedConfigFailed"] is not None
|
||||
|
||||
bwConfig2 = Config()
|
||||
assert bwConfig2.getConfig("test", "abc", "test_getSharedConfigFailed") is None
|
||||
|
|
|
|||
Loading…
Reference in a new issue