From 74048486b37cda4dc9a252ed1415b6e136f65186 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 9 Feb 2020 20:47:13 -0600 Subject: Add default + return_type param to get(), turn off automatic parsing --- config_file/config_file.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'config_file/config_file.py') 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.""" -- cgit v1.2.3