Skip to content

Commit

Permalink
Add audeer.current_dir()
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Jul 18, 2024
1 parent b24d6c7 commit b439038
Show file tree
Hide file tree
Showing 4 changed files with 28 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 @@ -2,6 +2,7 @@
from audeer.core.io import basename_wo_ext
from audeer.core.io import common_directory
from audeer.core.io import create_archive
from audeer.core.io import current_dir
from audeer.core.io import download_url
from audeer.core.io import extract_archive
from audeer.core.io import extract_archives
Expand Down
16 changes: 16 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 @@ -231,6 +232,21 @@ def create_archive(
)


def current_dir() -> str:
r"""Folder in which caller of this function is located.
Returns:
current directory of caller
Examples:
>>> os.path.basename(current_dir())
'audeer_core_io_current_dir0'
"""
caller = inspect.stack()[1].filename
return os.path.dirname(os.path.realpath(caller))


def download_url(
url: str,
destination: str,
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 @@ -11,6 +11,7 @@ audeer
common_directory
config
create_archive
current_dir
deprecated
deprecated_default_value
deprecated_keyword_argument
Expand Down
10 changes: 10 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ def test_common_directory(dirs, expected):
assert common == expected


def test_current_dir():
r"""Test estimation of current directory of caller.
See https://stackoverflow.com/a/5137509.
"""
current_dir = audeer.current_dir()
assert current_dir == os.path.dirname(os.path.realpath(__file__))


def test_download_url(tmpdir):
url = "https://audeering.github.io/audeer/_static/favicon.png"
audeer.download_url(url, tmpdir)
Expand Down

0 comments on commit b439038

Please sign in to comment.