Skip to content

Commit

Permalink
Merge pull request #1594 from Ashhar-24/master
Browse files Browse the repository at this point in the history
Temporary file/directory creation
  • Loading branch information
heinezen authored Nov 1, 2023
2 parents 821a6d1 + 2e10795 commit a630ff3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ _the openage authors_ are:
| Zoltán Ács | zoli111 | acszoltan111 à gmail dawt com |
| Trevor Slocum | tslocum | trevor à rocket9labs dawt com |
| Munawar Hafiz | munahaf | munawar dawt hafiz à gmail dawt com |
| Md Ashhar | ashhar | mdashhar01 à gmail dawt com |

If you're a first-time committer, add yourself to the above list. This is not
just for legal reasons, but also to keep an overview of all those nicknames.
Expand Down
40 changes: 38 additions & 2 deletions openage/util/fslike/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
Provides Path, which is analogous to pathlib.Path,
and the type of FSLikeObject.root.
"""
from typing import NoReturn

from io import UnsupportedOperation, TextIOWrapper
from typing import NoReturn
import os
import pathlib
import tempfile


class Path:
Expand All @@ -32,7 +35,7 @@ class Path:
# lower.
# pylint: disable=too-many-public-methods

def __init__(self, fsobj, parts=None):
def __init__(self, fsobj, parts: str | bytes | bytearray | list | tuple = None):
if isinstance(parts, str):
parts = parts.encode()

Expand Down Expand Up @@ -63,6 +66,9 @@ def __init__(self, fsobj, parts=None):

self.fsobj = fsobj

# Set to True by create_temp_file or create_temp_dir
self.is_temp: bool = False

# use tuple instead of list to prevent accidential modification
self.parts = tuple(result)

Expand Down Expand Up @@ -330,3 +336,33 @@ def mount(self, pathobj, priority=0) -> NoReturn:
# pylint: disable=no-self-use,unused-argument
# TODO: https://github.com/PyCQA/pylint/issues/2329
raise PermissionError("Do not call mount on Path instances!")

@staticmethod
def get_temp_file():
"""
Creates a temporary file.
"""
temp_fd, temp_file = tempfile.mkstemp()

# Close the file descriptor to release resources
os.close(temp_fd)

# Wrap the temporary file path in a Path object and return it
path = Path(pathlib.Path(temp_file))
path.is_temp = True

return path

@staticmethod
def get_temp_dir():
"""
Creates a temporary directory.
"""
# Create a temporary directory using tempfile.mkdtemp
temp_dir = tempfile.mkdtemp()

# Wrap the temporary directory path in a Path object and return it
path = Path(pathlib.Path(temp_dir))
path.is_temp = True

return path

0 comments on commit a630ff3

Please sign in to comment.