Skip to content

Commit

Permalink
move code into module, move small functions to utils and annotate types
Browse files Browse the repository at this point in the history
  • Loading branch information
rahiel committed Apr 8, 2022
1 parent 3422f49 commit 255ccb9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[bdist_wheel]
universal = 1

[flake8]
max-line-length = 120
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
python_requires=">=3.5",
py_modules=["telegram_send", "version"],
install_requires=["python-telegram-bot>=12.1.1", "colorama", "appdirs"],
entry_points={"console_scripts": ["telegram-send=telegram_send:main"]},
entry_points={"console_scripts": ["telegram-send=telegram_send.telegram_send:main"]},

author="Rahiel Kasim",
author_email="[email protected]",
Expand Down
Empty file added telegram_send/__init__.py
Empty file.
29 changes: 1 addition & 28 deletions telegram_send.py → telegram_send/telegram_send.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import colorama
import telegram
from telegram.constants import MAX_MESSAGE_LENGTH
from appdirs import AppDirs

from version import __version__
from utils import pre, split_message, get_config_path, markup

try:
import readline
Expand Down Expand Up @@ -493,32 +493,5 @@ class ConfigError(Exception):
pass


def markup(text, style):
ansi_codes = {"bold": "\033[1m", "red": "\033[31m", "green": "\033[32m",
"cyan": "\033[36m", "magenta": "\033[35m"}
return ansi_codes[style] + text + "\033[0m"


def pre(text):
if "```" in text:
print(markup("Sending a message containing ``` is not supported with --pre.", "red"))
sys.exit(1)
return "```text\n" + text + "```"


def get_config_path():
return AppDirs("telegram-send").user_config_dir + ".conf"


def split_message(message, max_length):
"""Split large message into smaller messages each smaller than the max_length."""
ms = []
while len(message) > max_length:
ms.append(message[:max_length])
message = message[max_length:]
ms.append(message)
return ms


if __name__ == "__main__":
main()
28 changes: 28 additions & 0 deletions telegram_send/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List

from appdirs import AppDirs


def markup(text: str, style: str) -> str:
ansi_codes = {"bold": "\033[1m", "red": "\033[31m", "green": "\033[32m",
"cyan": "\033[36m", "magenta": "\033[35m"}
return ansi_codes[style] + text + "\033[0m"

def pre(text: str) -> str:
if "```" in text:
print(markup("Sending a message containing ``` is not supported with --pre.", "red"))
sys.exit(1)
return "```text\n" + text + "```"

def split_message(message: str, max_length: int) -> List[str]:
"""Split large message into smaller messages each smaller than the max_length."""
ms = []
while len(message) > max_length:
ms.append(message[:max_length])
message = message[max_length:]
ms.append(message)
return ms

def get_config_path():
return AppDirs("telegram-send").user_config_dir + ".conf"

0 comments on commit 255ccb9

Please sign in to comment.