From 3bd0674baa59ef268473b498ab55aec0c157c878 Mon Sep 17 00:00:00 2001 From: Ashhar-24 Date: Wed, 1 Nov 2023 02:21:24 +0530 Subject: [PATCH 1/2] util: Temporary file/directory support. --- copying.md | 1 + openage/util/fslike/path.py | 53 +++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/copying.md b/copying.md index 310076a011..e1df2d97bb 100644 --- a/copying.md +++ b/copying.md @@ -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. diff --git a/openage/util/fslike/path.py b/openage/util/fslike/path.py index f69479360e..7532ec3e0d 100644 --- a/openage/util/fslike/path.py +++ b/openage/util/fslike/path.py @@ -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: @@ -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() @@ -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) @@ -330,3 +336,46 @@ 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 + + def __del__(self): + """ + Destructor used for temp files and directories. + """ + if self.is_temp: + # Cleanup temp file + if self.exists(): + if self.is_file(): + self.unlink() + + elif self.is_dir(): + self.removerecursive() From 2e10795d7b3649d0870efb60196abc5649555a35 Mon Sep 17 00:00:00 2001 From: heinezen Date: Wed, 1 Nov 2023 01:42:24 +0100 Subject: [PATCH 2/2] util: Remove __del__ because it doesn't work as destructor. --- openage/util/fslike/path.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/openage/util/fslike/path.py b/openage/util/fslike/path.py index 7532ec3e0d..c34a5d6a6c 100644 --- a/openage/util/fslike/path.py +++ b/openage/util/fslike/path.py @@ -366,16 +366,3 @@ def get_temp_dir(): path.is_temp = True return path - - def __del__(self): - """ - Destructor used for temp files and directories. - """ - if self.is_temp: - # Cleanup temp file - if self.exists(): - if self.is_file(): - self.unlink() - - elif self.is_dir(): - self.removerecursive()