Skip to content

Commit

Permalink
Add audeer.script_dir() (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw authored Jul 19, 2024
1 parent b24d6c7 commit 0867cdb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
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
31 changes: 31 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,36 @@ 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'
"""
# Returning the script dir is usually done with
# `os.path.dirname(os.path.realpath(__file__))`,
# see https://stackoverflow.com/a/5137509.
# We cannot use `__file__` here,
# as this would always point to this file (`io.py`).
# Instead we find the script
# of the caller of `audeer.script_dir()`,
# see https://stackoverflow.com/a/37792573
caller = inspect.stack()[1].filename
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

0 comments on commit 0867cdb

Please sign in to comment.