-
Notifications
You must be signed in to change notification settings - Fork 2
/
dot_sdkmod.py
78 lines (63 loc) · 2.37 KB
/
dot_sdkmod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import zipfile
from collections.abc import Iterator
from contextlib import AbstractContextManager, contextmanager
from io import TextIOWrapper
from pathlib import Path, PurePosixPath, PureWindowsPath
from typing import IO, Literal, overload
@overload
def open_in_mod_dir(
path: Path,
binary: Literal[False] = False,
) -> AbstractContextManager[IO[str]]: ...
@overload
def open_in_mod_dir(path: Path, binary: Literal[True]) -> AbstractContextManager[IO[bytes]]: ...
@contextmanager
def open_in_mod_dir(
path: Path,
binary: bool = False,
) -> Iterator[IO[str]] | Iterator[IO[bytes]]:
"""
Opens a file in a mod folder for reading, handling possibly being inside a .sdkmod.
Transforms the `KeyError` from a file not existing in a zip into a `FileNotFoundError` - so you
only need to catch a single exception.
Args:
path: The path to open. This should generally be constructed relative to `__file__`.
binary: True if to open in binary mode.
Returns:
An open file object.
"""
dot_sdkmod = next(
(p for p in path.parents if p.is_file() and p.suffix == ".sdkmod"),
None,
)
# If we don't have a .sdkmod, if we're just an actual directory
if dot_sdkmod is None:
# Use a plain open
# Separate the two paths for type hinting
if binary:
with path.open("rb") as file:
yield file
else:
with path.open("r") as file:
yield file
return
# If in a .sdkmod
with zipfile.ZipFile(str(dot_sdkmod)) as zip_file:
# Ensure the path has posix separators
relative_path = path.relative_to(dot_sdkmod)
# According to the standard, zip files are suppoosed to use posix separators
# However, some old programs throw windows ones in
# If we don't see the file, try the other separator - and if that fails just let the
# exception bubble up
inner_path = zipfile.Path(zip_file, str(PurePosixPath(relative_path)))
if not inner_path.exists():
inner_path = zipfile.Path(zip_file, str(PureWindowsPath(relative_path)))
try:
file = inner_path.open("rb")
except KeyError as ex:
raise FileNotFoundError from ex
with file:
if binary:
yield file
else:
yield TextIOWrapper(file)