aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-02-09 13:54:09 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-02-09 13:54:09 -0600
commit5fa867ba5a2e0927e9b87a8228989357924623d2 (patch)
tree76f298bf573a7d8839562e910874688c992029b7
parentAdd requests to tox (diff)
downloadconfig-file-5fa867ba5a2e0927e9b87a8228989357924623d2.zip
Allow a default value for the get() method
-rw-r--r--CHANGELOG.md7
-rw-r--r--config_file/config_file.py10
-rw-r--r--config_file/parsers/json_parser.py13
-rw-r--r--tests/test_config_file.py17
4 files changed, 40 insertions, 7 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 51f55cf..7a8e5af 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,13 @@ 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
+
+Added
+
+ - `default` optional parameter to the `get()` method of `ConfigFile`.
+ The allows a default value to be fallen back to if the given key is missing.
+
## 0.8.0 - 2020-02-02
Added
diff --git a/config_file/config_file.py b/config_file/config_file.py
index d9f9a2e..524272c 100644
--- a/config_file/config_file.py
+++ b/config_file/config_file.py
@@ -2,7 +2,7 @@ import inspect
from pathlib import Path, PurePath
from shutil import copyfile
-from config_file.parsers.base_parser import BaseParser
+from config_file.parsers.base_parser import BaseParser, ParsingError
from config_file.parsers.ini_parser import IniParser
from config_file.parsers.json_parser import JsonParser
from config_file.utils import split_on_dot
@@ -66,7 +66,7 @@ class ConfigFile:
with open(self.path, "r") as file:
return file.read()
- def get(self, key: str, parse_type: bool = True):
+ def get(self, key: str, parse_type: bool = True, default=None):
"""
Retrieve the value of a key in its native type.
This means the string 'true' will be parsed back as the
@@ -75,6 +75,12 @@ class ConfigFile:
If parse_type is set to False, all values will be returned
back as strings.
"""
+ if default is not None:
+ try:
+ return self.parser.get(key, parse_type=parse_type)
+ except ParsingError:
+ return default
+
return self.parser.get(key, parse_type=parse_type)
def set(self, key: str, value):
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
index 8e4617b..9b8e93c 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/parsers/json_parser.py
@@ -76,8 +76,17 @@ class JsonParser(BaseParser):
elif "." in key:
split_keys = split_on_dot(key)
result = self.parsed_content
- for key in split_keys:
- result = result[key]
+
+ # Used so we can more precisely specify what key is causing the error.
+ key_for_error = None
+ try:
+ for key in split_keys:
+ key_for_error = key
+ result = result[key]
+ except TypeError:
+ raise ParsingError(
+ f"Cannot get {key} because {key_for_error} is not subscriptable."
+ )
else:
result = self.parsed_content[key]
diff --git a/tests/test_config_file.py b/tests/test_config_file.py
index f57bd69..24b948b 100644
--- a/tests/test_config_file.py
+++ b/tests/test_config_file.py
@@ -76,12 +76,13 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name):
@pytest.mark.parametrize(
- "section_key, value, parse_type, file_name, file_contents",
+ "section_key, value, parse_type, default, file_name, file_contents",
[
(
"calendar.sunday_index",
0,
True,
+ False,
"config.ini",
"[calendar]\nsunday_index = 0\n\n",
),
@@ -89,6 +90,7 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name):
"calendar.sunday_index",
"0",
False,
+ False,
"config.ini",
"[calendar]\nsunday_index = 0\n\n",
),
@@ -96,18 +98,27 @@ def test_that_config_file_can_save(tmpdir, file_contents, file_name):
"calendar.sunday_index",
0,
False,
+ False,
"config.json",
json.dumps({"calendar": {"sunday_index": 0}}),
),
+ (
+ "calendar.missing",
+ "my default value",
+ False,
+ "my default value",
+ "config.json",
+ json.dumps({"calendar": 5}),
+ ),
],
)
def test_that_config_file_can_get(
- tmpdir, section_key, value, parse_type, file_name, file_contents
+ tmpdir, section_key, value, parse_type, file_name, file_contents, default
):
config_path = tmpdir / file_name
config_path.write_text(file_contents, encoding="utf-8")
config = ConfigFile(str(config_path))
- assert config.get(section_key, parse_type=parse_type) == value
+ assert config.get(section_key, parse_type=parse_type, default=default) == value
@pytest.mark.parametrize(