Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept str values for config, target_dir in file tool #553

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/uwtools/api/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from uwtools.file import Copier, Linker
from uwtools.utils.api import ensure_data_source as _ensure_data_source
from uwtools.utils.api import str2path as _str2path


def copy(
Expand All @@ -36,7 +35,7 @@ def copy(
"""
copier = Copier(
target_dir=Path(target_dir) if target_dir else None,
config=_ensure_data_source(_str2path(config), stdin_ok),
config=_ensure_data_source(config, stdin_ok),
cycle=cycle,
leadtime=leadtime,
keys=keys,
Expand Down Expand Up @@ -69,7 +68,7 @@ def link(
"""
linker = Linker(
target_dir=Path(target_dir) if target_dir else None,
config=_ensure_data_source(_str2path(config), stdin_ok),
config=_ensure_data_source(config, stdin_ok),
cycle=cycle,
leadtime=leadtime,
keys=keys,
Expand Down
15 changes: 8 additions & 7 deletions src/uwtools/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
from uwtools.config.validator import validate_internal
from uwtools.exceptions import UWConfigError
from uwtools.logging import log
from uwtools.utils.api import str2path
from uwtools.utils.tasks import filecopy, symlink


class FileStager:
class Stager:
"""
The base class for staging files.
"""

def __init__(
self,
config: Optional[Union[dict, Path]] = None,
target_dir: Optional[Path] = None,
config: Optional[Union[dict, str, Path]] = None,
target_dir: Optional[Union[str, Path]] = None,
cycle: Optional[dt.datetime] = None,
leadtime: Optional[dt.timedelta] = None,
keys: Optional[list[str]] = None,
Expand All @@ -42,8 +43,8 @@ def __init__(
:raises: UWConfigError if config fails validation.
"""
dryrun(enable=dry_run)
self._target_dir = target_dir
self._config = YAMLConfig(config=config)
self._target_dir = str2path(target_dir)
self._config = YAMLConfig(config=str2path(config))
self._keys = keys or []
self._config.dereference(
context={
Expand Down Expand Up @@ -98,7 +99,7 @@ def _validate(self) -> bool:
return True


class Copier(FileStager):
class Copier(Stager):
"""
Stage files by copying.
"""
Expand All @@ -113,7 +114,7 @@ def go(self):
yield [filecopy(src=Path(v), dst=dst(k)) for k, v in self._file_map.items()]


class Linker(FileStager):
class Linker(Stager):
"""
Stage files by linking.
"""
Expand Down
12 changes: 6 additions & 6 deletions src/uwtools/tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,27 @@ def test_Linker(assets, source):


@mark.parametrize("source", ("dict", "file"))
def test_FileStager(assets, source):
def test_Stager(assets, source):
dstdir, cfgdict, cfgfile = assets
config = cfgdict if source == "dict" else cfgfile
stager = file.FileStager(target_dir=dstdir, config=config, keys=["a", "b"])
stager = file.Stager(target_dir=dstdir, config=config, keys=["a", "b"])
assert set(stager._file_map.keys()) == {"foo", "subdir/bar"}
assert stager._validate() is True


@mark.parametrize("source", ("dict", "file"))
def test_FileStager_bad_key(assets, source):
def test_Stager_bad_key(assets, source):
dstdir, cfgdict, cfgfile = assets
config = cfgdict if source == "dict" else cfgfile
with raises(UWConfigError) as e:
file.FileStager(target_dir=dstdir, config=config, keys=["a", "x"])
file.Stager(target_dir=dstdir, config=config, keys=["a", "x"])
assert str(e.value) == "Failed following YAML key(s): a -> x"


@mark.parametrize("val", [None, True, False, "str", 88, 3.14, [], tuple()])
def test_FileStager_empty_val(assets, val):
def test_Stager_empty_val(assets, val):
dstdir, cfgdict, _ = assets
cfgdict["a"]["b"] = val
with raises(UWConfigError) as e:
file.FileStager(target_dir=dstdir, config=cfgdict, keys=["a", "b"])
file.Stager(target_dir=dstdir, config=cfgdict, keys=["a", "b"])
assert str(e.value) == "No file map found at key path: a -> b"
Loading