make it prettier

This commit is contained in:
Alexander Zveruk 2020-06-27 23:55:53 +03:00
parent 55833054a9
commit f451473b39
3 changed files with 23 additions and 32 deletions

View file

@ -2,7 +2,6 @@ import curses
import logging
import os
import shlex
import threading
from datetime import datetime
from functools import partial, wraps
from queue import Queue
@ -19,7 +18,6 @@ from tg.utils import (
get_duration,
get_video_resolution,
get_waveform,
handle_exception,
is_yes,
notify,
suspend,
@ -53,7 +51,7 @@ def bind(
return fun(*args, **kwargs)
@wraps(fun)
def _no_repeat_factor(self: Controller, _: bool) -> Any:
def _no_repeat_factor(self: "Controller", _: bool) -> Any:
return fun(self)
for key in keys:
@ -298,11 +296,14 @@ class Controller:
self.send_file(self.tg.send_audio)
def send_file(
self, send_file_fun: Callable[[str, int], AsyncResult], *args: Any, **kwargs: Any
self,
send_file_fun: Callable[[str, int], AsyncResult],
) -> None:
file_path = self.view.status.get_input()
if file_path and os.path.isfile(file_path):
if chat_id := self.model.chats.id_by_index(self.model.current_chat):
if chat_id := self.model.chats.id_by_index(
self.model.current_chat
):
send_file_fun(file_path, chat_id)
self.present_info("File sent")
@ -597,7 +598,7 @@ class Controller:
if text := msg.text_content if msg.is_text else msg.content_type:
notify(text, title=name)
def _refresh_current_chat(self, current_chat_id: Optional[int]) -> None:
def refresh_current_chat(self, current_chat_id: Optional[int]) -> None:
if current_chat_id is None:
return
# TODO: we can create <index> for chats, it's faster than sqlite anyway

View file

@ -15,8 +15,10 @@ handlers: Dict[str, UpdateHandler] = {}
max_download_size: int = utils.parse_size(config.MAX_DOWNLOAD_SIZE)
def update_handler(update_type: str) -> Callable:
def decorator(fun: Callable) -> Callable:
def update_handler(
update_type: str,
) -> Callable[[UpdateHandler], UpdateHandler]:
def decorator(fun: UpdateHandler) -> UpdateHandler:
global handlers
assert (
update_type not in handlers
@ -25,9 +27,9 @@ def update_handler(update_type: str) -> Callable:
handlers[update_type] = fun
@wraps(fun)
def wrapper(*args: Any, **kwargs: Any) -> Any:
def wrapper(controller: Controller, update: Dict[str, Any]) -> None:
try:
return fun(*args, **kwargs)
return fun(controller, update)
except Exception:
log.exception("Error happened in %s handler", fun.__name__)
@ -86,7 +88,7 @@ def update_chat_order(controller: Controller, update: Dict[str, Any]) -> None:
order = update["order"]
if controller.model.chats.update_chat(chat_id, order=order):
controller._refresh_current_chat(current_chat_id)
controller.refresh_current_chat(current_chat_id)
@update_handler("updateChatTitle")
@ -97,7 +99,7 @@ def update_chat_title(controller: Controller, update: Dict[str, Any]) -> None:
current_chat_id = controller.model.current_chat_id
if controller.model.chats.update_chat(chat_id, title=title):
controller._refresh_current_chat(current_chat_id)
controller.refresh_current_chat(current_chat_id)
@update_handler("updateChatIsMarkedAsUnread")
@ -112,7 +114,7 @@ def update_chat_is_marked_as_unread(
if controller.model.chats.update_chat(
chat_id, is_marked_as_unread=is_marked_as_unread
):
controller._refresh_current_chat(current_chat_id)
controller.refresh_current_chat(current_chat_id)
@update_handler("updateChatIsPinned")
@ -128,7 +130,7 @@ def update_chat_is_pinned(
if controller.model.chats.update_chat(
chat_id, is_pinned=is_pinned, order=order
):
controller._refresh_current_chat(current_chat_id)
controller.refresh_current_chat(current_chat_id)
@update_handler("updateChatReadOutbox")
@ -143,7 +145,7 @@ def update_chat_read_outbox(
if controller.model.chats.update_chat(
chat_id, last_read_outbox_message_id=last_read_outbox_message_id
):
controller._refresh_current_chat(current_chat_id)
controller.refresh_current_chat(current_chat_id)
@update_handler("updateChatReadInbox")
@ -161,7 +163,7 @@ def update_chat_read_inbox(
last_read_inbox_message_id=last_read_inbox_message_id,
unread_count=unread_count,
):
controller._refresh_current_chat(current_chat_id)
controller.refresh_current_chat(current_chat_id)
@update_handler("updateChatDraftMessage")
@ -176,7 +178,7 @@ def update_chat_draft_message(
current_chat_id = controller.model.current_chat_id
if controller.model.chats.update_chat(chat_id, order=order):
controller._refresh_current_chat(current_chat_id)
controller.refresh_current_chat(current_chat_id)
@update_handler("updateChatLastMessage")
@ -196,7 +198,7 @@ def update_chat_last_message(
if controller.model.chats.update_chat(
chat_id, last_message=last_message, order=order
):
controller._refresh_current_chat(current_chat_id)
controller.refresh_current_chat(current_chat_id)
@update_handler("updateChatNotificationSettings")

View file

@ -12,10 +12,9 @@ import struct
import subprocess
import sys
from datetime import datetime
from functools import wraps
from logging.handlers import RotatingFileHandler
from types import TracebackType
from typing import Any, Callable, Optional, TextIO, Tuple, Type
from typing import Any, Optional, Tuple, Type
from tg import config
@ -173,18 +172,7 @@ def notify(
os.system(notify_cmd)
def handle_exception(fun: Callable) -> Callable:
@wraps(fun)
def wrapper(*args: Any, **kwargs: Any) -> Any:
try:
return fun(*args, **kwargs)
except Exception:
log.exception("Error happened in %s handler", fun.__name__)
return wrapper
def truncate_to_len(s: str, target_len: int, encoding: str = "utf-8") -> str:
def truncate_to_len(s: str, target_len: int) -> str:
target_len -= sum(map(bool, map(emoji_pattern.findall, s[:target_len])))
return s[: max(1, target_len - 1)]