diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-02-09 20:47:13 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-02-09 20:47:13 -0600 |
| commit | 74048486b37cda4dc9a252ed1415b6e136f65186 (patch) | |
| tree | 705815f5dbb7f89cb1435e748b6407beac299dcc /config_file/config_file.py | |
| parent | Merge branch 'master' of github.com:eugenetriguba/config_file (diff) | |
| download | config-file-74048486b37cda4dc9a252ed1415b6e136f65186.zip | |
Add default + return_type param to get(), turn off automatic parsing
Diffstat (limited to 'config_file/config_file.py')
| -rw-r--r-- | config_file/config_file.py | 25 |
1 files changed, 16 insertions, 9 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.""" |