From f8d0b1b8a6dc0cdb07afd49bcf06fdc83bb185cc Mon Sep 17 00:00:00 2001 From: Andrey Tikhonov <17@itishka.org> Date: Sun, 1 Oct 2023 15:03:02 +0400 Subject: [PATCH 1/2] reformat --- src/sulguk/post_manager/chat_info.py | 2 +- src/sulguk/post_manager/file.py | 6 +++--- src/sulguk/post_manager/links.py | 12 ++++++------ src/sulguk/post_manager/params.py | 6 +++--- src/sulguk/post_manager/sender.py | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/sulguk/post_manager/chat_info.py b/src/sulguk/post_manager/chat_info.py index 78be3aa..6f9f5a6 100644 --- a/src/sulguk/post_manager/chat_info.py +++ b/src/sulguk/post_manager/chat_info.py @@ -15,5 +15,5 @@ async def get_chat(bot: Bot, chat_id: Union[str, int]): except TelegramBadRequest as e: if "chat not found" in e.message: logger.error("Chat %s not found", chat_id) - raise ChatNotFound + raise ChatNotFound from e raise diff --git a/src/sulguk/post_manager/file.py b/src/sulguk/post_manager/file.py index 7736571..a354a04 100644 --- a/src/sulguk/post_manager/file.py +++ b/src/sulguk/post_manager/file.py @@ -1,6 +1,6 @@ import logging -from sulguk import transform_html, RenderResult +from sulguk import RenderResult, transform_html from .exceptions import ManagerError logger = logging.getLogger(__name__) @@ -10,6 +10,6 @@ def load_file(filename) -> RenderResult: try: with open(filename) as f: return transform_html(f.read()) - except FileNotFoundError: + except FileNotFoundError as e: logger.error("File `%s` not found", filename) - raise ManagerError + raise ManagerError from e diff --git a/src/sulguk/post_manager/links.py b/src/sulguk/post_manager/links.py index 38020f2..6e06638 100644 --- a/src/sulguk/post_manager/links.py +++ b/src/sulguk/post_manager/links.py @@ -1,8 +1,8 @@ from dataclasses import dataclass from typing import Optional, Union -from urllib.parse import urlparse, parse_qs +from urllib.parse import parse_qs, urlparse -from aiogram.types import Message, Chat +from aiogram.types import Chat, Message @dataclass @@ -40,16 +40,16 @@ def parse_link(link: str) -> Link: params = parse_qs(parsed.query) try: post_id = int(path[1]) - except ValueError: - raise LinkParseError(f"Invalid post id: {path[1]}") + except ValueError as e: + raise LinkParseError(f"Invalid post id: {path[1]}") from e comment_id_raw = params.get("comment") if not comment_id_raw: comment_id = None elif len(comment_id_raw) == 1: try: comment_id = int(comment_id_raw[0]) - except ValueError: - raise LinkParseError(f"Invalid comment id: {path[1]}") + except ValueError as e: + raise LinkParseError(f"Invalid comment id: {path[1]}") from e else: raise LinkParseError(f"Cannot parse comment id: {parsed.query}") return Link( diff --git a/src/sulguk/post_manager/params.py b/src/sulguk/post_manager/params.py index 96238b8..477b484 100644 --- a/src/sulguk/post_manager/params.py +++ b/src/sulguk/post_manager/params.py @@ -22,10 +22,10 @@ def init_parser(): subparsers = root.add_subparsers(dest="command") sender = subparsers.add_parser("send") sender.add_argument( - "destination", type=parse_link + "destination", type=parse_link, ) sender.add_argument( - "file", nargs='+' + "file", nargs='+', ) sender.add_argument( "-m", "--mode", choices=["poll", "getChat"], @@ -33,7 +33,7 @@ def init_parser(): ) editor = subparsers.add_parser("edit") editor.add_argument( - "destination", type=parse_link + "destination", type=parse_link, ) editor.add_argument( "file", diff --git a/src/sulguk/post_manager/sender.py b/src/sulguk/post_manager/sender.py index f19d6d8..9530df5 100644 --- a/src/sulguk/post_manager/sender.py +++ b/src/sulguk/post_manager/sender.py @@ -3,7 +3,7 @@ from typing import Optional from aiogram import Bot, Dispatcher, F -from aiogram.types import Message, Chat +from aiogram.types import Chat, Message from .chat_info import get_chat from .exceptions import LinkedMessageNotFound From 93e3d0d370e74535c84898b9b4fd889237331b18 Mon Sep 17 00:00:00 2001 From: Andrey Tikhonov <17@itishka.org> Date: Sun, 1 Oct 2023 15:17:05 +0400 Subject: [PATCH 2/2] rename error --- src/sulguk/post_manager/chat_info.py | 4 ++-- src/sulguk/post_manager/exceptions.py | 4 ++-- src/sulguk/post_manager/sender.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/sulguk/post_manager/chat_info.py b/src/sulguk/post_manager/chat_info.py index 6f9f5a6..fce47df 100644 --- a/src/sulguk/post_manager/chat_info.py +++ b/src/sulguk/post_manager/chat_info.py @@ -4,7 +4,7 @@ from aiogram import Bot from aiogram.exceptions import TelegramBadRequest -from .exceptions import ChatNotFound +from .exceptions import ChatNotFoundError logger = getLogger(__name__) @@ -15,5 +15,5 @@ async def get_chat(bot: Bot, chat_id: Union[str, int]): except TelegramBadRequest as e: if "chat not found" in e.message: logger.error("Chat %s not found", chat_id) - raise ChatNotFound from e + raise ChatNotFoundError from e raise diff --git a/src/sulguk/post_manager/exceptions.py b/src/sulguk/post_manager/exceptions.py index 2a0901e..f8dde13 100644 --- a/src/sulguk/post_manager/exceptions.py +++ b/src/sulguk/post_manager/exceptions.py @@ -2,9 +2,9 @@ class ManagerError(Exception): pass -class LinkedMessageNotFound(ManagerError): +class LinkedMessageNotFoundError(ManagerError): pass -class ChatNotFound(ManagerError): +class ChatNotFoundError(ManagerError): pass diff --git a/src/sulguk/post_manager/sender.py b/src/sulguk/post_manager/sender.py index 9530df5..252b85d 100644 --- a/src/sulguk/post_manager/sender.py +++ b/src/sulguk/post_manager/sender.py @@ -6,7 +6,7 @@ from aiogram.types import Chat, Message from .chat_info import get_chat -from .exceptions import LinkedMessageNotFound +from .exceptions import LinkedMessageNotFoundError from .file import load_file from .links import make_link, unparse_link from .params import SendArgs @@ -80,7 +80,7 @@ async def send(bot: Bot, args: SendArgs): linked_message = await get_linked_message[args.mode](bot, chat, message) if not linked_message: logger.error("Cannot load linked message to leave a comment") - raise LinkedMessageNotFound("No linked message found") + raise LinkedMessageNotFoundError("No linked message found") for file in args.file[1:]: data = load_file(file) comment = await bot.send_message(