-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathutils.py
109 lines (86 loc) · 2.5 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
from datetime import datetime
import json
from flask import make_response, Response
def info(log):
print(f"[Info] {datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')} " + log)
def infon(log):
print(f"\n[Info] {datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')} " + log)
def warning(log):
print(f"[Warning] {datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')} " + log)
def error(log):
print(f"[Error] {datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')} " + log)
def format_dict(dic) -> Response:
'''
字典 -> Response (内容为格式化后的 json 文本)
@param dic: 字典
'''
response = make_response(
json.dumps(dic, indent=4, ensure_ascii=False, sort_keys=False, separators=(', ', ': '))
)
response.mimetype = 'application/json'
return response
def reterr(code: int, message: str) -> str:
'''
返回错误信息 json
:param code: 代码
:param message: 消息
'''
ret = {
'success': False,
'code': code,
'message': message
}
error(f'{code} - {message}')
return format_dict(ret)
def tobool(string: str, throw: bool = False) -> bool:
'''
将形似 `true`, `1`, `t`, `yes`, `y` 之类的内容转换为布尔值
:param throw: 控制不匹配时是否直接抛出错误
:return: `True` or `False` or `None` (如果不在 `booldict` 内)
'''
booldict = {
'true': True,
'false': False,
'1': True,
'0': False,
't': True,
'f': False,
'yes': True,
'no': False,
'y': True,
'n': False,
'on': True,
'off': False,
'enable': True,
'disable': False,
'active': True,
'inactive': False,
'positive': True,
'negative': False
}
try:
ret = booldict[str(string).lower()]
except KeyError:
if throw:
raise
else:
ret = None
return ret
@property
def show_404():
return '<!DOCTYPE HTML>\n<html lang=en>\n<title>404 Not Found</title>\n<h1>Not Found</h1>\n<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>', 404
class SleepyException(Exception):
'''
Custom Exception
'''
def __init__(self, msg = None):
if msg:
self.msg = msg
def __str__(self):
return self.msg
def exception(msg: str):
'''
抛出 SleepyException
:param msg: 错误描述
'''
raise SleepyException(msg)