summaryrefslogtreecommitdiff
path: root/ATRI/utils/file.py
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2021-02-20 08:19:41 +0800
committerKyomotoi <[email protected]>2021-02-20 08:19:41 +0800
commitae7ea2d379ec7fb0edb8e333145bb141106a2fd2 (patch)
tree3243cb1dd668d36f92f17e958ff2a38e7b2c450c /ATRI/utils/file.py
parent3d16960b487f8911fb3817c2abcdb252ad848f58 (diff)
downloadATRI-ae7ea2d379ec7fb0edb8e333145bb141106a2fd2.tar.gz
ATRI-ae7ea2d379ec7fb0edb8e333145bb141106a2fd2.tar.bz2
ATRI-ae7ea2d379ec7fb0edb8e333145bb141106a2fd2.zip
✨ 更新插件,埋下bug
- 更新插件: - call-owner - code-runner - status - anime-search - tex(待修复) - 埋下bug: - service中limit作为机器人服务中的开关,目前写入文件亟待修复
Diffstat (limited to 'ATRI/utils/file.py')
-rw-r--r--ATRI/utils/file.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/ATRI/utils/file.py b/ATRI/utils/file.py
new file mode 100644
index 0000000..4699f5d
--- /dev/null
+++ b/ATRI/utils/file.py
@@ -0,0 +1,27 @@
+import aiofiles
+from pathlib import Path
+
+from ATRI.exceptions import WriteError
+
+
+async def write_file(path: Path, text, encoding='utf-8') -> None:
+ try:
+ async with aiofiles.open(path, 'w', encoding=encoding) as target:
+ await target.write(text)
+ except WriteError:
+ raise WriteError("Writing file failed!")
+
+
+async def open_file(path: Path, method, encoding='utf-8'):
+ try:
+ async with aiofiles.open(path, 'r', encoding=encoding) as target:
+ if method == "read":
+ return target.read()
+ elif method == "readlines":
+ return await target.readlines()
+ elif method == "readline":
+ return await target.readline()
+ else:
+ return target.readable()
+ except EOFError:
+ raise EOFError("File not fond!")