From 59eeb1321fbad41dce7c971778e37ac1943fda3b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 20:39:28 -0500 Subject: Redo package to have a flat package structure By having a flat package structure like this, it'll avoid exposing packages and things we don't want to be exposed. --- config_file/yaml_parser.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 config_file/yaml_parser.py (limited to 'config_file/yaml_parser.py') diff --git a/config_file/yaml_parser.py b/config_file/yaml_parser.py new file mode 100644 index 0000000..fb42e08 --- /dev/null +++ b/config_file/yaml_parser.py @@ -0,0 +1,27 @@ +try: + from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load + + YAML_AVAILABLE = True +except ImportError: + YAML_AVAILABLE = False + +from .base_parser import BaseParser +from .exceptions import MissingDependencyError + + +class YamlParser(BaseParser): + def __init__(self, file_contents: str): + if not YAML_AVAILABLE: + raise MissingDependencyError( + "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]`." + ) + + 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 + ) -- cgit v1.2.3 From 19d23dc13636b30f6282e8b3f02b659bcdb2bb11 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:48:08 -0500 Subject: Remove MissingDependencyError in favor of ImportError --- config_file/yaml_parser.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'config_file/yaml_parser.py') diff --git a/config_file/yaml_parser.py b/config_file/yaml_parser.py index fb42e08..938b0d7 100644 --- a/config_file/yaml_parser.py +++ b/config_file/yaml_parser.py @@ -6,13 +6,12 @@ except ImportError: YAML_AVAILABLE = False from .base_parser import BaseParser -from .exceptions import MissingDependencyError class YamlParser(BaseParser): def __init__(self, file_contents: str): if not YAML_AVAILABLE: - raise MissingDependencyError( + 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]`." -- cgit v1.2.3 From d2f3161dac2313e4109ba2159fdc0d43e50506f8 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 6 Aug 2020 09:43:44 -0500 Subject: Test extras parsers & move imports inside those parsers --- config_file/yaml_parser.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'config_file/yaml_parser.py') diff --git a/config_file/yaml_parser.py b/config_file/yaml_parser.py index 938b0d7..8d88881 100644 --- a/config_file/yaml_parser.py +++ b/config_file/yaml_parser.py @@ -1,26 +1,21 @@ -try: - from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load - - YAML_AVAILABLE = True -except ImportError: - YAML_AVAILABLE = False - from .base_parser import BaseParser class YamlParser(BaseParser): def __init__(self, file_contents: str): - if not YAML_AVAILABLE: + 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 + ) + 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]`." ) - - 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 - ) -- cgit v1.2.3