wip - still not working

This commit is contained in:
Dobromir Popov
2024-10-22 14:10:42 +03:00
parent 86b3a086b9
commit 1a31455d98
3 changed files with 133 additions and 101 deletions

View File

@ -10,6 +10,8 @@ from telegram.constants import ParseMode
from config import TELEGRAM_BOT_TOKEN, DEVELOPER_CHAT_ID, BOT_NAME
import asyncio
from typing import Callable, Any
import time
import logging
from logging.handlers import RotatingFileHandler
@ -67,7 +69,7 @@ class Log:
# Set up success logger for accounting CSV
def __init__(self):
logger = logging.getLogger(__name__)
self.logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
#logging.basicConfig(level=logging.INFO)
@ -109,11 +111,34 @@ def safe_get_property(info, property_name, default='Unknown'):
value = info.get(property_name, default)
return str(value) if value is not None else str(default)
async def async_safe_call(func: Callable, *args: Any, **kwargs: Any) -> Any:
"""
Safely call a function that might be synchronous, asynchronous, or a coroutine object.
:param func: The function to call, or a coroutine object
:param args: Positional arguments to pass to the function
:param kwargs: Keyword arguments to pass to the function
:return: The result of the function call, or None if func is not callable or a coroutine
"""
if func is None:
return None
if callable(func):
if asyncio.iscoroutinefunction(func):
return await func(*args, **kwargs)
else:
return func(*args, **kwargs)
elif asyncio.iscoroutine(func):
# If func is already a coroutine object, just await it
return await func
else:
logging.warning(f"Expected a callable or coroutine, but got {type(func)}: {func}")
return None
# Create a global instance of TelegramUtils
telegram_utils = TelegramUtils()
log = Log()
log = Log().logger
# You can add more Telegram-related methods to the TelegramUtils class if needed