Skip to content

Commit

Permalink
Add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-edward committed Aug 24, 2023
1 parent 5a0e323 commit 5b8f60d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

38 changes: 36 additions & 2 deletions telegram_notifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@


class EmptyMessageError(Exception):
pass
"""
An error reported when attempting to send an empty message.
"""


class InvalidConfigError(Exception):
pass
"""
An error reported when config values are not fully satisfied.
"""


def get_config() -> ConfigParser:
"""
Gets config options from local installation.
"""

config = ConfigParser()
config.read(CONFIG_PATH)
return config


def send_message(message: str, no_escape: bool = False, **kwargs) -> Optional[Response]:
"""
Sends a message to a Telegram chat. If `no_escape` is True, does not attempt to escape
special characters.
"""

if not validate_config(get_config()):
raise InvalidConfigError("Required config options not defined.")
config = get_config()
Expand All @@ -40,12 +53,20 @@ def send_message(message: str, no_escape: bool = False, **kwargs) -> Optional[Re


def escape_specials(to_escape: str) -> str:
"""
Escapes characters special to the Telegram chat.
"""

return to_escape.replace(".", "\\.").replace("-", "\\-")


def set_config_options(
chat_id: Optional[Union[str, int]] = None, token: Optional[str] = None
) -> None:
"""
Sets config options local to the telegram-notifier installation.
"""

config = get_config()
if chat_id is not None:
config["DEFAULT"]["chat_id"] = str(chat_id)
Expand All @@ -56,6 +77,10 @@ def set_config_options(


def validate_config(config: ConfigParser) -> bool:
"""
Returns True if local config has sufficient entries to send messages.
"""

if config.has_option("DEFAULT", "chat_id") and config.has_option(
"DEFAULT", "token"
):
Expand All @@ -67,13 +92,22 @@ def validate_config(config: ConfigParser) -> bool:


def process_response(response: Response) -> str:
"""
Translates a Response to a human-readable description.
"""

if response.ok:
return "telegram_notifier: Notification sent"
else:
return f"telegram_notifier: Error {response.text}"


class Notifier:
"""
Functions as a context manager, with default behavior of sending a message to a Telegram chat on entrance/exit of a
block of code.
"""

def __init__(
self,
description: str = "",
Expand Down

0 comments on commit 5b8f60d

Please sign in to comment.