Skip to content

Commit c211f65

Browse files
committed
Merge branch '78-get_absolute_path_of_file'
2 parents 0490f50 + 7df0970 commit c211f65

5 files changed

Lines changed: 56 additions & 24 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
coverage erase
3030
coverage run -a --source ./sksurgerycore -m pytest -v -s ./tests/
3131
coverage report -m
32-
pylint --rcfile=tests/pylintrc --ignore _version.py sksurgerycore tests
32+
pylint --rcfile=tests/pylintrc sksurgerycore tests
3333
3434
- name: Run coveralls
3535
run: |

sksurgerycore/utilities/file_utilities.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@
77

88
def get_absolute_path_of_file(file_name, dir_name=None):
99
"""
10-
Filenames in our .json config could be absolute or relative to the
11-
current working dir. This method tries to find the valid, full file path.
10+
This method tries to setup an absolute pathname.
11+
Tt doesn't actually check if the file exists.
12+
If dir_name is provided, it is used as a prefix.
13+
If dir_name is None, the function is equivalent
14+
to os.path.abspath(file_name).
1215
13-
:param file_name:
14-
:param dir_name: prefix, for example, the dirname of our .json file.
15-
:return: absolute path name of file if found, otherwise None.
16+
:param file_name: str, the file name to convert to absolute path.
17+
:param dir_name: str or None, optional prefix directory.
18+
:return: absolute path name
19+
:raises ValueError if file_name is empty or None.
1620
"""
17-
if not file_name:
18-
raise ValueError("Empty file_name.")
19-
file = None
20-
if os.path.isabs(file_name):
21-
if os.path.isfile(file_name):
22-
file = file_name
23-
elif dir_name is not None:
24-
joined = os.path.join(dir_name, file_name)
25-
if os.path.isfile(joined):
26-
file = joined
27-
elif os.getcwd() is not None:
28-
joined = os.path.join(os.getcwd(), file_name)
29-
if os.path.isfile(joined):
30-
file = joined
31-
return file
21+
if file_name is None:
22+
raise ValueError("file_name is None.")
23+
if not isinstance(file_name, str):
24+
raise TypeError("file_name is not a string.")
25+
if file_name.strip() == "":
26+
raise ValueError("file_name is empty.")
27+
fname = file_name.strip()
28+
if dir_name is not None:
29+
if not isinstance(dir_name, str):
30+
raise TypeError("dir_name is not a string.")
31+
if dir_name.strip() == "":
32+
raise ValueError("dir_name is empty.")
33+
dir_name = dir_name.strip()
34+
fname = os.path.join(dir_name, fname)
35+
return os.path.abspath(fname)

tests/pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# Add files or directories to the blacklist. They should be base names, not
1111
# paths.
12-
ignore=CVS
12+
ignore=CVS, _version.py
1313

1414
# Add files or directories matching the regex patterns to the blacklist. The
1515
# regex matches against base names, not paths.

tests/utilities/test_file_utilities.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@
77

88
import sksurgerycore.utilities.file_utilities as fu
99

10+
def test_get_absolute_file_exceptions():
11+
with pytest.raises(ValueError) as excinfo:
12+
fu.get_absolute_path_of_file(None)
13+
assert "file_name is None." in str(excinfo.value)
14+
15+
with pytest.raises(TypeError) as excinfo:
16+
fu.get_absolute_path_of_file(123)
17+
assert "file_name is not a string." in str(excinfo.value)
18+
19+
with pytest.raises(ValueError) as excinfo:
20+
fu.get_absolute_path_of_file(" ")
21+
assert "file_name is empty." in str(excinfo.value)
22+
23+
with pytest.raises(TypeError) as excinfo:
24+
fu.get_absolute_path_of_file("file.txt", dir_name=123)
25+
assert "dir_name is not a string." in str(excinfo.value)
26+
27+
with pytest.raises(ValueError) as excinfo:
28+
fu.get_absolute_path_of_file("file.txt", dir_name=" ")
29+
assert "dir_name is empty." in str(excinfo.value)
1030

1131
def test_get_absolute_file():
1232

@@ -21,3 +41,11 @@ def test_get_absolute_file():
2141

2242
fp3 = fu.get_absolute_path_of_file(this_file, cwd)
2343
assert fp3 == fp
44+
45+
# Similar to test for fp, but file doesn't exist.
46+
# So, really just checking that we always get an absolute path.
47+
fp4 = fu.get_absolute_path_of_file("nonsense.txt")
48+
assert fp4 is not None
49+
assert os.path.basename(fp4) == "nonsense.txt"
50+
assert os.path.isabs(fp4)
51+
assert os.path.dirname(fp4) == cwd

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ envlist = test,lint
44
skipsdist = True
55

66
[testenv]
7-
basepython=python3.10
7+
basepython=python3.13
88
passenv = *
99
deps=-rrequirements-dev.txt
1010

@@ -16,7 +16,7 @@ commands = coverage erase
1616
coverage report -m
1717

1818
[testenv:lint]
19-
commands=pylint --rcfile=tests/pylintrc --ignore _version.py sksurgerycore tests
19+
commands=pylint --rcfile=tests/pylintrc sksurgerycore tests
2020

2121
[testenv:docs]
2222
changedir = docs

0 commit comments

Comments
 (0)