Compare commits
No commits in common. "3a43b8d07db334f754c65c4d1a44833fa60d5857" and "e9477b15664e5666c6e0082e1ae581f16c1d9dd6" have entirely different histories.
3a43b8d07d
...
e9477b1566
1 changed files with 31 additions and 45 deletions
|
@ -43,7 +43,6 @@ class BaseOption: # pylint: disable=too-many-instance-attributes
|
||||||
self.arg = arg
|
self.arg = arg
|
||||||
self.short_arg = short_arg
|
self.short_arg = short_arg
|
||||||
self.arg_help = arg_help if arg_help else comment
|
self.arg_help = arg_help if arg_help else comment
|
||||||
self._set = False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _isset_in_options(self):
|
def _isset_in_options(self):
|
||||||
|
@ -71,7 +70,7 @@ class BaseOption: # pylint: disable=too-many-instance-attributes
|
||||||
def _isset_in_config_file(self):
|
def _isset_in_config_file(self):
|
||||||
""" Check if option is defined in the loaded configuration file """
|
""" Check if option is defined in the loaded configuration file """
|
||||||
return (
|
return (
|
||||||
self.config.config_filepath
|
self.config.config_parser
|
||||||
and self.config.config_parser.has_option(self.section.name, self.name)
|
and self.config.config_parser.has_option(self.section.name, self.name)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -94,32 +93,36 @@ class BaseOption: # pylint: disable=too-many-instance-attributes
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
""" Get option value from options, config or default """
|
""" Get option value from options, config or default """
|
||||||
if self._isset_in_options and not self._set:
|
if self._isset_in_options:
|
||||||
return self._from_options
|
return self._from_options
|
||||||
if self._isset_in_config_file:
|
if self._isset_in_config_file:
|
||||||
return self._from_config
|
return self._from_config
|
||||||
return self.default
|
return self.default
|
||||||
|
|
||||||
def set(self, value):
|
def set(self, value):
|
||||||
""" Set option value to config file and options """
|
""" Set option value to config file """
|
||||||
|
assert self.config.config_parser or (self.config.options and not self.no_arg), (
|
||||||
|
"Can't set option value: configuration file not configured, not options (or no argument)")
|
||||||
if value == '':
|
if value == '':
|
||||||
value = None
|
value = None
|
||||||
|
|
||||||
if value == self.default or value is None:
|
if self.config.config_parser:
|
||||||
# Remove option from config (is section exists)
|
if value == self.default or value is None:
|
||||||
if self.config.config_parser.has_section(self.section.name):
|
# Remove option from config (is section exists)
|
||||||
self.config.config_parser.remove_option(
|
if self.config.config_parser.has_section(self.section.name):
|
||||||
self.section.name, self.name)
|
self.config.config_parser.remove_option(
|
||||||
else:
|
self.section.name, self.name)
|
||||||
# Store option to config
|
else:
|
||||||
if not self.config.config_parser.has_section(self.section.name):
|
# Store option to config
|
||||||
self.config.config_parser.add_section(self.section.name)
|
if not self.config.config_parser.has_section(self.section.name):
|
||||||
|
self.config.config_parser.add_section(self.section.name)
|
||||||
|
|
||||||
self.config.config_parser.set(
|
self.config.config_parser.set(
|
||||||
self.section.name, self.name, self.to_config(value)
|
self.section.name, self.name, self.to_config(value)
|
||||||
)
|
)
|
||||||
|
|
||||||
self._set = True
|
if self.config.options and not self.no_arg:
|
||||||
|
setattr(self.config.options, self.parser_dest, value)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parser_action(self):
|
def parser_action(self):
|
||||||
|
@ -619,7 +622,6 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
self._filepath = None
|
self._filepath = None
|
||||||
self.config_file_env_variable = config_file_env_variable
|
self.config_file_env_variable = config_file_env_variable
|
||||||
self.default_config_dirpath = default_config_dirpath
|
self.default_config_dirpath = default_config_dirpath
|
||||||
self._init_config_parser()
|
|
||||||
|
|
||||||
def add_section(self, name, loaded_callback=None, **kwargs):
|
def add_section(self, name, loaded_callback=None, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -636,7 +638,7 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
if loaded_callback:
|
if loaded_callback:
|
||||||
self._loaded_callbacks.append(loaded_callback)
|
self._loaded_callbacks.append(loaded_callback)
|
||||||
# If configuration is already loaded, execute callback immediatly
|
# If configuration is already loaded, execute callback immediatly
|
||||||
if self._filepath or self.options:
|
if self.config_parser or self.options:
|
||||||
self._loaded()
|
self._loaded()
|
||||||
return self.sections[name]
|
return self.sections[name]
|
||||||
|
|
||||||
|
@ -650,6 +652,7 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
|
|
||||||
def get(self, section, option):
|
def get(self, section, option):
|
||||||
""" Get option value """
|
""" Get option value """
|
||||||
|
assert self.config_parser or self.options, 'Unconfigured options parser'
|
||||||
assert self.defined(
|
assert self.defined(
|
||||||
section, option), f'Unknown option {section}.{option}'
|
section, option), f'Unknown option {section}.{option}'
|
||||||
value = self.sections[section].get(option)
|
value = self.sections[section].get(option)
|
||||||
|
@ -663,24 +666,21 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
|
assert self.config_parser or self.options, 'Unconfigured options parser'
|
||||||
assert key in self.sections, f'Unknown section {key}'
|
assert key in self.sections, f'Unknown section {key}'
|
||||||
return ConfigSectionAsDictWrapper(self.sections[key])
|
return ConfigSectionAsDictWrapper(self.sections[key])
|
||||||
|
|
||||||
def set(self, section, option, value):
|
def set(self, section, option, value):
|
||||||
""" Set option value """
|
""" Set option value """
|
||||||
|
assert self.config_parser, 'Unconfigured options parser'
|
||||||
assert self.defined(
|
assert self.defined(
|
||||||
section, option), f'Unknown option {section}.{option}'
|
section, option), f'Unknown option {section}.{option}'
|
||||||
self._init_config_parser()
|
|
||||||
self.sections[section].set(option, value)
|
self.sections[section].set(option, value)
|
||||||
|
|
||||||
def _init_config_parser(self, force=False):
|
|
||||||
""" Initialize ConfigParser object """
|
|
||||||
if not self.config_parser or force:
|
|
||||||
self.config_parser = ConfigParser(defaults={'hash': '#'})
|
|
||||||
|
|
||||||
def load_file(self, filepath, execute_callback=True):
|
def load_file(self, filepath, execute_callback=True):
|
||||||
""" Read configuration file """
|
""" Read configuration file """
|
||||||
self._init_config_parser(force=True)
|
|
||||||
|
self.config_parser = ConfigParser(defaults={'hash': '#'})
|
||||||
self._filepath = filepath
|
self._filepath = filepath
|
||||||
|
|
||||||
# Checking access of configuration file
|
# Checking access of configuration file
|
||||||
|
@ -693,7 +693,7 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
try:
|
try:
|
||||||
self.config_parser.read(filepath, encoding=self.encoding)
|
self.config_parser.read(filepath, encoding=self.encoding)
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
self._init_config_parser(force=True)
|
self.config_parser = None
|
||||||
log.exception('Failed to read configuration file %s', filepath)
|
log.exception('Failed to read configuration file %s', filepath)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -847,8 +847,7 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
return self.options_parser
|
return self.options_parser
|
||||||
|
|
||||||
def parse_arguments_options(self, argv=None, parser=None, create=True, ask_values=True,
|
def parse_arguments_options(self, argv=None, parser=None, create=True, ask_values=True,
|
||||||
exit_after_created=True, execute_callback=True,
|
exit_after_created=True, execute_callback=True):
|
||||||
hardcoded_options=None):
|
|
||||||
"""
|
"""
|
||||||
Parse arguments options
|
Parse arguments options
|
||||||
|
|
||||||
|
@ -863,11 +862,6 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
creation (default: True)
|
creation (default: True)
|
||||||
:param execute_callback: Sections's loaded callbacks will be executed only if True
|
:param execute_callback: Sections's loaded callbacks will be executed only if True
|
||||||
(default: True)
|
(default: True)
|
||||||
:param hardcoded_options: Optional hard-coded options to set after loading arguments
|
|
||||||
and configuration file. These options are passed using an
|
|
||||||
list of tuple of 3 elements: the section and the option
|
|
||||||
names and the value.
|
|
||||||
[('section1', 'option1', value), ...]
|
|
||||||
"""
|
"""
|
||||||
parser = parser if parser else self.get_arguments_parser()
|
parser = parser if parser else self.get_arguments_parser()
|
||||||
argcomplete.autocomplete(parser)
|
argcomplete.autocomplete(parser)
|
||||||
|
@ -905,17 +899,6 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
elif options.verbose:
|
elif options.verbose:
|
||||||
logging.getLogger().setLevel(logging.INFO)
|
logging.getLogger().setLevel(logging.INFO)
|
||||||
|
|
||||||
if hardcoded_options:
|
|
||||||
assert isinstance(hardcoded_options, list), (
|
|
||||||
'hardcoded_options must be a list of tuple of 3 elements: '
|
|
||||||
'the section and the option names and the value.')
|
|
||||||
for opt_info in hardcoded_options:
|
|
||||||
assert isinstance(opt_info, tuple) and len(opt_info) == 3, (
|
|
||||||
'Invalid hard-coded option value: it must be a tuple of 3 '
|
|
||||||
'elements: the section and the option names and the value.'
|
|
||||||
)
|
|
||||||
self.set(*opt_info)
|
|
||||||
|
|
||||||
if self.get('console', 'enabled'):
|
if self.get('console', 'enabled'):
|
||||||
stdout_console_handler = logging.StreamHandler(
|
stdout_console_handler = logging.StreamHandler(
|
||||||
sys.stderr if self.get('console', 'force_stderr')
|
sys.stderr if self.get('console', 'force_stderr')
|
||||||
|
@ -970,6 +953,9 @@ class Config: # pylint: disable=too-many-instance-attributes
|
||||||
section and their options value.
|
section and their options value.
|
||||||
:rtype: bool of dict
|
:rtype: bool of dict
|
||||||
"""
|
"""
|
||||||
|
# On set it mode, ensure configuration file parser is initialized
|
||||||
|
if set_it and not self.config_parser:
|
||||||
|
self.config_parser = ConfigParser()
|
||||||
result = {}
|
result = {}
|
||||||
error = False
|
error = False
|
||||||
for name, section in self.sections.items():
|
for name, section in self.sections.items():
|
||||||
|
|
Loading…
Reference in a new issue