refactor config.py
All checks were successful
Build and publish Image / build-and-push (push) Successful in 7s

This commit is contained in:
2024-02-10 05:30:54 +00:00
parent a35d0cbb58
commit 3c54175ab8

View File

@@ -1,67 +1,75 @@
#===========================================================================
#
# Config parsing
#
#===========================================================================
"""Config parsing."""
__doc__ = """Config parsing.
"""
import os
import glob
import configparser
from .util import Data
import ConfigParser
import glob
import os.path
#===========================================================================
def parse( configDir ):
# Parse the files. Default xform makes all keys lower case so set
# it to str to stop that behavior.
p = ConfigParser.ConfigParser()
p.optionxform = str
def parse(configDir):
"""
Parse configuration files from the specified directory.
files = glob.glob( os.path.join( configDir, "*.conf" ) )
for f in files:
p.read( f )
Args:
configDir (str): The directory containing the configuration files.
cfg = Data( _config = p )
for s in p.sections():
d = Data()
for o in p.options( s ):
setattr( d, o, p.get( s, o ) )
Returns:
Data: Parsed configuration data.
"""
config = configparser.ConfigParser()
config.optionxform = str # Prevent keys from being transformed to lowercase
setattr( cfg, s, d )
config_files = glob.glob(os.path.join(configDir, "*.conf"))
for file in config_files:
config.read(file)
return cfg
parsed_config = Data(_config=config)
for section in config.sections():
section_data = Data()
for option in config.options(section):
setattr(section_data, option, config.get(section, option))
setattr(parsed_config, section, section_data)
#===========================================================================
def update( data, secDef ):
for section, fields in secDef.iteritems():
if not hasattr( data, section ):
setattr( data, section, Data() )
return parsed_config
secData = data[section]
for name, convertFunc, defaultValue in fields:
if hasattr( secData, name ):
secData[name] = convertFunc( secData[name] )
def update(data, secDef):
"""
Update configuration data with default values and type conversion.
else:
secData[name] = defaultValue
Args:
data (Data): Configuration data to be updated.
secDef (dict): Dictionary defining sections and their default values and conversion functions.
"""
for section, fields in secDef.items():
if not hasattr(data, section):
setattr(data, section, Data())
#===========================================================================
def toPath( value ):
"""TODO: doc
"""
if value is None:
return None
value = str( value )
if "$" in value:
value = os.path.expandvars( value )
if "~" in value:
value = os.path.expanduser( value )
sec_data = data[section]
for name, convert_func, default_value in fields:
if hasattr(sec_data, name):
sec_data[name] = convert_func(sec_data[name])
else:
sec_data[name] = default_value
return value
def toPath(value):
"""
Convert a path value to its absolute path.
#===========================================================================
Args:
value (str): The path value.
Returns:
str: The absolute path.
"""
if value is None:
return None
value = str(value)
if "$" in value:
value = os.path.expandvars(value)
if "~" in value:
value = os.path.expanduser(value)
return value