diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-11-26 13:18:44 -0600 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-11-26 13:18:44 -0600 |
| commit | d6832cd600a28c1396c67229245347c1cf9440ca (patch) | |
| tree | e216eed48e9130be1bd63d4ad0ee3c8cad68d915 /config_file/parsers/toml_parser.py | |
| parent | Add python 3.9 to pyproject and update lockfile (diff) | |
| download | config-file-d6832cd600a28c1396c67229245347c1cf9440ca.zip | |
Move parsers to parsers directory
Diffstat (limited to 'config_file/parsers/toml_parser.py')
| -rw-r--r-- | config_file/parsers/toml_parser.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py new file mode 100644 index 0000000..de5226c --- /dev/null +++ b/config_file/parsers/toml_parser.py @@ -0,0 +1,31 @@ +from typing import Type + +from .base_parser import BaseParser + + +class TomlParser(BaseParser): + def __init__(self, file_contents: str): + try: + 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) |