aboutsummaryrefslogtreecommitdiffstats
path: root/config_file
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-01-19 14:40:13 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-01-19 14:40:13 -0600
commit9c8c6c1fc2ccf31bc8c709ab052c34a45e207b36 (patch)
tree54dcc521d4390880b89166deffbd5709d97b1e44 /config_file
parentAdd section on using your own parser (diff)
downloadconfig-file-9c8c6c1fc2ccf31bc8c709ab052c34a45e207b36.zip
Add support for retrieving entire sections with get()
Diffstat (limited to 'config_file')
-rw-r--r--config_file/parsers/ini_parser.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py
index 3e93e4c..549ed37 100644
--- a/config_file/parsers/ini_parser.py
+++ b/config_file/parsers/ini_parser.py
@@ -33,15 +33,27 @@ class IniParser(BaseParser):
:raises ParsingError: if the specified `section.key` is not found, in an
invalid format, or if we are unable to coerce the return value to value_type.
"""
- # TODO: Support for retrieving entire sections instead of just key/values
- section, key = split_on_dot(section_key, only_last_dot=True)
+ if "." not in section_key:
+ return self.__retrieve_section(section_key, parse_type)
try:
+ section, key = split_on_dot(section_key, only_last_dot=True)
value = self.parsed_content.get(section, key)
return parse_value(value) if parse_type else value
except configparser.Error as error:
raise ParsingError(error.message)
+ def __retrieve_section(self, section, parse_type):
+ items = dict(self.parsed_content.items(section))
+
+ if not parse_type:
+ return items
+
+ for item in items:
+ items[item] = parse_value(items[item])
+
+ return items
+
def set(self, section_key, value):
"""
Sets the value of 'section.key' of the config file. If the specified section