From b439038a5d87cb36b77d0fb11a157d4079aee4a5 Mon Sep 17 00:00:00 2001 From: Hagen Wierstorf Date: Thu, 18 Jul 2024 10:53:24 +0200 Subject: [PATCH] Add audeer.current_dir() --- audeer/__init__.py | 1 + audeer/core/io.py | 16 ++++++++++++++++ docs/api-src/audeer.rst | 1 + tests/test_io.py | 10 ++++++++++ 4 files changed, 28 insertions(+) diff --git a/audeer/__init__.py b/audeer/__init__.py index f35709b..9d05708 100644 --- a/audeer/__init__.py +++ b/audeer/__init__.py @@ -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 diff --git a/audeer/core/io.py b/audeer/core/io.py index afdc4d2..35c246e 100644 --- a/audeer/core/io.py +++ b/audeer/core/io.py @@ -1,6 +1,7 @@ import errno import fnmatch import hashlib +import inspect import itertools import os import platform @@ -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, diff --git a/docs/api-src/audeer.rst b/docs/api-src/audeer.rst index aa780a2..a433156 100644 --- a/docs/api-src/audeer.rst +++ b/docs/api-src/audeer.rst @@ -11,6 +11,7 @@ audeer common_directory config create_archive + current_dir deprecated deprecated_default_value deprecated_keyword_argument diff --git a/tests/test_io.py b/tests/test_io.py index 2275de4..fc62304 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -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)