-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_util.py
73 lines (57 loc) · 1.64 KB
/
app_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
import time, re, random, os, sys
from typing import Callable
import asyncio
import string
import quart
def prxxx(*args, q: bool = False, from_debug=False, **kwargs):
if q:
return
pass
if from_debug:
return
pass
print(
time.strftime(
"RWKV [\033[33m%Y-%m-%d %H:%M:%S\033[0m] \033[0m", time.localtime()
),
*args,
"\033[0m",
**kwargs,
)
def log_call(func):
def nfunc(*args, **kwargs):
prxxx(f"Call {func.__name__}", from_debug=True)
return func(*args, **kwargs)
return nfunc
def use_async_lock(func):
lock = asyncio.locks.Lock()
async def nfunc(*args, **kwargs):
async with lock:
return await func(*args, **kwargs)
return nfunc
def run_in_async_thread(func):
if sys.version_info.minor < 9:
async def nfunc(*args, **kwargs) -> asyncio.coroutine:
return func(*args, **kwargs)
return nfunc
async def nfunc(*args, **kwargs) -> asyncio.coroutine:
thread = asyncio.to_thread(func, *args, **kwargs)
return await thread
return nfunc
symbols = "[!@#$%^&*+[\]{};:/<>?\|`~]"
def clean_symbols(s):
return "".join([c for c in s if c not in symbols])
def gen_echo():
return "".join(random.sample(string.ascii_lowercase + string.digits, 4))
def check_dir(path):
if not os.path.isdir(path):
os.makedirs(path)
def check_file(path):
return os.path.isfile(path)
@run_in_async_thread
def check_dir_async(path):
if not os.path.isdir(path):
os.makedirs(path)
@run_in_async_thread
def check_file_async(path):
return os.path.isfile(path)