Skip to content

Commit

Permalink
util: Temporary file/directory support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashhar-24 authored and heinezen committed Nov 1, 2023
1 parent 821a6d1 commit 3bd0674
Show file tree
Hide file tree
Showing 2 changed files with 52 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
53 changes: 51 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,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()

0 comments on commit 3bd0674

Please sign in to comment.