-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
202 lines (153 loc) · 4.69 KB
/
util.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from typing import List, Dict, TypeVar, Callable, Iterable, Any, Sequence
from difflib import SequenceMatcher
import re
import os
from pathlib import Path
import platform
T = TypeVar("T")
all_true = ["true", "yes", "y", "ok"]
system_type = platform.system()
def to_bool(s: str, empty_means=False) -> bool:
if len(s) == 0 and empty_means: # empty_means True
return True
else:
return s.lower() in all_true
def split_para(args: Iterable[str],
heading="", equal="=") -> Dict[str, str]:
paras = {}
for arg in args:
arg.removeprefix(heading)
name2para = arg.split(equal)
if len(name2para) == 2:
paras[name2para[0]] = name2para[1]
return paras
def check_para_exist(test: Dict[str, str], required: Iterable[str]):
for name in required:
if name not in test.keys():
raise Exception(f"parameter \"{name}\" is missing")
def From(dic: Dict[str, T], *, Get: str, Or: T | Any = None) -> T:
if Get not in dic:
return Or
return dic[Get]
def getAttrOrSet(obj, name: str, default: Callable[[], T]) -> T:
if hasattr(obj, name):
return getattr(obj, name)
else:
new = default()
setattr(obj, name, new)
return new
class Ref:
def __init__(self, value=None):
self.value = value
def contains(small, big) -> bool:
for i in range(len(big) - len(small) + 1):
for j in range(len(small)):
if big[i + j] != small[j]:
break
else:
return True
return False
def read_fi(path: str, mode="r"):
with open(path, mode=mode, encoding="UTF-8") as f:
return f.read()
# noinspection PyBroadException
def delete_fi(path: str) -> bool:
if os.path.exists(path):
try:
os.unlink(path)
return True
except:
return False
return True
# noinspection PyBroadException
def try_cast(*, template: T, attempt: str) -> T | None:
try:
if type(template) == str:
return str(attempt)
if type(template) == int:
return int(attempt)
if type(template) == bool:
return to_bool(attempt)
except:
return None
return None
def try_read_fi(path: str, mode="r") -> None | str:
if os.path.isfile(path):
return read_fi(path, mode)
else:
return None
def write_fi(path: str, content: str, mode="w"):
with open(path, mode=mode, encoding="UTF-8") as f:
f.write(content)
def append_fi(path: str, content: str, mode="a"):
with open(path, mode=mode, encoding="UTF-8") as f:
f.write(content)
def ensure_folder(path: str) -> bool:
if os.path.exists(path):
if os.path.isdir(path):
return True
else:
return False
else:
Path(path).mkdir(parents=True, exist_ok=True)
return True
def fuzzy_match(target: str, candidates: Iterable[str]) -> tuple[str, float]:
largest = None
largest_num = 0.0
for candidate in candidates:
matcher = SequenceMatcher(isjunk=None, a=target, b=candidate)
ratio = matcher.ratio()
if largest is None or ratio > largest_num:
largest = candidate
largest_num = ratio
return largest, largest_num
key_regex = re.compile("[A-Za-z0-9_]*$")
def validate_key(key: str) -> bool:
if len(key) <= 0:
return False
if key[0].isupper():
return False
if " " in key:
return False
if key_regex.match(key) is None:
return False
return True
def env_var_str(name: str) -> str:
if system_type == "Windows":
return f"%{name}%"
else:
return f"${name}"
invalid_file_name_parts = [
'<', '>', ':', ':', ':', '', '/', '\\', '|', '?', '*'
]
def validate_file_name(name: str) -> bool:
if name == "." or name == "..":
return False
for char in name:
if char in invalid_file_name_parts:
return False
return True
class UpdatableFile:
path: str
last_stamp: float
cache: str | None
default: str | None
def __init__(self, path):
self.path = path
self.last_stamp = 0.0
self.cache = None
def exists(self) -> bool:
return os.path.isfile(self.path)
def is_changed(self, *, consume: bool = True) -> bool:
cur_stamp = os.stat(self.path).st_mtime
changed = cur_stamp != self.last_stamp
if consume:
self.last_stamp = cur_stamp
return changed
def read(self) -> str | None:
if self.exists():
if self.is_changed(consume=True):
self.cache = read_fi(self.path)
return self.cache
else:
return self.default