change from sync to async and from flask to quart
Build and Push Docker Container / build-and-push (push) Successful in 1m35s
Build and Push Docker Container / build-and-push (push) Successful in 1m35s
This commit is contained in:
+19
-11
@@ -1,16 +1,24 @@
|
||||
import aiofiles.os
|
||||
import aiofiles
|
||||
import os
|
||||
|
||||
def read_file(path, callback=None):
|
||||
if os.path.exists(path):
|
||||
with open(path, 'r') as f:
|
||||
data = callback(f)
|
||||
return data
|
||||
else:
|
||||
async def read_file(path: str, callback=None):
|
||||
if not await aiofiles.os.path.exists(path):
|
||||
return None
|
||||
|
||||
def save_file(path, data, callback=None, *args, **kwargs):
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
async with aiofiles.open(path, "r") as f:
|
||||
if callback:
|
||||
return await callback(f)
|
||||
return await f.read()
|
||||
|
||||
with open(path, 'w') as f:
|
||||
callback(data, f, *args, **kwargs)
|
||||
|
||||
async def save_file(path: str, data, callback=None, *args, **kwargs):
|
||||
dir_path = os.path.dirname(path)
|
||||
if dir_path:
|
||||
await aiofiles.os.makedirs(dir_path, exist_ok=True)
|
||||
|
||||
async with aiofiles.open(path, "w") as f:
|
||||
if callback:
|
||||
await callback(data, f, *args, **kwargs)
|
||||
else:
|
||||
await f.write(data)
|
||||
|
||||
Reference in New Issue
Block a user