Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary file/directory creation #1594

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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