From ba5d3e2541670829697d794943322dd5f2a453af Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:51:14 -0500 Subject: Add toml and yaml parsers --- config_file/parsers/yaml_parser.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'config_file/parsers/yaml_parser.py') diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py index a45d7f6..77b59fe 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/parsers/yaml_parser.py @@ -1,5 +1,29 @@ -from config_file.parsers import AbstractParser +try: + import yaml + YAML_AVAILABLE = True -class YamlParser(AbstractParser): - pass + # Conform yaml to json and + # toml package syntax + yaml.loads = yaml.load + yaml.dumps = yaml.dump +except ImportError: + YAML_AVAILABLE = False + +from config_file.exceptions import MissingDependencyError + +from .base_parser import BaseParser + + +class YamlParser(BaseParser): + def __init__(self, file_contents: str): + if not YAML_AVAILABLE: + raise MissingDependencyError( + "It doesn't appear `PyYaml` is installed, but a yaml " + "file was attempted to be used. Install the `toml` " + "extra first with `pip install config-file[toml]`." + ) + + super().__init__( + file_contents=file_contents, module=yaml, decode_error=yaml.YAMLError + ) -- cgit v1.2.3