Skip to content

Commit

Permalink
Use user's cache dir as fallback and make sure file is closed if copy…
Browse files Browse the repository at this point in the history
…ing fails
  • Loading branch information
fkglr committed Jun 20, 2024
1 parent 3963fd8 commit 99acbeb
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/cursed_hr/cursed_hr.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pathlib import Path
from typing import Any, BinaryIO

import platformdirs
import zstandard as zstd
from gallia.log import PenlogPriority, PenlogRecord
from gallia.services.uds.core.service import NegativeResponse, UDSRequest, UDSResponse
Expand Down Expand Up @@ -279,18 +280,26 @@ def copy_to_file(tmp_file: Any):
self.window.addstr(f"Loading contents from {self.in_file}: Decompressing file ...")
self.window.refresh()

file = tempfile.TemporaryFile()

try:
file = tempfile.TemporaryFile()
copy_to_file(file)
except OSError:
self.window.erase()
self.window.addstr(
f"Could not decompress to {tempfile.gettempdir()}. Trying to decompress to source directory ..."
)
self.window.refresh()
try:
copy_to_file(file)
except OSError:
file.close()

self.window.erase()
self.window.addstr(
f"Could not decompress to {tempfile.gettempdir()}. Trying to decompress to source directory ..."
)
self.window.refresh()

file = tempfile.TemporaryFile(dir=platformdirs.user_cache_dir())

file = tempfile.TemporaryFile(dir=self.in_file.parent)
copy_to_file(file)
copy_to_file(file)
except:
file.close()
raise
else:
file = self.in_file.open("rb") # type: ignore

Expand Down

0 comments on commit 99acbeb

Please sign in to comment.