diff options
author | Kyomotoi <[email protected]> | 2022-11-04 10:19:24 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2022-11-04 10:19:24 +0800 |
commit | 24342060a99979e6c51390cfe8d86b177136d5c5 (patch) | |
tree | 8a9fed5feee61574a904d199003a9fbc41338276 /ATRI/utils | |
parent | 790e4b49e4394565aba3a76c10ed6b90ffb37c9b (diff) | |
download | ATRI-24342060a99979e6c51390cfe8d86b177136d5c5.tar.gz ATRI-24342060a99979e6c51390cfe8d86b177136d5c5.tar.bz2 ATRI-24342060a99979e6c51390cfe8d86b177136d5c5.zip |
🎨 优化代码
Diffstat (limited to 'ATRI/utils')
-rw-r--r-- | ATRI/utils/__init__.py | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/ATRI/utils/__init__.py b/ATRI/utils/__init__.py index 9d92977..29858f2 100644 --- a/ATRI/utils/__init__.py +++ b/ATRI/utils/__init__.py @@ -1,10 +1,11 @@ import os import re -import string +import json import pytz import yaml -import aiofiles import time +import string +import aiofiles from pathlib import Path from random import sample from datetime import datetime @@ -111,25 +112,25 @@ class MessageChecker: class FileDealer: """ - 打开文件 + 操作文件 """ def __init__(self, path: Path, encoding: str = "utf-8"): self.path = path self.encoding = encoding - async def write(self, path: Path, content): + async def write(self, content): try: - async with aiofiles.open(path, "w", encoding=self.encoding) as target: + async with aiofiles.open(self.path, "w", encoding=self.encoding) as target: await target.write(content) except Exception: - raise Exception(f"Writing file({path}) failed!") + raise Exception(f"Writing file ({self.path}) failed!") async def _reader(self) -> AsyncTextIOWrapper: try: tar = await aiofiles.open(self.path, "r", encoding=self.encoding) except Exception: - raise FileNotFoundError(f"File({self.path}) not find!") + raise FileNotFoundError(f"File ({self.path}) not find!") return tar async def read(self): @@ -147,6 +148,9 @@ class FileDealer: async def readtable(self): tar = await self._reader() return tar.readable() + + def json(self) -> dict: + return json.loads(self.path.read_bytes()) class ImageDealer: |