-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileutils.py
117 lines (74 loc) · 2.4 KB
/
fileutils.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import struct
import sys
from os import getenv
from pathlib import Path
from tools import align_byte_size, get_alignment
def resource_path(relPath: str = "") -> Path:
"""
Get absolute path to resource, works for dev and for cx_freeze
"""
import sys
if hasattr(sys, "_MEIPASS"):
return Path(getattr(sys, "_MEIPASS", Path(__file__).parent)) / relPath
else:
if getattr(sys, "frozen", False):
# The application is frozen
base_path = Path(sys.executable).parent
else:
base_path = Path(__file__).parent
return base_path / relPath
def get_program_folder(folder: str = "") -> Path:
"""Get path to appdata"""
if sys.platform == "win32":
datapath = Path(getenv("APPDATA")) / folder
elif sys.platform == "darwin":
if folder:
folder = "." + folder
datapath = Path("~/Library/Application Support").expanduser() / folder
elif "linux" in sys.platform:
if folder:
folder = "." + folder
datapath = Path.home() / folder
else:
raise NotImplementedError(f"{sys.platform} OS is unsupported")
return datapath
def read_sbyte(f):
return struct.unpack("b", f.read(1))[0]
def write_sbyte(f, val):
f.write(struct.pack("b", val))
def read_sint16(f):
return struct.unpack(">h", f.read(2))[0]
def write_sint16(f, val):
f.write(struct.pack(">h", val))
def read_sint32(f):
return struct.unpack(">i", f.read(4))[0]
def write_sint32(f, val):
f.write(struct.pack(">i", val))
def read_ubyte(f):
return struct.unpack("B", f.read(1))[0]
def write_ubyte(f, val):
f.write(struct.pack("B", val))
def read_uint16(f):
return struct.unpack(">H", f.read(2))[0]
def write_uint16(f, val):
f.write(struct.pack(">H", val))
def read_uint32(f):
return struct.unpack(">I", f.read(4))[0]
def write_uint32(f, val):
f.write(struct.pack(">I", val))
def read_float(f):
return struct.unpack(">f", f.read(4))[0]
def write_float(f, val):
f.write(struct.pack(">f", val))
def read_double(f):
return struct.unpack(">d", f.read(4))[0]
def write_double(f, val):
f.write(struct.pack(">d", val))
def read_bool(f, vSize=1):
return struct.unpack("B", f.read(vSize))[0] > 0
def write_bool(f, val, vSize=1):
(
f.write(b"\x00" * (vSize - 1) + b"\x01")
if val is True
else f.write(b"\x00" * vSize)
)