diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-06-20 13:41:35 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-06-20 13:41:35 -0500 |
| commit | 94fe938075c4edba742db66280c8e02198d79e1e (patch) | |
| tree | 86979be9a12d6563de34914927be6413535ffb6c /config_file/parsers | |
| parent | docs: Tweak project description (diff) | |
| download | config-file-94fe938075c4edba742db66280c8e02198d79e1e.zip | |
Add python 3.5 support, ConfigFileError, and more type hints
Also, get_all, set_all, and delete_all in JsonParser have been
renamed to just all.
Diffstat (limited to 'config_file/parsers')
| -rw-r--r-- | config_file/parsers/base_parser.py | 3 | ||||
| -rw-r--r-- | config_file/parsers/ini_parser.py | 7 | ||||
| -rw-r--r-- | config_file/parsers/json_parser.py | 21 | ||||
| -rw-r--r-- | config_file/parsers/toml_parser.py | 0 | ||||
| -rw-r--r-- | config_file/parsers/yaml_parser.py | 0 |
5 files changed, 15 insertions, 16 deletions
diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index 42f93b1..5fc47c2 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -74,6 +74,3 @@ class BaseParser(ABC): """ raise NotImplementedError - -class ParsingError(Exception): - """Unable to parse the configuration file""" diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 81f2d1b..31b0dcf 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,9 +1,10 @@ import configparser from io import StringIO -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot +from config_file.exceptions import ParsingError class IniParser(BaseParser): @@ -95,7 +96,7 @@ class IniParser(BaseParser): if "." not in section_key: if not self.parsed_content.has_section(section_key): raise ValueError( - f"Cannot delete {section_key} because it is not in the config file." + "Cannot delete {} because it is not in the config file.".format(section_key) ) self.parsed_content.remove_section(section_key) @@ -104,7 +105,7 @@ class IniParser(BaseParser): section, key = split_on_dot(section_key, only_last_dot=True) if key not in self.parsed_content[section]: raise ValueError( - f"Cannot delete {section}.{key} because {key} is not in {section}." + "Cannot delete {section}.{key} because {key} is not in {section}.".format(section = section, key = key) ) self.parsed_content.remove_option(section, key) diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py index 20eb085..4d93ca3 100644 --- a/config_file/parsers/json_parser.py +++ b/config_file/parsers/json_parser.py @@ -7,9 +7,10 @@ from nested_lookup import ( nested_update, ) -from config_file.parsers.base_parser import BaseParser, ParsingError +from config_file.parsers.base_parser import BaseParser from config_file.parsers.parse_value import parse_value from config_file.utils import split_on_dot +from config_file.exceptions import ParsingError class JsonParser(BaseParser): @@ -34,7 +35,7 @@ class JsonParser(BaseParser): >>> { "key2": "foo", "key1": "bar" } parser.get('key1.key2') >>> "foo" - parser.get('key1', get_all=True) + parser.get('key1', all=True) >>> [{ "key2": "foo", "key1": "bar" }, "bar"] """ @@ -48,7 +49,7 @@ class JsonParser(BaseParser): except json.decoder.JSONDecodeError as error: raise ParsingError(error) - def get(self, key: str, parse_types: bool = False, get_all: bool = False): + def get(self, key: str, parse_types: bool = False, all: bool = False): """ Retrieve values using a dot syntax. @@ -56,14 +57,14 @@ class JsonParser(BaseParser): :param parse_types: Whether you'd like the type parsed into its native one. - :param get_all: Specify whether you'd like to recursively receive + :param all: Specify whether you'd like to recursively receive all values that match the given key. Returned as a list. e.g. 'foo' would retrieve all values that have the key 'foo', anywhere in the json. :return: the value of the given key """ - if get_all: + if all: result = nested_lookup(key, self.parsed_content) elif "." in key: split_keys = split_on_dot(key) @@ -77,15 +78,15 @@ class JsonParser(BaseParser): result = result[key] except TypeError: raise ParsingError( - f"Cannot get {key} because {key_for_error} is not subscriptable." + "Cannot get {} because {} is not subscriptable.".format(key, key_for_error) ) else: result = self.parsed_content[key] return parse_value(result) if parse_types else result - def set(self, key: str, value, set_all: bool = False): - if set_all: + def set(self, key: str, value, all: bool = False): + if all: nested_update(self.parsed_content, key, value, in_place=True) elif "." in key: indexes = split_on_dot(key) @@ -103,8 +104,8 @@ class JsonParser(BaseParser): return True - def delete(self, key, delete_all: bool = False): - if delete_all: + def delete(self, key, all: bool = False): + if all: nested_delete(self.parsed_content, key, in_place=True) elif "." in key: indexes = split_on_dot(key) diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/config_file/parsers/toml_parser.py diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/config_file/parsers/yaml_parser.py |