-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
186 lines (142 loc) · 3.95 KB
/
utils.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
import json
import os
import sys
from colorama import Fore, Style
# ---
# Get paths
def get_basepath(self):
'''
获取 base path (main.py 所在目录)
'''
main_py = sys.argv[0]
return os.path.dirname(main_py)
def get_datapath(path):
'''
将相对路径的数据目录改为绝对路径
@param path: 相对路径
'''
return os.path.join(get_basepath(), path)
# ---
# Logging
'''
info 信息
warning 警告
error 错误
debug 调试 -> Only show when `env_debug == True`
@param noret: 是否在输出最后取消换行
+s -> 多行输出 (每行为一参数)
'''
def info(msg, noret=False):
if noret:
print(f'{Fore.GREEN}[I]{Style.RESET_ALL} {msg}', end='')
else:
print(f'{Fore.GREEN}[I]{Style.RESET_ALL} {msg}')
def warning(msg, noret=False):
if noret:
print(f'{Fore.YELLOW}[W]{Style.RESET_ALL} {msg}', end='')
else:
print(f'{Fore.YELLOW}[W]{Style.RESET_ALL} {msg}')
def error(msg, noret=False):
if noret:
print(f'{Fore.RED}[E]{Style.RESET_ALL} {msg}', end='')
else:
print(f'{Fore.RED}[E]{Style.RESET_ALL} {msg}')
def debug(msg, noret=False):
if noret:
print(f'{Fore.BLUE}[D]{Style.RESET_ALL} {msg}', end='')
else:
print(f'{Fore.BLUE}[D]{Style.RESET_ALL} {msg}')
# ↑单行 / ↓多行
def infos(*msgs, noret=False):
for n in msgs:
if n == msgs[-1]:
if noret:
print(f'{Fore.GREEN}[I]{Style.RESET_ALL} {n}', end='')
else:
print(f'{Fore.GREEN}[I]{Style.RESET_ALL} {n}')
else:
print(f'{Fore.GREEN}[I]{Style.RESET_ALL} {n}')
def warnings(*msgs, noret=False):
for n in msgs:
if n == msgs[-1]:
if noret:
print(f'{Fore.YELLOW}[W]{Style.RESET_ALL} {n}', end='')
else:
print(f'{Fore.YELLOW}[W]{Style.RESET_ALL} {n}')
else:
print(f'{Fore.YELLOW}[W]{Style.RESET_ALL} {n}')
def errors(*msgs, noret=False):
for n in msgs:
if n == msgs[-1]:
if noret:
print(f'{Fore.RED}[E]{Style.RESET_ALL} {n}', end='')
else:
print(f'{Fore.RED}[E]{Style.RESET_ALL} {n}')
else:
print(f'{Fore.RED}[E]{Style.RESET_ALL} {n}')
def debugs(*msgs, noret=False):
for n in msgs:
if n == msgs[-1]:
if noret:
print(f'{Fore.BLUE}[D]{Style.RESET_ALL} {n}', end='')
else:
print(f'{Fore.BLUE}[D]{Style.RESET_ALL} {n}')
else:
print(f'{Fore.BLUE}[D]{Style.RESET_ALL} {n}')
# ---
# Format
def format_dict(dic):
'''
列表 -> 格式化 json
@param dic: 列表
'''
return json.dumps(dic, indent=4, ensure_ascii=False, sort_keys=False, separators=(', ', ': '))
def remove_json(lst):
'''
移除列表中每项末尾的 `.json`
@param lst: 列表
'''
lst_after = []
for i in lst:
lst_after += [os.path.splitext(i)[0]]
return lst_after
# ---
# Load / File (dir) option
def load_json(json_name):
'''
加载 json 文件
'''
try:
with open(json_name, 'r', encoding='utf-8') as file:
return json.load(file)
except json.decoder.JSONDecodeError as err:
error(f'Load json file "{json_name}" Failed! Please check the json format!')
raise
def read_dir(dirpath):
'''
遍历文件夹
@param dirpath: 文件夹路径
'''
if not os.path.exists(dirpath):
raise NotADirectoryError(f'{dirpath} not exist.')
indir_list = []
for filename in os.listdir(dirpath):
indir_list += [filename]
return indir_list
# ---
# Terminal option
backlinestr = '\033[F\033[K' # 退行
def backline(num=1):
'''
退行
@param num: 行数
'''
for i in range(num + 1):
print(backlinestr, end='')
# print()
def prints(*args):
'''
方便输出多行文本
'''
for n in args:
print(n)