aboutsummaryrefslogtreecommitdiffstats
path: root/config_file
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-02-09 20:47:13 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-02-09 20:47:13 -0600
commit74048486b37cda4dc9a252ed1415b6e136f65186 (patch)
tree705815f5dbb7f89cb1435e748b6407beac299dcc /config_file
parentMerge branch 'master' of github.com:eugenetriguba/config_file (diff)
downloadconfig-file-74048486b37cda4dc9a252ed1415b6e136f65186.zip
Add default + return_type param to get(), turn off automatic parsing
Diffstat (limited to 'config_file')
-rw-r--r--config_file/config_file.py25
-rw-r--r--config_file/parsers/base_parser.py4
-rw-r--r--config_file/parsers/ini_parser.py14
-rw-r--r--config_file/parsers/json_parser.py8
4 files changed, 29 insertions, 22 deletions
diff --git a/config_file/config_file.py b/config_file/config_file.py
index 524272c..734bb57 100644
--- a/config_file/config_file.py
+++ b/config_file/config_file.py
@@ -66,22 +66,29 @@ class ConfigFile:
with open(self.path, "r") as file:
return file.read()
- def get(self, key: str, parse_type: bool = True, default=None):
+ def get(self, key: str, parse_types: bool = False, return_type=None, default=None):
"""
- Retrieve the value of a key in its native type.
- This means the string 'true' will be parsed back as the
- boolean True.
-
- If parse_type is set to False, all values will be returned
- back as strings.
+ Retrieve the value of a key.
+
+ :param key: The key to retrieve.
+ :param parse_type: Automatically parse ints, floats, booleans, dicts, and lists.
+ This recursively parses all types in whatever you're
+ retrieving, not just a single type. e.g. If you are
+ retrieving a section, all values in that section with be
+ parsed.
+ :param return_type: The type to coerce the return value to.
+ :param default: The default value to return if the value of the key is empty.
+
+ :return: The value of the key.
"""
if default is not None:
try:
- return self.parser.get(key, parse_type=parse_type)
+ return self.parser.get(key, parse_types=parse_types)
except ParsingError:
return default
- return self.parser.get(key, parse_type=parse_type)
+ key_value = self.parser.get(key, parse_types=parse_types)
+ return return_type(key_value) if return_type else key_value
def set(self, key: str, value):
"""Sets the value of a key."""
diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py
index 54756cd..42f93b1 100644
--- a/config_file/parsers/base_parser.py
+++ b/config_file/parsers/base_parser.py
@@ -25,13 +25,13 @@ class BaseParser(ABC):
self.parsed_content = self.parse(self.content)
@abstractmethod
- def get(self, key, parse_type=True):
+ def get(self, key, parse_types=False):
"""
Retrieve the value of a key in its native type.
This means the string 'true' will be parsed back as the
boolean True.
- If parse_type is set to False, all values will be returned
+ If parse_types is set to False, all values will be returned
back as strings.
"""
raise NotImplementedError
diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py
index ba44c64..81f2d1b 100644
--- a/config_file/parsers/ini_parser.py
+++ b/config_file/parsers/ini_parser.py
@@ -19,14 +19,14 @@ class IniParser(BaseParser):
except configparser.Error as error:
raise ParsingError(error)
- def get(self, section_key: str, parse_type: bool = True):
+ def get(self, section_key: str, parse_types: bool = False):
"""
Read the value of `section.key` of the config file.
:param section_key: The section and key to read from in the config file.
e.g. 'section1.key2'
- :param parse_type: Coerces the return value to its native type.
- :return: The value of the key, parsed to its native type if parse_type is True.
+ :param parse_types: Coerces the return value to its native type.
+ :return: The value of the key, parsed to its native type if parse_types is True.
:raises ValueError: if the section_key given is not in a dot format. e.g.
'section1.key'
@@ -34,19 +34,19 @@ class IniParser(BaseParser):
invalid format, or if we are unable to coerce the return value to value_type.
"""
if "." not in section_key:
- return self.__retrieve_section(section_key, parse_type)
+ return self.__retrieve_section(section_key, parse_types)
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
+ return parse_value(value) if parse_types else value
except configparser.Error as error:
raise ParsingError(error.message)
- def __retrieve_section(self, section, parse_type):
+ def __retrieve_section(self, section, parse_types):
items = dict(self.parsed_content.items(section))
- if not parse_type:
+ if not parse_types:
return items
for item in items:
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
index 9e9f52d..67ddc11 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/parsers/json_parser.py
@@ -16,7 +16,7 @@ class JsonParser(BaseParser):
"""
The JsonParser allows use of json files with ConfigFile. The syntax it
supports is a bit more flexible than the IniParser.
-
+
By default, the dot syntax is used. However, if you'd like to get/set/delete all
of a particular key, you can specify so with an optional parameter.
@@ -48,13 +48,13 @@ class JsonParser(BaseParser):
except json.decoder.JSONDecodeError as error:
raise ParsingError(error)
- def get(self, key: str, parse_type: bool = True, retrieve_all: bool = False):
+ def get(self, key: str, parse_types: bool = False, retrieve_all: bool = False):
"""
Retrieve values using a dot syntax.
:param key: The key to retrieve. e.g. 'foo.bar.boo'
- :param parse_type: Whether you'd like the type parsed into its native one.
+ :param parse_types: Whether you'd like the type parsed into its native one.
:param retrieve_all: Specify whether you'd like to recursively receive
all values that match the given key. Returned as a list.
@@ -82,7 +82,7 @@ class JsonParser(BaseParser):
else:
result = self.parsed_content[key]
- return parse_value(result) if parse_type else result
+ return parse_value(result) if parse_types else result
def set(self, key: str, value, set_all: bool = False):
if set_all: