aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/parsers/toml_parser.py
blob: de5226c6b415e1c3fffaa1b0e26f5a4dcaf6e1f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)