Skip to content

Commit

Permalink
🎨 better snkconfig error handeling
Browse files Browse the repository at this point in the history
  • Loading branch information
Wytamma committed Nov 2, 2023
1 parent 45ef429 commit aba4f7d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
29 changes: 16 additions & 13 deletions snk/cli/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List
import snakemake
from dataclasses import dataclass, field

from snk.errors import InvalidSnkConfigError, MissingSnkConfigError
import yaml


Expand Down Expand Up @@ -35,20 +35,23 @@ def from_path(cls, snk_config_path: Path):
>>> SnkConfig.from_path(Path("snk.yaml"))
SnkConfig(art=None, logo=None, tagline='A Snakemake pipeline CLI generated with Snk', font='small', resources=[], annotations={}, symlink_resources=False, _snk_config_path=PosixPath('snk.yaml'))
"""
if snk_config_path.exists():
snk_config_dict = snakemake.load_configfile(snk_config_path)
snk_config = cls(**snk_config_dict)
snk_config.resources = [
snk_config_path.parent / resource for resource in snk_config.resources
]
snk_config.validate_resources(snk_config.resources)
snk_config._snk_config_path = snk_config_path
return snk_config
else:
raise FileNotFoundError(
if not snk_config_path.exists():
raise MissingSnkConfigError(
f"Could not find SNK config file: {snk_config_path}"
)
) from FileNotFoundError
# raise error if file is empty
if snk_config_path.stat().st_size == 0:
raise InvalidSnkConfigError(f"SNK config file is empty: {snk_config_path}") from ValueError

snk_config_dict = snakemake.load_configfile(snk_config_path)
snk_config = cls(**snk_config_dict)
snk_config.resources = [
snk_config_path.parent / resource for resource in snk_config.resources
]
snk_config.validate_resources(snk_config.resources)
snk_config._snk_config_path = snk_config_path
return snk_config

@classmethod
def from_pipeline_dir(
cls, pipeline_dir_path: Path, create_if_not_exists: bool = False
Expand Down
10 changes: 10 additions & 0 deletions snk/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ class PipelineNotFoundError(NestError):

class InvalidPipelineRepositoryError(NestError):
"""Thrown if the given repository appears to have an invalid format."""


class SnkConfigError(Exception):
"""Base class for all SNK config exceptions"""

class InvalidSnkConfigError(SnkConfigError, ValueError):
"""Thrown if the given SNK config appears to have an invalid format."""

class MissingSnkConfigError(SnkConfigError, FileNotFoundError):
"""Thrown if the given SNK config file cannot be found."""
17 changes: 17 additions & 0 deletions tests/snk/test_snk_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ def test_from_path_with_existing_file(tmp_path):
snk_config = SnkConfig.from_path(config_file)
assert snk_config._snk_config_path == config_file

def test_missing_file(tmp_path):
config_file = tmp_path / "snk.yaml"
with pytest.raises(FileNotFoundError):
SnkConfig.from_path(config_file)

def test_empty_file(tmp_path):
config_file = tmp_path / "snk.yaml"
config_file.touch()
with pytest.raises(ValueError):
SnkConfig.from_path(config_file)

def test_invalid_yaml(tmp_path):
config_file = tmp_path / "snk.yaml"
config_file.touch()
config_file.write_text("logo: test_logo\ninvalid_yaml")
with pytest.raises(Exception):
SnkConfig.from_path(config_file)

def test_from_dir_with_existing_file(tmp_path):
config_file = tmp_path / ".snk"
Expand Down

0 comments on commit aba4f7d

Please sign in to comment.