From e9f8a1ac8e4c822d680f24072d0852c341eb6737 Mon Sep 17 00:00:00 2001 From: Jiashuo Li Date: Mon, 27 Jul 2020 16:50:30 +0800 Subject: [PATCH] [Config] Support listing sections (#217) --- knack/config.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/knack/config.py b/knack/config.py index d5ccf21..9f19243 100644 --- a/knack/config.py +++ b/knack/config.py @@ -97,6 +97,16 @@ def get(self, section, option, fallback=_UNSET): raise last_ex # pylint:disable=raising-bad-type return fallback + def sections(self): + combined_sections = [] + # Go through the config chain and combine all sections + for config in self._config_file_chain if self.use_local_config else self._config_file_chain[-1:]: + sections = config.sections() + for section in sections: + if section not in combined_sections: + combined_sections.append(section) + return combined_sections + def items(self, section): import re pattern = self.env_var_name(section, '.+') @@ -174,6 +184,9 @@ def __init__(self, config_dir, config_path, config_comment=None): def items(self, section): return self.config_parser.items(section) if self.config_parser else [] + def sections(self): + return self.config_parser.sections() if self.config_parser else [] + def has_option(self, section, option): return self.config_parser.has_option(section, option) if self.config_parser else False @@ -234,6 +247,3 @@ def clear(self): for section in self.config_parser.sections(): self.config_parser.remove_section(section) self.set(self.config_parser) - - def sections(self): - return self.config_parser.sections()