Skip to content

Commit

Permalink
Support *paths in mkdir() and rmdir() (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw authored Dec 4, 2023
1 parent 3e62a71 commit 48fdf9f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
23 changes: 15 additions & 8 deletions audeer/core/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ def list_dir_names(
FileNotFoundError: if path does not exists
Examples:
>>> _ = mkdir('path/a/.b/c')
>>> _ = mkdir('path', 'a', '.b', 'c')
>>> list_dir_names(
... 'path',
... basenames=True,
Expand Down Expand Up @@ -579,7 +579,7 @@ def list_file_names(
>>> _ = touch(os.path.join(dir_path, 'file.wav'))
>>> _ = touch(os.path.join(dir_path, 'File.wav'))
>>> _ = touch(os.path.join(dir_path, '.lock'))
>>> sub_dir_path = mkdir(os.path.join('path', 'sub'))
>>> sub_dir_path = mkdir('path', 'sub')
>>> _ = touch(os.path.join(sub_dir_path, 'file.ogg'))
>>> _ = touch(os.path.join(sub_dir_path, '.lock'))
>>> list_file_names(
Expand Down Expand Up @@ -714,7 +714,7 @@ def is_pattern(pattern):

def mkdir(
path: typing.Union[str, bytes],
*,
*paths: typing.Sequence[typing.Union[str, bytes]],
mode: int = 0o777
) -> str:
"""Create directory.
Expand All @@ -738,18 +738,21 @@ def mkdir(
Args:
path: absolute or relative path of directory to create
*paths: additional arguments
to be joined with ``path``
by :func:`os.path.join`
mode: set permissions of created folders
Returns:
absolute path to the created directory
Examples:
>>> p = mkdir('path1/path2/path3')
>>> p = mkdir('path1', 'path2', 'path3')
>>> os.path.basename(p)
'path3'
"""
path = safe_path(path)
path = safe_path(path, *paths)
if path:
os.makedirs(path, mode=mode, exist_ok=True)
return path
Expand Down Expand Up @@ -934,6 +937,7 @@ def replace_file_extension(

def rmdir(
path: typing.Union[str, bytes],
*paths: typing.Sequence[typing.Union[str, bytes]],
):
"""Remove directory.
Expand All @@ -943,20 +947,23 @@ def rmdir(
Args:
path: absolute or relative path of directory to remove
*paths: additional arguments
to be joined with ``path``
by :func:`os.path.join`
Raises:
NotADirectoryError: if path is not a directory
Examples:
>>> _ = mkdir('path1/path2/path3')
>>> _ = mkdir('path1', 'path2', 'path3')
>>> list_dir_names('path1', basenames=True)
['path2']
>>> rmdir('path1/path2')
>>> rmdir('path1', 'path2')
>>> list_dir_names('path1')
[]
"""
path = safe_path(path)
path = safe_path(path, *paths)
if os.path.exists(path):
shutil.rmtree(path)

Expand Down
16 changes: 6 additions & 10 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ def test_mkdir(tmpdir):
assert p == os.path.join(path, 'folder4')
# Subdirectories
os.chdir(path)
p = audeer.mkdir('folder5/folder6')
p = audeer.mkdir('folder5', 'folder6')
os.chdir(current_path)
assert os.path.isdir(p) is True
assert p == os.path.join(path, 'folder5', 'folder6')
Expand All @@ -1109,27 +1109,24 @@ def test_mkdir(tmpdir):
assert p == path
# Mode, see https://stackoverflow.com/a/705088
# Default mode
path = os.path.join(str(tmpdir.mkdir('folder8')), 'sub-folder')
os.umask(0)
p = audeer.mkdir(path)
p = audeer.mkdir(tmpdir, 'folder8', 'sub-folder')
mode = stat.S_IMODE(os.stat(p).st_mode)
expected_mode = int('777', 8)
assert mode == expected_mode
# Non-default modes
# Under Windows, changing permissions does not work,
# there we always expect 777
path = os.path.join(str(tmpdir.mkdir('folder9')), 'sub-folder')
os.umask(0)
p = audeer.mkdir(path, mode=0o775)
p = audeer.mkdir(tmpdir, 'folder9', 'sub-folder', mode=0o775)
expected_mode = '775'
if platform.system() == 'Windows':
expected_mode = '777'
mode = stat.S_IMODE(os.stat(p).st_mode)
assert mode == int(expected_mode, 8)
assert mode != int('755', 8)
path = os.path.join(str(tmpdir.mkdir('folder10')), 'sub-folder')
os.umask(0)
p = audeer.mkdir(path, mode=0o755)
p = audeer.mkdir(tmpdir, 'folder10', 'sub-folder', mode=0o755)
expected_mode = '755'
if platform.system() == 'Windows':
expected_mode = '777'
Expand Down Expand Up @@ -1208,9 +1205,8 @@ def test_rmdir(tmpdir):
audeer.rmdir(p)
assert not os.path.exists(p)
# Folder with folder content
path = str(tmpdir.mkdir('folder'))
p = audeer.mkdir(os.path.join(path, 'sub-folder'))
audeer.rmdir(os.path.dirname(p))
p = audeer.mkdir(tmpdir, 'folder', 'sub-folder')
audeer.rmdir(tmpdir, 'folder')
assert not os.path.exists(p)
assert not os.path.exists(os.path.dirname(p))
# Relative path
Expand Down

0 comments on commit 48fdf9f

Please sign in to comment.