Skip to content

Commit

Permalink
Lazily import YAML libraries
Browse files Browse the repository at this point in the history
Took 37 minutes
  • Loading branch information
EpicWink committed Feb 26, 2020
1 parent 6f21e70 commit 3407e2c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = seddy
version = 0.2.0a0.dev2
version = 0.2.0a0.dev3
url = https://github.com/EpicWink/seddy
project_urls =
Documentation = https://seddy.readthedocs.io/en/latest/
Expand Down
17 changes: 7 additions & 10 deletions src/seddy/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@

import boto3

try:
import yaml
except ImportError as e: # pragma: no cover
try:
from ruamel import yaml
except ImportError:
yaml = e

from . import decisions as seddy_decisions

logger = lg.getLogger(__package__)
Expand Down Expand Up @@ -174,7 +166,12 @@ def load_workflows(workflows_file: pathlib.Path) -> t.Dict[str, t.Any]:
if workflows_file.suffix == ".json":
return json.loads(workflows_text)
elif workflows_file.suffix in (".yml", ".yaml"):
if isinstance(yaml, Exception):
raise yaml
try:
import yaml
except ImportError as e:
try:
from ruamel import yaml
except ImportError as er:
raise e from er
return yaml.safe_load(workflows_text)
raise ValueError("Unknown extension: %s" % workflows_file.suffix)
7 changes: 3 additions & 4 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test ``seddy._util``."""

import sys
import json
from unittest import mock

Expand Down Expand Up @@ -75,17 +76,15 @@ def test_load_workflows_yaml(tmp_path, workflows_spec):
def test_load_workflows_yaml_raises(tmp_path, workflows_spec):
"""Test workflows specs loading from YAML raises when unavailable."""
# Setup environment
yaml_exc = ModuleNotFoundError("yaml")
yaml_patch = mock.patch.object(seddy_util, "yaml", yaml_exc)
yaml_patch = mock.patch.dict(sys.modules, {"yaml": None, "ruamel.yaml": None})

# Build input
workflows_file = tmp_path / "workflows.yml"
workflows_file.write_text(yaml.safe_dump(workflows_spec))

# Run function
with pytest.raises(ModuleNotFoundError) as e, yaml_patch:
with pytest.raises(ModuleNotFoundError), yaml_patch:
seddy_util.load_workflows(workflows_file)
assert e.value == yaml_exc


def test_load_workflows_with_incorrect_suffix(tmp_path, workflows_spec):
Expand Down

0 comments on commit 3407e2c

Please sign in to comment.