aboutsummaryrefslogtreecommitdiffstats
path: root/config_file/yaml_parser.py
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-08-16 21:35:56 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-08-16 21:35:56 -0500
commit2d091dcc5b1250b2c9457f0ff34c0c997a6a0867 (patch)
tree7d0bbde5193b4a298f3510cfda9cd48d6bca597a /config_file/yaml_parser.py
parentRemove sphinx dev dependencies (diff)
downloadconfig-file-2d091dcc5b1250b2c9457f0ff34c0c997a6a0867.zip
Refactor parsers to enforce loads/dumps/decode_error requirement
Diffstat (limited to 'config_file/yaml_parser.py')
-rw-r--r--config_file/yaml_parser.py28
1 files changed, 19 insertions, 9 deletions
diff --git a/config_file/yaml_parser.py b/config_file/yaml_parser.py
index 8d88881..fc4239a 100644
--- a/config_file/yaml_parser.py
+++ b/config_file/yaml_parser.py
@@ -1,21 +1,31 @@
+from typing import Type
+
from .base_parser import BaseParser
class YamlParser(BaseParser):
def __init__(self, file_contents: str):
try:
- from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load
-
- yaml = YAML(typ="safe")
- yaml.loads = round_trip_load
- yaml.dumps = round_trip_dump
-
- super().__init__(
- file_contents=file_contents, module=yaml, decode_error=YAMLError
- )
+ 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)