Compare commits
2 Commits
51de53d01c
...
2d7a2505d4
| Author | SHA1 | Date | |
|---|---|---|---|
|
2d7a2505d4
|
|||
|
a82eaaaec5
|
+8
-2
@@ -1,6 +1,7 @@
|
|||||||
import aiofiles.os
|
import aiofiles.os
|
||||||
import aiofiles
|
import aiofiles
|
||||||
import os
|
import os
|
||||||
|
import inspect
|
||||||
|
|
||||||
async def read_file(path: str, callback=None):
|
async def read_file(path: str, callback=None):
|
||||||
if not await aiofiles.os.path.exists(path):
|
if not await aiofiles.os.path.exists(path):
|
||||||
@@ -8,7 +9,10 @@ async def read_file(path: str, callback=None):
|
|||||||
|
|
||||||
async with aiofiles.open(path, "r") as f:
|
async with aiofiles.open(path, "r") as f:
|
||||||
if callback:
|
if callback:
|
||||||
return await callback(f)
|
result = callback(f)
|
||||||
|
if inspect.isawaitable(result):
|
||||||
|
return await result
|
||||||
|
return result
|
||||||
return await f.read()
|
return await f.read()
|
||||||
|
|
||||||
|
|
||||||
@@ -19,6 +23,8 @@ async def save_file(path: str, data, callback=None, *args, **kwargs):
|
|||||||
|
|
||||||
async with aiofiles.open(path, "w") as f:
|
async with aiofiles.open(path, "w") as f:
|
||||||
if callback:
|
if callback:
|
||||||
await callback(data, f, *args, **kwargs)
|
result = callback(data, f, *args, **kwargs)
|
||||||
|
if inspect.isawaitable(result):
|
||||||
|
await result
|
||||||
else:
|
else:
|
||||||
await f.write(data)
|
await f.write(data)
|
||||||
|
|||||||
@@ -153,7 +153,7 @@ class LocalStorage:
|
|||||||
True if game_board.winner_snake_names and "me" in game_board.winner_snake_names else False
|
True if game_board.winner_snake_names and "me" in game_board.winner_snake_names else False
|
||||||
)
|
)
|
||||||
|
|
||||||
await save_file(save_file_path, {
|
payload = {
|
||||||
"winner": game_board.winner_snake_names,
|
"winner": game_board.winner_snake_names,
|
||||||
"game": {
|
"game": {
|
||||||
"url": game_board.url,
|
"url": game_board.url,
|
||||||
@@ -169,11 +169,9 @@ class LocalStorage:
|
|||||||
"calculations": game_board.snake_class.get_history(),
|
"calculations": game_board.snake_class.get_history(),
|
||||||
},
|
},
|
||||||
"dataset": dataset,
|
"dataset": dataset,
|
||||||
},
|
}
|
||||||
callback=json.dump,
|
|
||||||
indent=2,
|
await save_file(save_file_path, json.dumps(payload, indent=2, ensure_ascii=False))
|
||||||
ensure_ascii=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user