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

Add audeer.script_dir() #155

Merged
merged 9 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions audeer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from audeer.core.io import move_file
from audeer.core.io import replace_file_extension
from audeer.core.io import rmdir
from audeer.core.io import script_dir
from audeer.core.io import touch
from audeer.core.path import path
from audeer.core.path import safe_path
Expand Down
25 changes: 25 additions & 0 deletions audeer/core/io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import errno
import fnmatch
import hashlib
import inspect
import itertools
import os
import platform
Expand Down Expand Up @@ -1007,6 +1008,30 @@ def rmdir(
shutil.rmtree(path)


def script_dir() -> str:
r"""Folder in which caller of this function is located.

When called from a file,
it returns the directory,
in which the file is stored.
When called in an interactive session,
it returns the current working directory
of the interactive session.

Returns:
current directory of caller

Examples:
>>> os.path.basename(script_dir()) # folder of docstring test
'audeer_core_io_script_dir0'

"""
# See https://stackoverflow.com/a/37792573
caller = inspect.stack()[1].filename
frankenjoe marked this conversation as resolved.
Show resolved Hide resolved
# See https://stackoverflow.com/a/5137509
return os.path.dirname(os.path.realpath(caller))


def touch(
path: typing.Union[str, bytes],
*paths: typing.Sequence[typing.Union[str, bytes]],
Expand Down
1 change: 1 addition & 0 deletions docs/api-src/audeer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ audeer
rmdir
run_tasks
safe_path
script_dir
sort_versions
StrictVersion
to_list
Expand Down
17 changes: 17 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,23 @@ def test_rmdir(tmpdir):
assert not os.path.exists(path)


def test_script_dir(tmpdir):
r"""Test estimation of current directory of caller.

See https://stackoverflow.com/a/5137509.

Args:
tmpdir: tmpdir fixture

"""
expected_script_dir = os.path.dirname(os.path.realpath(__file__))
assert audeer.script_dir() == expected_script_dir
current_dir = os.getcwd()
os.chdir(tmpdir)
assert audeer.script_dir() == expected_script_dir
os.chdir(current_dir)


def test_touch(tmpdir):
path = audeer.mkdir(tmpdir, "folder1")
path = os.path.join(path, "file")
Expand Down
Loading