Skip to content

Commit 82fa1eb

Browse files
authored
Supported prefix and suffix in TmpDirIfNecessary (#12)
This patch adds the optional arguments `prefix` and `suffix` in `TmpDirIfNecessary` which are then passed over to `mkdtemp`. Fixes #11.
1 parent 21fd2b9 commit 82fa1eb

3 files changed

Lines changed: 36 additions & 11 deletions

File tree

pylint.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ generated-members=bottle\.request\.forms\.decode,bottle\.request\.query\.decode
77
max-line-length=120
88

99
[MESSAGES CONTROL]
10-
disable=too-few-public-methods,abstract-class-little-used,len-as-condition,bad-continuation,bad-whitespace
10+
disable=too-few-public-methods,abstract-class-little-used,len-as-condition,bad-continuation,bad-whitespace,too-many-arguments
1111

temppathlib/__init__.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,32 @@ class TmpDirIfNecessary:
4141
def __init__(self,
4242
path: Union[None, str, pathlib.Path],
4343
base_tmp_dir: Union[None, str, pathlib.Path] = None,
44-
dont_delete_tmp_dir: bool = False) -> None:
44+
dont_delete_tmp_dir: bool = False,
45+
prefix: Optional[str] = None,
46+
suffix: Optional[str] = None) -> None:
4547
"""
4648
Initialize with the given values.
4749
48-
:param path: provided path to the directory; if specified, no temporary directory is created.
49-
:param base_tmp_dir: parent directory of the temporary directories; if not set,
50-
the default is used (usually '/tmp'). This path is only used if a temporary directory needs to be created
51-
and has no effect if 'path' was provided.
50+
:param path:
51+
provided path to the directory; if specified, no temporary directory is created.
52+
53+
:param base_tmp_dir:
54+
parent directory of the temporary directories; if not set,
55+
the default is used (usually '/tmp'). This path is only used if a temporary directory needs to be created
56+
and has no effect if 'path' was provided.
57+
58+
:param dont_delete_tmp_dir:
59+
if set, the temporary directory is not deleted upon close.
5260
53-
:param dont_delete_tmp_dir: if set, the temporary directory is not deleted upon close.
61+
If the 'path' was provided, this argument has no effect.
5462
55-
If the 'path' was provided, this argument has no effect.
63+
:param prefix:
64+
If 'prefix' is not None, the name will begin with that prefix,
65+
otherwise a default prefix is used.
66+
67+
:param suffix:
68+
If 'suffix' is not None, the name will end with that suffix,
69+
otherwise a default suffix is used.
5670
"""
5771
if base_tmp_dir is None:
5872
self.base_tmp_dir = base_tmp_dir
@@ -76,6 +90,9 @@ def __init__(self,
7690

7791
self.dont_delete = dont_delete_tmp_dir
7892

93+
self._prefix = prefix
94+
self._suffix = suffix
95+
7996
self.__use_tmp_dir = path is None
8097

8198
self.exited = False
@@ -96,9 +113,10 @@ def __enter__(self) -> 'TmpDirIfNecessary':
96113

97114
if self._path is None:
98115
if self.base_tmp_dir is None:
99-
self._path = pathlib.Path(tempfile.mkdtemp())
116+
self._path = pathlib.Path(tempfile.mkdtemp(prefix=self._prefix, suffix=self._suffix))
100117
else:
101-
self._path = pathlib.Path(tempfile.mkdtemp(dir=str(self.base_tmp_dir)))
118+
self._path = pathlib.Path(
119+
tempfile.mkdtemp(dir=str(self.base_tmp_dir), prefix=self._prefix, suffix=self._suffix))
102120
else:
103121
self._path.mkdir(exist_ok=True, parents=True)
104122

@@ -213,7 +231,6 @@ def __init__(
213231
214232
:param delete: whether the file is deleted on close (default True).
215233
"""
216-
# pylint: disable=too-many-arguments
217234
self.__tmpfile = tempfile.NamedTemporaryFile(
218235
mode=mode,
219236
buffering=buffering,

tests/test_temppathlib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ def test_with_base_tmp_dir(self) -> None:
9494
finally:
9595
shutil.rmtree(str(basedir))
9696

97+
def test_prefix(self) -> None:
98+
with temppathlib.TmpDirIfNecessary(path=None, prefix="some_prefix") as tmp_dir:
99+
self.assertTrue(tmp_dir.path.name.startswith("some_prefix"))
100+
101+
def test_suffix(self) -> None:
102+
with temppathlib.TmpDirIfNecessary(path=None, suffix="some_suffix") as tmp_dir:
103+
self.assertTrue(tmp_dir.path.name.endswith("some_suffix"))
104+
97105

98106
class TestTemporaryDirectory(unittest.TestCase):
99107
def test_that_it_works(self) -> None:

0 commit comments

Comments
 (0)