aboutsummaryrefslogtreecommitdiffstats
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
parentAdd section on using your own parser (diff)
downloadconfig-file-9c8c6c1fc2ccf31bc8c709ab052c34a45e207b36.zip
Add support for retrieving entire sections with get()
-rw-r--r--CHANGELOG.md4
-rw-r--r--README.md12
-rw-r--r--config_file/parsers/ini_parser.py16
-rw-r--r--tests/test_ini_parser.py10
4 files changed, 34 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5306057..d90dc54 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+## Unreleased
+
+ - Add support for retrieving entire sections as a `dict` with `get()`.
+
## 0.4.0 - 2020-01-19
- Add `reset()` and `save()` methods to `ConfigFile`. This allows you to reset your
diff --git a/README.md b/README.md
index bb76e0f..708ad83 100644
--- a/README.md
+++ b/README.md
@@ -46,19 +46,19 @@ config.stringify()
>>> '[calendar]\ntoday = monday\nstart_week_on_sunday = false\ntoday_index = 0\nquarter_hours_passed = 0.25\n\n'
```
-### Retrieve values
+### Retrieve values or sections
A section.key format is used for retrieving and setting values.
```python
-config.get("calendar.today")
->>> 'monday'
-
# Values from the config file are automatically parsed
config.get("calendar.start_week_on_sunday")
>>> False
# Unless you don't want them to be parsed
-config.get("calendar.today_index", parse_type=False)
->>> '0'
+config.get("calendar.start_week_on_sunday", parse_type=False)
+>>> 'false'
+
+config.get("calendar")
+>>> {'today': 'monday', 'start_week_on_sunday': False, 'today_index': 0, 'quarter_hours_passed': 0.25}
```
### Set values
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
diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py
index da1b43e..7af3ea4 100644
--- a/tests/test_ini_parser.py
+++ b/tests/test_ini_parser.py
@@ -20,6 +20,16 @@ def test_incorrect_ini_formats(ini_file):
("test_section.strkey", "blah", False),
("test_section.boolkey", "false", False),
("test_section.floatkey", "5.3", False),
+ (
+ "test_section",
+ {"intkey": "5", "strkey": "blah", "boolkey": "false", "floatkey": "5.3"},
+ False,
+ ),
+ (
+ "test_section",
+ {"intkey": 5, "strkey": "blah", "boolkey": False, "floatkey": 5.3},
+ True,
+ ),
],
)
def test_that_ini_parser_can_get_values(section_key, expected_value, parse_type):