Skip to content

Commit

Permalink
Remove redundancy in models.py
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaeger committed Sep 10, 2024
1 parent f350b5b commit fc41bb6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 44 deletions.
21 changes: 3 additions & 18 deletions pydesigner/system/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,13 @@ def input_path_validator(path: str, ctype: str = None):
--------
output_path_validator
"""
if not isinstance(path, str):
msg = f"Entered path ({path}) is not a valid string. "
msg += "Please enter path as a string."
raise TypeError(msg)
opts = modelmrtrix(input=path)
if not ctype is None:
if not isinstance(ctype, str):
msg = f"ctype variable (ctype={ctype}) needs to be a valid string."
raise(TypeError(msg))
if not op.exists(path):
msg = f"Input file ({path}) does not exist."
raise FileNotFoundError(msg)
if ctype:
if op.splitext(op.basename(path))[-1] != ctype:
if op.splitext(op.basename(opts.input))[-1] != ctype:
msg = f"Input file ({path}) does not posses the required {ctype} extension."
raise FileExtensionError(msg)
return str(path)
Expand All @@ -119,21 +113,12 @@ def output_path_validator(path: str, ctype: str = None):
--------
input_path_validator
"""
if not isinstance(path, str):
msg = f"Entered path ({path}) is not a valid string. "
msg += "Please enter path as a string."
raise TypeError(msg)
if not ctype is None:
if not isinstance(ctype, str):
msg = f"ctype variable (ctype={ctype}) needs to be a valid string."
raise(TypeError(msg))
if not op.exists(op.dirname(path)):
msg = f"Specified directory ({op.dirname(path)}) for output file "
msg += f"({op.basename(path)}) does not exist. "
msg += "Pleasure ensure that the output parent directory exists."
raise OSError(msg)
if ctype:
if op.splitext(op.basename(path))[-1] != ctype:
msg = f"Input file ({path}) does not posses the required {ctype} extension."
msg = f"Output file ({path}) does not posses the required {ctype} extension."
raise FileExtensionError(msg)
return str(path)
27 changes: 1 addition & 26 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,6 @@ def test_modelmrtrix_nthreads_negative():
assert "nthreads needs to be a valid positive integer" in str(exc.value)


def test_input_validator_path_invalid():
with pytest.raises(TypeError) as exc:
path = input_path_validator(path=10)
assert "Please enter path as a string" in str(exc.value)


def test_input_validator_path_nonexistent(tmp_path):
input_path = str(tmp_path / "input.nii")
with pytest.raises(FileNotFoundError) as exc:
path = input_path_validator(path=input_path)
assert f"Input file ({input_path}) does not exist" in str(exc.value)


def test_input_validator_ctype_invalid():
with pytest.raises(TypeError) as exc:
path = input_path_validator(path=PATH_DWI, ctype=20)
Expand All @@ -109,18 +96,6 @@ def test_input_validator_success():
assert path == PATH_DWI


def test_output_validator_path_invalid():
with pytest.raises(TypeError) as exc:
path = output_path_validator(path=10)
assert "Please enter path as a string" in str(exc.value)


def test_output_validator_basepath():
with pytest.raises(OSError) as exc:
path = output_path_validator(path="nonexistent/output.nii")
assert "Pleasure ensure that the output parent directory exists" in str(exc.value)


def test_output_validator_ctype_invalid():
with pytest.raises(TypeError) as exc:
path = output_path_validator(path=PATH_DWI, ctype=20)
Expand All @@ -130,7 +105,7 @@ def test_output_validator_ctype_invalid():
def test_output_validator_ctype_check_fail():
with pytest.raises(FileExtensionError) as exc:
path = output_path_validator(path=PATH_DWI, ctype=".tar")
assert f"Input file ({PATH_DWI}) does not posses the required .tar extension" in str(exc.value)
assert f"Output file ({PATH_DWI}) does not posses the required .tar extension" in str(exc.value)


def test_output_validator_success(tmp_path):
Expand Down

0 comments on commit fc41bb6

Please sign in to comment.