Refactor code to be more readable #46
@@ -1,67 +1,75 @@
|
|||||||
#===========================================================================
|
"""Config parsing."""
|
||||||
#
|
|
||||||
# Config parsing
|
|
||||||
#
|
|
||||||
#===========================================================================
|
|
||||||
|
|
||||||
__doc__ = """Config parsing.
|
import os
|
||||||
"""
|
import glob
|
||||||
|
import configparser
|
||||||
|
|
||||||
from .util import Data
|
from .util import Data
|
||||||
import ConfigParser
|
|
||||||
import glob
|
|
||||||
import os.path
|
|
||||||
|
|
||||||
#===========================================================================
|
def parse(configDir):
|
||||||
def parse( configDir ):
|
"""
|
||||||
# Parse the files. Default xform makes all keys lower case so set
|
Parse configuration files from the specified directory.
|
||||||
# it to str to stop that behavior.
|
|
||||||
p = ConfigParser.ConfigParser()
|
|
||||||
p.optionxform = str
|
|
||||||
|
|
||||||
files = glob.glob( os.path.join( configDir, "*.conf" ) )
|
Args:
|
||||||
for f in files:
|
configDir (str): The directory containing the configuration files.
|
||||||
p.read( f )
|
|
||||||
|
|
||||||
cfg = Data( _config = p )
|
Returns:
|
||||||
for s in p.sections():
|
Data: Parsed configuration data.
|
||||||
d = Data()
|
"""
|
||||||
for o in p.options( s ):
|
config = configparser.ConfigParser()
|
||||||
setattr( d, o, p.get( s, o ) )
|
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)
|
||||||
|
|
||||||
#===========================================================================
|
return parsed_config
|
||||||
def update( data, secDef ):
|
|
||||||
for section, fields in secDef.iteritems():
|
|
||||||
if not hasattr( data, section ):
|
|
||||||
setattr( data, section, Data() )
|
|
||||||
|
|
||||||
secData = data[section]
|
def update(data, secDef):
|
||||||
for name, convertFunc, defaultValue in fields:
|
"""
|
||||||
if hasattr( secData, name ):
|
Update configuration data with default values and type conversion.
|
||||||
secData[name] = convertFunc( secData[name] )
|
|
||||||
|
|
||||||
else:
|
Args:
|
||||||
secData[name] = defaultValue
|
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())
|
||||||
|
|
||||||
#===========================================================================
|
sec_data = data[section]
|
||||||
def toPath( value ):
|
for name, convert_func, default_value in fields:
|
||||||
"""TODO: doc
|
if hasattr(sec_data, name):
|
||||||
"""
|
sec_data[name] = convert_func(sec_data[name])
|
||||||
if value is None:
|
else:
|
||||||
return None
|
sec_data[name] = default_value
|
||||||
|
|
||||||
value = str( value )
|
|
||||||
|
|
||||||
if "$" in value:
|
|
||||||
value = os.path.expandvars( value )
|
|
||||||
|
|
||||||
if "~" in value:
|
|
||||||
value = os.path.expanduser( 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
|
||||||
Reference in New Issue
Block a user