44Provides Path, which is analogous to pathlib.Path,
55and the type of FSLikeObject.root.
66"""
7+ from typing import NoReturn
78
89from io import UnsupportedOperation , TextIOWrapper
9- from typing import NoReturn
10+ import os
11+ import pathlib
12+ import tempfile
1013
1114
1215class Path :
@@ -32,7 +35,7 @@ class Path:
3235 # lower.
3336 # pylint: disable=too-many-public-methods
3437
35- def __init__ (self , fsobj , parts = None ):
38+ def __init__ (self , fsobj , parts : str | bytes | bytearray | list | tuple = None ):
3639 if isinstance (parts , str ):
3740 parts = parts .encode ()
3841
@@ -63,6 +66,9 @@ def __init__(self, fsobj, parts=None):
6366
6467 self .fsobj = fsobj
6568
69+ # Set to True by create_temp_file or create_temp_dir
70+ self .is_temp : bool = False
71+
6672 # use tuple instead of list to prevent accidential modification
6773 self .parts = tuple (result )
6874
@@ -330,3 +336,33 @@ def mount(self, pathobj, priority=0) -> NoReturn:
330336 # pylint: disable=no-self-use,unused-argument
331337 # TODO: https://github.com/PyCQA/pylint/issues/2329
332338 raise PermissionError ("Do not call mount on Path instances!" )
339+
340+ @staticmethod
341+ def get_temp_file ():
342+ """
343+ Creates a temporary file.
344+ """
345+ temp_fd , temp_file = tempfile .mkstemp ()
346+
347+ # Close the file descriptor to release resources
348+ os .close (temp_fd )
349+
350+ # Wrap the temporary file path in a Path object and return it
351+ path = Path (pathlib .Path (temp_file ))
352+ path .is_temp = True
353+
354+ return path
355+
356+ @staticmethod
357+ def get_temp_dir ():
358+ """
359+ Creates a temporary directory.
360+ """
361+ # Create a temporary directory using tempfile.mkdtemp
362+ temp_dir = tempfile .mkdtemp ()
363+
364+ # Wrap the temporary directory path in a Path object and return it
365+ path = Path (pathlib .Path (temp_dir ))
366+ path .is_temp = True
367+
368+ return path
0 commit comments