@@ -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 ,
0 commit comments