From 4aaf9d9d1fe876ea58cff6fde2079fb67ceee0c9 Mon Sep 17 00:00:00 2001 From: Nameless Date: Thu, 16 Jul 2020 04:26:14 +0800 Subject: [PATCH] update readme (#142) * Add contacts shortcut and link to config file * Remove logging in utils * Remove unused imports and create script for checking formatting * Remove unused import in models * Fix check.sh script * Reformat check.sh * Update isort * Add check flag for black --- .github/workflows/main.yml | 26 ++++++-------------------- check.sh | 20 ++++++++++++++++++++ readme.md | 3 +++ tg/controllers.py | 1 + tg/models.py | 2 +- tg/tdlib.py | 10 +++++++--- tg/utils.py | 3 +-- tg/views.py | 9 +++++---- 8 files changed, 44 insertions(+), 30 deletions(-) create mode 100755 check.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 458ba6c..7e29652 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,28 +1,22 @@ -# This is a basic workflow to help you get started with Actions - name: CI -# Controls when the action will run. Triggers the workflow on push or pull request -# events but only for the master branch on: push: branches: [ master ] pull_request: branches: [ master ] -# A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: - # This workflow contains a single job called "build" + build: - # The type of runner that the job will run on + runs-on: ubuntu-latest strategy: matrix: python-version: [3.8] - # Steps represent a sequence of tasks that will be executed as part of the job steps: - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} @@ -32,16 +26,8 @@ jobs: - name: Install tools run: | - pip install black==19.10b0 mypy==0.770 isort==4.3.21 + pip install black==19.10b0 mypy==0.770 isort==5.1.0 flake8==3.8.3 - - name: Check formatting with black + - name: Check formatting and run linters run: | - black --check tg - - - name: Check sort formatting - run: | - isort -c tg/*.py - - - name: Check types with mypy - run: | - mypy tg --warn-redundant-casts --warn-unused-ignores --no-warn-no-return --warn-unreachable --strict-equality --ignore-missing-imports --warn-unused-configs --disallow-untyped-calls --disallow-untyped-defs --disallow-incomplete-defs --check-untyped-defs --disallow-untyped-decorators + sh check.sh diff --git a/check.sh b/check.sh new file mode 100755 index 0000000..57bb63f --- /dev/null +++ b/check.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +set -ex + +echo Checking and formatting with black... +black --check . + +echo Python type checking... +mypy tg --warn-redundant-casts --warn-unused-ignores \ + --no-warn-no-return --warn-unreachable --strict-equality \ + --ignore-missing-imports --warn-unused-configs \ + --disallow-untyped-calls --disallow-untyped-defs \ + --disallow-incomplete-defs --check-untyped-defs \ + --disallow-untyped-decorators + +echo Checking import sorting... +isort -c tg/*.py + +echo Checking unused imports... +flake8 --select=F401 diff --git a/readme.md b/readme.md index 6040e53..3b23d35 100644 --- a/readme.md +++ b/readme.md @@ -90,6 +90,8 @@ PHONE = "[your phone number]" ### Advanced configuration: +All configurable variables can be found [here](https://github.com/paul-nameless/tg/blob/master/tg/config.py) + ```python import os @@ -210,6 +212,7 @@ For navigation arrow keys also can be used. - `p`: pin/unpin current chat - `u`: mark read/unread - `r`: read current chat +- `c`: show list of contacts - `?`: show help ## Msgs: diff --git a/tg/controllers.py b/tg/controllers.py index 7ca446b..30e7b78 100644 --- a/tg/controllers.py +++ b/tg/controllers.py @@ -9,6 +9,7 @@ from tempfile import NamedTemporaryFile from typing import Any, Callable, Dict, List, Optional from telegram.utils import AsyncResult + from tg import config from tg.models import Model from tg.msg import MsgProxy diff --git a/tg/models.py b/tg/models.py index 5c575c1..2351cad 100644 --- a/tg/models.py +++ b/tg/models.py @@ -2,7 +2,7 @@ import logging import sys import time from collections import defaultdict -from typing import Any, Dict, Iterable, List, Optional, Set, Tuple +from typing import Any, Dict, List, Optional, Set, Tuple from tg.msg import MsgProxy from tg.tdlib import ChatAction, Tdlib, UserStatus diff --git a/tg/tdlib.py b/tg/tdlib.py index 4411ae7..44b4b52 100644 --- a/tg/tdlib.py +++ b/tg/tdlib.py @@ -27,9 +27,6 @@ class ChatType(Enum): channel = "channel" chatTypeSecret = "secret" - def is_group(self, chat_type: "Union[str, ChatType]") -> bool: - return chat_type in (self.chatTypeSupergroup, self.chatTypeBasicGroup) - class UserStatus(Enum): userStatusEmpty = "" @@ -276,3 +273,10 @@ def get_chat_type(chat: Dict[str, Any]) -> Optional[ChatType]: except KeyError: pass return None + + +def is_group(chat_type: Union[str, ChatType]) -> bool: + return chat_type in ( + ChatType.chatTypeSupergroup, + ChatType.chatTypeBasicGroup, + ) diff --git a/tg/utils.py b/tg/utils.py index f58bec0..1364edc 100644 --- a/tg/utils.py +++ b/tg/utils.py @@ -19,7 +19,7 @@ from subprocess import CompletedProcess from types import TracebackType from typing import Any, Optional, Tuple, Type -from tg import colors, config +from tg import config log = logging.getLogger(__name__) emoji_pattern = re.compile( @@ -240,7 +240,6 @@ def pretty_ts(ts: int) -> str: diff = now - datetime.utcfromtimestamp(ts) second_diff = diff.seconds day_diff = diff.days - log.info("diff:: %s, %s, %s", ts, second_diff, day_diff) if day_diff < 0: return "" diff --git a/tg/views.py b/tg/views.py index 600f7c7..1cbbb25 100644 --- a/tg/views.py +++ b/tg/views.py @@ -1,14 +1,15 @@ import curses import logging -from _curses import window # type: ignore from datetime import datetime from typing import Any, Dict, List, Optional, Tuple, Union, cast +from _curses import window # type: ignore + from tg import config from tg.colors import bold, cyan, get_color, magenta, reverse, white, yellow from tg.models import Model, UserModel from tg.msg import MsgProxy -from tg.tdlib import ChatType, get_chat_type +from tg.tdlib import ChatType, get_chat_type, is_group from tg.utils import emoji_pattern, get_color_by_str, num, truncate_to_len log = logging.getLogger(__name__) @@ -215,7 +216,7 @@ class ChatView: if user: last_msg_sender = get_user_label(self.model.users, user) chat_type = get_chat_type(chat) - if chat_type and chat_type.is_group(chat_type): + if chat_type and is_group(chat_type): return last_msg_sender, last_msg return None, last_msg @@ -629,7 +630,7 @@ def _get_action_label(users: UserModel, chat: Dict[str, Any]) -> Optional[str]: if actioner and action: label = f"{action}..." chat_type = get_chat_type(chat) - if chat_type and chat_type.is_group(chat_type): + if chat_type and is_group(chat_type): user_label = get_user_label(users, actioner) label = f"{user_label} {label}"