aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/config_file.py
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 /config_file/config_file.py
parentAdd requests to tox (diff)
downloadconfig-file-5fa867ba5a2e0927e9b87a8228989357924623d2.zip
Allow a default value for the get() method
Diffstat (limited to 'config_file/config_file.py')
-rw-r--r--config_file/config_file.py10
1 files changed, 8 insertions, 2 deletions
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):