Config: Add config_file_env_variable parameter
The new config_file_env_variable parameter allow to specify environment variable name that will be used to determine configuration file path if it's not explicitly provided using -c/--config parameter.
This commit is contained in:
parent
e6ce412db0
commit
8438f95e68
1 changed files with 9 additions and 3 deletions
|
@ -412,7 +412,8 @@ class RawWrappedTextHelpFormatter(argparse.RawDescriptionHelpFormatter):
|
||||||
class Config:
|
class Config:
|
||||||
""" Configuration helper """
|
""" Configuration helper """
|
||||||
|
|
||||||
def __init__(self, appname, shortname=None, version=None, encoding=None):
|
def __init__(self, appname, shortname=None, version=None, encoding=None,
|
||||||
|
config_file_env_variable=None):
|
||||||
self.appname = appname
|
self.appname = appname
|
||||||
self.shortname = shortname
|
self.shortname = shortname
|
||||||
self.version = version if version else '0.0'
|
self.version = version if version else '0.0'
|
||||||
|
@ -424,6 +425,7 @@ class Config:
|
||||||
self._loaded_callbacks = []
|
self._loaded_callbacks = []
|
||||||
self._loaded_callbacks_executed = []
|
self._loaded_callbacks_executed = []
|
||||||
self._filepath = None
|
self._filepath = None
|
||||||
|
self.config_file_env_variable = config_file_env_variable
|
||||||
|
|
||||||
def add_section(self, name, loaded_callback=None, **kwargs):
|
def add_section(self, name, loaded_callback=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -567,12 +569,14 @@ class Config:
|
||||||
formatter_class=RawWrappedTextHelpFormatter,
|
formatter_class=RawWrappedTextHelpFormatter,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
|
|
||||||
|
config_file_help = 'Configuration file to use (default: %s)' % self.config_filepath
|
||||||
|
if self.config_file_env_variable:
|
||||||
|
config_file_help += '\n\nYou also could set %s environment variable to specify your configuration file path.' % self.config_file_env_variable
|
||||||
self.options_parser.add_argument(
|
self.options_parser.add_argument(
|
||||||
'-c',
|
'-c',
|
||||||
'--config',
|
'--config',
|
||||||
default=self.config_filepath,
|
default=self.config_filepath,
|
||||||
help='Configuration file to use (default: %s)' %
|
help=config_file_help
|
||||||
self.config_filepath
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.options_parser.add_argument(
|
self.options_parser.add_argument(
|
||||||
|
@ -684,6 +688,8 @@ class Config:
|
||||||
""" Retrieve configuration file path """
|
""" Retrieve configuration file path """
|
||||||
if self._filepath:
|
if self._filepath:
|
||||||
return self._filepath
|
return self._filepath
|
||||||
|
if self.config_file_env_variable and os.environ.get(self.config_file_env_variable):
|
||||||
|
return os.environ.get(self.config_file_env_variable)
|
||||||
return os.path.join(
|
return os.path.join(
|
||||||
self.config_dir,
|
self.config_dir,
|
||||||
("%s.ini" % self.shortname) if self.shortname
|
("%s.ini" % self.shortname) if self.shortname
|
||||||
|
|
Loading…
Reference in a new issue