Skip to content

Commit a630ff3

Browse files
authored
Merge pull request #1594 from Ashhar-24/master
Temporary file/directory creation
2 parents 821a6d1 + 2e10795 commit a630ff3

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

copying.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ _the openage authors_ are:
148148
| Zoltán Ács | zoli111 | acszoltan111 à gmail dawt com |
149149
| Trevor Slocum | tslocum | trevor à rocket9labs dawt com |
150150
| Munawar Hafiz | munahaf | munawar dawt hafiz à gmail dawt com |
151+
| Md Ashhar | ashhar | mdashhar01 à gmail dawt com |
151152

152153
If you're a first-time committer, add yourself to the above list. This is not
153154
just for legal reasons, but also to keep an overview of all those nicknames.

openage/util/fslike/path.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
Provides Path, which is analogous to pathlib.Path,
55
and the type of FSLikeObject.root.
66
"""
7+
from typing import NoReturn
78

89
from io import UnsupportedOperation, TextIOWrapper
9-
from typing import NoReturn
10+
import os
11+
import pathlib
12+
import tempfile
1013

1114

1215
class 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

Comments
 (0)