From d6832cd600a28c1396c67229245347c1cf9440ca Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 26 Nov 2020 13:18:44 -0600 Subject: Move parsers to parsers directory --- config_file/parsers/yaml_parser.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 config_file/parsers/yaml_parser.py (limited to 'config_file/parsers/yaml_parser.py') diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py new file mode 100644 index 0000000..fc4239a --- /dev/null +++ b/config_file/parsers/yaml_parser.py @@ -0,0 +1,31 @@ +from typing import Type + +from .base_parser import BaseParser + + +class YamlParser(BaseParser): + def __init__(self, file_contents: str): + try: + super().__init__(file_contents) + except ImportError: + raise ImportError( + "It doesn't appear `ruamel.yaml` is installed, but a yaml " + "file was attempted to be used. Install the `yaml` " + "extra first with `pip install config-file[yaml]`." + ) + + @property + def decode_error(self) -> Type[Exception]: + from ruamel.yaml import YAMLError + + return YAMLError + + def loads(self, contents: str) -> dict: + from ruamel.yaml import round_trip_load + + return round_trip_load(contents) + + def dumps(self, loaded_contents: dict) -> str: + from ruamel.yaml import round_trip_dump + + return round_trip_dump(loaded_contents) -- cgit v1.2.3