Added colorful logs, file restarts on changes to sync

This commit is contained in:
coder2020official 2022-08-30 21:25:41 +04:00
parent e0ffe0b4f5
commit 0f7ab0d05f
1 changed files with 43 additions and 4 deletions

View File

@ -131,6 +131,9 @@ class TeleBot:
:param use_class_middlewares: Use class middlewares, defaults to False
:type use_class_middlewares: :obj:`bool`, optional
:param colorful_logs: Outputs colorful logs
:type colorful_logs: :obj:`bool`, optional
"""
def __init__(
@ -139,7 +142,8 @@ class TeleBot:
next_step_backend: Optional[HandlerBackend]=None, reply_backend: Optional[HandlerBackend]=None,
exception_handler: Optional[ExceptionHandler]=None, last_update_id: Optional[int]=0,
suppress_middleware_excepions: Optional[bool]=False, state_storage: Optional[StateStorageBase]=StateMemoryStorage(),
use_class_middlewares: Optional[bool]=False
use_class_middlewares: Optional[bool]=False,
colorful_logs: Optional[bool]=False
):
self.token = token
self.parse_mode = parse_mode
@ -147,6 +151,16 @@ class TeleBot:
self.skip_pending = skip_pending
self.suppress_middleware_excepions = suppress_middleware_excepions
if colorful_logs:
try:
import coloredlogs
coloredlogs.install(logger=logger, level=logger.level)
except ImportError:
raise ImportError(
'Install colorredlogs module to use colorful_logs option.'
)
self.__stop_polling = threading.Event()
self.last_update_id = last_update_id
self.exc_info = None
@ -838,7 +852,8 @@ class TeleBot:
def infinity_polling(self, timeout: Optional[int]=20, skip_pending: Optional[bool]=False, long_polling_timeout: Optional[int]=20,
logger_level: Optional[int]=logging.ERROR, allowed_updates: Optional[List[str]]=None, *args, **kwargs):
logger_level: Optional[int]=logging.ERROR, allowed_updates: Optional[List[str]]=None,
restart_on_change: Optional[bool]=False, *args, **kwargs):
"""
Wrap polling with infinite loop and exception handling to avoid bot stops polling.
@ -864,6 +879,9 @@ class TeleBot:
so unwanted updates may be received for a short period of time.
:type allowed_updates: :obj:`list` of :obj:`str`
:param restart_on_change: Restart a file on file(s) change. Defaults to False
:type restart_on_change: :obj:`bool`
:return:
"""
if skip_pending:
@ -872,7 +890,8 @@ class TeleBot:
while not self.__stop_polling.is_set():
try:
self.polling(non_stop=True, timeout=timeout, long_polling_timeout=long_polling_timeout,
logger_level=logger_level, allowed_updates=allowed_updates, *args, **kwargs)
logger_level=logger_level, allowed_updates=allowed_updates, restart_on_change=restart_on_change,
*args, **kwargs)
except Exception as e:
if logger_level and logger_level >= logging.ERROR:
logger.error("Infinity polling exception: %s", str(e))
@ -889,7 +908,7 @@ class TeleBot:
def polling(self, non_stop: Optional[bool]=False, skip_pending: Optional[bool]=False, interval: Optional[int]=0,
timeout: Optional[int]=20, long_polling_timeout: Optional[int]=20,
logger_level: Optional[int]=logging.ERROR, allowed_updates: Optional[List[str]]=None,
none_stop: Optional[bool]=None):
none_stop: Optional[bool]=None, restart_on_change: Optional[bool]=False):
"""
This function creates a new Thread that calls an internal __retrieve_updates function.
This allows the bot to retrieve Updates automatically and notify listeners and message handlers accordingly.
@ -932,6 +951,9 @@ class TeleBot:
:param none_stop: Deprecated, use non_stop. Old typo, kept for backward compatibility.
:type none_stop: :obj:`bool`
:param restart_on_change: Restart a file on file(s) change. Defaults to False
:type restart_on_change: :obj:`bool`
:return:
"""
@ -941,6 +963,23 @@ class TeleBot:
if skip_pending:
self.__skip_updates()
if restart_on_change:
try:
from watchdog.observers import Observer
from telebot.ext.reloader import EventHandler
except ImportError:
raise ImportError(
'Please install watchdog and psutil before using restart_on_change option.'
)
event_handler = EventHandler()
path = sys.argv[1] if len(sys.argv) > 1 else '.'
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
logger.info('Starting your bot with username: [@%s]', self.user.username)
if self.threaded:
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,