diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-16 21:35:56 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-16 21:35:56 -0500 |
| commit | 2d091dcc5b1250b2c9457f0ff34c0c997a6a0867 (patch) | |
| tree | 7d0bbde5193b4a298f3510cfda9cd48d6bca597a /config_file/toml_parser.py | |
| parent | Remove sphinx dev dependencies (diff) | |
| download | config-file-2d091dcc5b1250b2c9457f0ff34c0c997a6a0867.zip | |
Refactor parsers to enforce loads/dumps/decode_error requirement
Diffstat (limited to 'config_file/toml_parser.py')
| -rw-r--r-- | config_file/toml_parser.py | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/config_file/toml_parser.py b/config_file/toml_parser.py index d3cf226..de5226c 100644 --- a/config_file/toml_parser.py +++ b/config_file/toml_parser.py @@ -1,18 +1,31 @@ +from typing import Type + from .base_parser import BaseParser class TomlParser(BaseParser): def __init__(self, file_contents: str): try: - import tomlkit - from tomlkit.exceptions import ParseError - - super().__init__( - file_contents=file_contents, module=tomlkit, decode_error=ParseError, - ) + super().__init__(file_contents) except ImportError: raise ImportError( "It doesn't appear `tomlkit` is installed, but a toml " "file was attempted to be used. Install the `toml` " "extra first with `pip install config-file[toml]`." ) + + @property + def decode_error(self) -> Type[Exception]: + from tomlkit.exceptions import ParseError + + return ParseError + + def loads(self, contents: str) -> dict: + import tomlkit + + return tomlkit.loads(contents) + + def dumps(self, loaded_contents: dict) -> str: + import tomlkit + + return tomlkit.dumps(loaded_contents) |