Skip to content

Commit

Permalink
chore: 优化utils模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Cassius0924 committed Mar 5, 2024
1 parent 4ec4c07 commit 1f6ef75
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 71 deletions.
5 changes: 2 additions & 3 deletions wechatter/commands/_commands/gasoline_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
from bs4 import BeautifulSoup
from loguru import logger

import wechatter.utils.path_manager as pm
from wechatter.commands.handlers import command
from wechatter.exceptions import Bs4ParsingError
from wechatter.models.wechat import SendTo
from wechatter.sender import sender
from wechatter.utils import get_request, load_json
from wechatter.utils import get_abs_path, get_request, load_json


@command(
Expand Down Expand Up @@ -78,7 +77,7 @@ def _parse_gasoline_price_response(response: requests.Response) -> str:
return desired_text


CITY_IDS_PATH = pm.get_abs_path("assets/gasoline_price_china/city_ids.json")
CITY_IDS_PATH = get_abs_path("assets/gasoline_price_china/city_ids.json")


def _get_city_id(city_name: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion wechatter/commands/_commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from wechatter.config import config
from wechatter.models.wechat import SendTo
from wechatter.sender import sender
from wechatter.utils.text_to_image import text_to_image
from wechatter.utils import text_to_image


@command(command="help", keys=["帮助", "help"], desc="获取帮助信息。")
Expand Down
3 changes: 1 addition & 2 deletions wechatter/commands/_commands/openai_chat_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
from wechatter.models.gpt import GptChatInfo
from wechatter.models.wechat import Person, SendTo
from wechatter.sender import sender
from wechatter.utils import post_request_json
from wechatter.utils import join_urls, post_request_json
from wechatter.utils.time import get_current_date, get_current_time, get_current_week
from wechatter.utils.url_joiner import join_urls

DEFAULT_TOPIC = "(对话进行中*)"
# TODO: 初始化对话,Prompt选择
Expand Down
4 changes: 2 additions & 2 deletions wechatter/commands/_commands/qrcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import qrcode as qrc
from loguru import logger

import wechatter.utils.path_manager as pm
from wechatter.commands.handlers import command
from wechatter.models.wechat import SendTo
from wechatter.sender import sender
from wechatter.utils import get_abs_path
from wechatter.utils.time import get_current_datetime


Expand Down Expand Up @@ -39,7 +39,7 @@ def get_qrcode_saved_path(data: str) -> str:
if not data:
raise ValueError("请输入文本或链接")
datetime_str = get_current_datetime()
path = pm.get_abs_path(f"data/qrcodes/{datetime_str}.png")
path = get_abs_path(f"data/qrcodes/{datetime_str}.png")
img = _generate_qrcode(data)
_save_qrcode(img, path)
return path
Expand Down
11 changes: 3 additions & 8 deletions wechatter/commands/_commands/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

from loguru import logger

import wechatter.utils.path_manager as pm
from wechatter.commands.handlers import command
from wechatter.models.wechat import SendTo
from wechatter.sender import sender
from wechatter.utils import load_json, save_json
from wechatter.utils import get_abs_path, load_json, save_json


# TODO: 使用SQLite进行数据持久化
Expand Down Expand Up @@ -64,19 +63,15 @@ def remove_todo_command_handler(to: Union[str, SendTo], message: str = "") -> No

def _load_todos(person_id: str) -> List[str]:
"""加载特定用户的待办事项"""
file_path = pm.get_abs_path(
os.path.join("data", "todos", f"p{person_id}_todo.json")
)
file_path = get_abs_path(os.path.join("data", "todos", f"p{person_id}_todo.json"))
if os.path.exists(file_path):
return load_json(file_path)
return []


def _save_todos(person_id: str, content: List[str]) -> None:
"""保存待办事项到特定用户的 JSON 文件中"""
file_path = pm.get_abs_path(
os.path.join("data", "todos", f"p{person_id}_todo.json")
)
file_path = get_abs_path(os.path.join("data", "todos", f"p{person_id}_todo.json"))
save_json(file_path, content)


Expand Down
5 changes: 2 additions & 3 deletions wechatter/commands/_commands/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from bs4 import BeautifulSoup
from loguru import logger

import wechatter.utils.path_manager as pm
from wechatter.commands.handlers import command
from wechatter.exceptions import Bs4ParsingError
from wechatter.models.wechat import SendTo
from wechatter.sender import sender
from wechatter.utils import get_request, load_json
from wechatter.utils import get_abs_path, get_request, load_json
from wechatter.utils.time import get_current_hour, get_current_minute, get_current_ymdh


Expand Down Expand Up @@ -92,7 +91,7 @@ def weather_command_handler(to: Union[str, SendTo], message: str = "") -> None:
}
# fmt: on

CITY_IDS_PATH = pm.get_abs_path("assets/weather_china/city_ids.json")
CITY_IDS_PATH = get_abs_path("assets/weather_china/city_ids.json")


# 封装起来,方便定时任务调用
Expand Down
2 changes: 1 addition & 1 deletion wechatter/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import yaml
from loguru import logger

from wechatter.utils.path_manager import get_abs_path
from wechatter.utils import get_abs_path


def load_config(config_path="config.yaml") -> Dict:
Expand Down
2 changes: 1 addition & 1 deletion wechatter/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy.orm import sessionmaker

from wechatter.database.tables import Base
from wechatter.utils.path_manager import get_abs_path
from wechatter.utils import get_abs_path

DB_PATH = get_abs_path("data/wechatter.sqlite")

Expand Down
2 changes: 1 addition & 1 deletion wechatter/database/tables/game_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from wechatter.database.tables import Base
from wechatter.models.game.game_states import GameStates as GameStatesModel
from wechatter.utils.unique_list import UniqueListDecoder, UniqueListEncoder
from wechatter.utils import UniqueListDecoder, UniqueListEncoder

if TYPE_CHECKING:
from wechatter.database.tables.group import Group
Expand Down
2 changes: 1 addition & 1 deletion wechatter/games/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from wechatter.commands.handlers import command
from wechatter.models.wechat import Message, SendTo
from wechatter.sender import sender
from wechatter.utils.unique_list import UniqueList
from wechatter.utils import UniqueList

from ._games import * # noqa: F403
from .game import Game
Expand Down
2 changes: 1 addition & 1 deletion wechatter/games/_games/tictactoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing_extensions import override

from wechatter.games.game import Game
from wechatter.utils.path_manager import get_abs_path
from wechatter.utils import get_abs_path


class Tictactoe(Game):
Expand Down
2 changes: 1 addition & 1 deletion wechatter/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from wechatter.models.game import GameStates
from wechatter.models.wechat import Group, Person
from wechatter.sender import sender
from wechatter.utils.unique_list import UniqueList
from wechatter.utils import UniqueList


class Game(ABC):
Expand Down
2 changes: 1 addition & 1 deletion wechatter/init_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from loguru import logger

from wechatter.utils.path_manager import get_abs_path
from wechatter.utils import get_abs_path

# 使用环境变量中的 LOG_LEVEL
LOG_LEVEL_NAME = os.environ.get("WECHATTER_LOG_LEVEL", "INFO")
Expand Down
3 changes: 1 addition & 2 deletions wechatter/sender/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

from wechatter.config import config
from wechatter.sender import sender
from wechatter.utils import get_request
from wechatter.utils.url_joiner import join_urls
from wechatter.utils import get_request, join_urls

if TYPE_CHECKING:
from wechatter.models.wechat import SendTo
Expand Down
5 changes: 2 additions & 3 deletions wechatter/sender/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import tenacity
from loguru import logger

import wechatter.utils.http_request as http_request
from wechatter.config import config
from wechatter.models.wechat import QuotedResponse, SendTo
from wechatter.sender.quotable import make_quotable
from wechatter.utils.url_joiner import join_urls
from wechatter.utils import join_urls, post_request


# 对retry装饰器重新包装,增加日志输出
Expand Down Expand Up @@ -94,7 +93,7 @@ def logging_wrapper(*args, **kwargs):
def _post_request(
url, data=None, json=None, files=None, headers={}, timeout=5
) -> requests.Response:
return http_request.post_request(
return post_request(
url, data=data, json=json, files=files, headers=headers, timeout=timeout
)

Expand Down
16 changes: 16 additions & 0 deletions wechatter/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# isort:skip_file
from .path_manager import get_abs_path, is_file_exist, join_path
from .text_to_image import text_to_image
from .file_manager import check_and_create_file, check_and_create_folder
from .http_request import get_request, get_request_json, post_request, post_request_json
from .json_manager import load_json, save_json
from .unique_list import UniqueList, UniqueListDecoder, UniqueListEncoder
from .url_codec import url_decode, url_encode
from .url_joiner import join_urls

__all__ = [
"load_json",
Expand All @@ -11,4 +17,14 @@
"post_request_json",
"url_encode",
"url_decode",
"text_to_image",
"get_abs_path",
"join_path",
"is_file_exist",
"join_urls",
"check_and_create_folder",
"check_and_create_file",
"UniqueList",
"UniqueListEncoder",
"UniqueListDecoder",
]
Loading

0 comments on commit 1f6ef75

Please sign in to comment.