Skip to content

Commit 8e485ee

Browse files
committed
Added additional arguments to maybe_make to mirror pathlib.Path.mkdir()
1 parent 8b41c59 commit 8e485ee

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

domdf_python_tools/paths.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,31 @@ def copytree(src, dst, symlinks=False, ignore=None):
9191
return shutil.copy2(s, d)
9292

9393

94-
def maybe_make(directory):
94+
def maybe_make(directory, mode=0o777, parents=False, exist_ok=False):
9595
"""
96-
Makes a directory only if it doesn't already exist
97-
96+
Create a directory at this given path, but only if the directory does
97+
not already exist.
98+
9899
:param directory: Directory to create
99100
:type directory: str or pathlib.Path
100-
101+
:param mode: Combined with the process’ umask value to determine the file mode and access flags
102+
:type mode:
103+
:param parents: If ``False`` (the default), a missing parent raises a :class:`~python:FileNotFoundError`.
104+
If ``True``, any missing parents of this path are created as needed; they are created with the
105+
default permissions without taking mode into account (mimicking the POSIX mkdir -p command).
106+
:type parents: bool
107+
:param exist_ok: If ``False`` (the default), a :class:`~python:FileExistsError` is raised if the
108+
target directory already exists. If ``True``, :class:`~python:FileExistsError` exceptions
109+
will be ignored (same behavior as the POSIX mkdir -p command), but only if the last path
110+
component is not an existing non-directory file.
111+
:type exist_ok: bool
101112
"""
102113

103114
if not isinstance(directory, pathlib.Path):
104115
directory = pathlib.Path(directory)
105116

106117
if not directory.exists():
107-
directory.mkdir()
118+
directory.mkdir(mode, parents, exist_ok)
108119

109120

110121
def parent_path(path):

tests/test_paths.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121

2222
def test_maybe_make():
23+
# TODO: test making parents
24+
2325
# TODO: test with strings as well as pathlib
2426
with TemporaryDirectory() as tmpdir:
2527

0 commit comments

Comments
 (0)