mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Rewrite.
Add class 'Saver' that provides methods for saving (next step|reply) handlers. Add methods enable_save_next_step_handlers, enable_save_reply_handlers, disable_save_next_step_handlers, disable_save_reply_handlers, load_next_step_handlers, load_reply_handlers to Telebot and AsyncTelebot. update telebot/__init__.py
This commit is contained in:
parent
00c8dcc19b
commit
b1d5cb2129
@ -30,6 +30,84 @@ Module : telebot
|
||||
"""
|
||||
|
||||
|
||||
class Saver:
|
||||
def __init__(self, handlers, filename, delay):
|
||||
self.handlers = handlers
|
||||
self.filename = filename
|
||||
self.delay = delay
|
||||
self.timer = threading.Timer(delay, self.save_handlers)
|
||||
|
||||
def start_save_timer(self):
|
||||
if not self.timer.is_alive():
|
||||
self.timer = threading.Timer(self.delay, self.save_handlers)
|
||||
self.timer.start()
|
||||
|
||||
def save_handlers(self):
|
||||
self.dump_handlers(self.handlers, self.filename)
|
||||
|
||||
def load_handlers(self, filename):
|
||||
tmp = self.return_load_handlers(filename)
|
||||
if tmp is not None:
|
||||
self.handlers.update(tmp)
|
||||
|
||||
@staticmethod
|
||||
def dump_handlers(handlers, filename, file_mode="w"):
|
||||
dirs = filename.rsplit('/', maxsplit=1)[0]
|
||||
os.makedirs(dirs, exist_ok=True)
|
||||
to_dump = {}
|
||||
with open(filename + ".tmp", file_mode) as file:
|
||||
for id_, handlers_ in handlers.items():
|
||||
for handler in handlers_:
|
||||
name = handler['callback'].__name__
|
||||
module = handler['callback'].__module__
|
||||
|
||||
tmp = {"callback": {"module": module, "name": name}, "args": handler["args"],
|
||||
"kwargs": handler["kwargs"]}
|
||||
if id_ in to_dump.keys():
|
||||
to_dump[id_].append(tmp)
|
||||
else:
|
||||
to_dump[id_] = [tmp]
|
||||
|
||||
json.dump(to_dump, file)
|
||||
|
||||
if os.path.isfile(filename):
|
||||
os.remove(filename)
|
||||
|
||||
os.rename(filename + ".tmp", filename)
|
||||
|
||||
@staticmethod
|
||||
def return_load_handlers(filename):
|
||||
if os.path.isfile(filename) and os.path.getsize(filename) > 0:
|
||||
with open(filename, "r") as file:
|
||||
handlers = json.load(file)
|
||||
|
||||
result = {}
|
||||
for id_, handlers_ in handlers.items():
|
||||
for handler in handlers_:
|
||||
name = handler['callback']["name"]
|
||||
module = handler['callback']["module"]
|
||||
callback = getattr(sys.modules[module], name)
|
||||
|
||||
tmp = {"callback": callback, "args": handler["args"], "kwargs": handler["kwargs"]}
|
||||
|
||||
if int(id_) in result.keys():
|
||||
result[int(id_)].append(tmp)
|
||||
else:
|
||||
result[int(id_)] = [tmp]
|
||||
|
||||
a = """ for key, handlers_ in handlers.items():
|
||||
for handler in handlers_:
|
||||
name = handler["callback"]["name"]
|
||||
module = handler["callback"]["module"]
|
||||
|
||||
callback = getattr(sys.modules["__main__"], "next_")
|
||||
handler["callback"] = callback"""
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
class TeleBot:
|
||||
""" This is TeleBot Class
|
||||
Methods:
|
||||
@ -88,15 +166,8 @@ class TeleBot:
|
||||
# key: chat_id, value: handler list
|
||||
self.next_step_handlers = {}
|
||||
|
||||
self.save_reply_handlers = False
|
||||
self.reply_handlers_save_file = None
|
||||
self.reply_save_delay = None
|
||||
self.save_reply_timer = None
|
||||
|
||||
self.save_step_handlers = False
|
||||
self.step_handlers_save_file = None
|
||||
self.step_save_delay = None
|
||||
self.save_step_timer = None
|
||||
self.next_step_saver = None
|
||||
self.reply_saver = None
|
||||
|
||||
self.message_handlers = []
|
||||
self.edited_message_handlers = []
|
||||
@ -112,72 +183,23 @@ class TeleBot:
|
||||
if self.threaded:
|
||||
self.worker_pool = util.ThreadPool(num_threads=num_threads)
|
||||
|
||||
@staticmethod
|
||||
def dump_handlers(handlers, filename, file_mode="w"):
|
||||
dirs = filename.rsplit('/', maxsplit=1)[0]
|
||||
os.makedirs(dirs, exist_ok=True)
|
||||
with open(filename + ".tmp", file_mode) as file:
|
||||
for mid, handler in handlers.items():
|
||||
name = handler['callback'].__name__
|
||||
module = handler['callback'].__module__
|
||||
|
||||
tmp = {str(mid): {"callback": {"module": module, "name": name}, "args": handler["args"],
|
||||
"kwargs": handler["kwargs"]}}
|
||||
|
||||
json.dump(tmp, file)
|
||||
|
||||
if os.path.isfile(filename):
|
||||
os.remove(filename)
|
||||
|
||||
os.rename(filename + ".tmp", filename)
|
||||
|
||||
def save_next_step_handlers(self):
|
||||
self.dump_handlers(self.next_step_handlers, self.step_handlers_save_file)
|
||||
|
||||
def save_reply_handlers_method(self):
|
||||
self.dump_handlers(self.next_step_handlers, self.step_handlers_save_file)
|
||||
|
||||
def start_save_next_step_timer(self):
|
||||
if self.save_step_timer.is_alive(): return
|
||||
|
||||
self.save_step_timer.start()
|
||||
|
||||
def start_save_reply_timer(self):
|
||||
if self.save_reply_timer.is_alive(): return
|
||||
|
||||
self.save_reply_timer.start()
|
||||
|
||||
def enable_save_next_step_handlers(self, delay=120, filename="./handler-saves/step.save"):
|
||||
self.save_step_handlers = True
|
||||
self.step_handlers_save_file = filename
|
||||
self.step_save_delay = delay
|
||||
self.save_step_timer = threading.Timer(self.step_save_delay, self.save_next_step_handlers)
|
||||
self.next_step_saver = Saver(self.next_step_handlers, filename, delay)
|
||||
|
||||
def enable_save_reply_handlers(self, delay=120, filename="./handler-saves/reply.save"):
|
||||
self.save_reply_handlers = True
|
||||
self.reply_handlers_save_file = filename
|
||||
self.reply_save_delay = delay
|
||||
self.save_reply_timer = threading.Timer(self.reply_save_delay, self.save_reply_handlers_method)
|
||||
self.reply_saver = Saver(self.reply_handlers, filename, delay)
|
||||
|
||||
@staticmethod
|
||||
def load_handlers(filename):
|
||||
if os.path.isfile(filename) and os.path.getsize(filename) > 0:
|
||||
with open(filename, "r") as file:
|
||||
handlers = json.load(file)
|
||||
def disable_save_next_step_handlers(self):
|
||||
self.next_step_saver = None
|
||||
|
||||
for handler in handlers:
|
||||
name = handler["callback"]["name"]
|
||||
module = handler["callback"]["module"]
|
||||
|
||||
handler = getattr(sys.modules[module], name)
|
||||
|
||||
return handlers
|
||||
def disable_save_reply_handlers(self):
|
||||
self.reply_saver = None
|
||||
|
||||
def load_next_step_handlers(self, filename="./handler-saves/step.save"):
|
||||
self.next_step_handlers.update(self.load_handlers(filename))
|
||||
self.next_step_saver.load_handlers(filename)
|
||||
|
||||
def load_reply_handlers(self, filename="./handler-saves/reply.save"):
|
||||
self.reply_handlers.update(self.load_handlers(filename))
|
||||
self.reply_saver.load_handlers(filename)
|
||||
|
||||
def set_webhook(self, url=None, certificate=None, max_connections=None, allowed_updates=None):
|
||||
return apihelper.set_webhook(self.token, url, certificate, max_connections, allowed_updates)
|
||||
@ -1158,7 +1180,7 @@ class TeleBot:
|
||||
else:
|
||||
self.reply_handlers[message_id] = [{"callback": callback, "args": args, "kwargs": kwargs}]
|
||||
|
||||
self.start_save_reply_timer()
|
||||
self.reply_saver.start_save_timer()
|
||||
|
||||
def _notify_reply_handlers(self, new_messages):
|
||||
for message in new_messages:
|
||||
@ -1169,7 +1191,7 @@ class TeleBot:
|
||||
for handler in handlers:
|
||||
self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"])
|
||||
self.reply_handlers.pop(reply_msg_id)
|
||||
self.start_save_reply_timer()
|
||||
self.reply_saver.start_save_timer()
|
||||
|
||||
def register_next_step_handler(self, message, callback, *args, **kwargs):
|
||||
"""
|
||||
@ -1197,7 +1219,7 @@ class TeleBot:
|
||||
else:
|
||||
self.next_step_handlers[chat_id] = [{"callback": callback, "args": args, "kwargs": kwargs}]
|
||||
|
||||
self.start_save_next_step_timer()
|
||||
self.next_step_saver.start_save_timer()
|
||||
|
||||
def clear_step_handler(self, message):
|
||||
"""
|
||||
@ -1216,7 +1238,7 @@ class TeleBot:
|
||||
"""
|
||||
self.next_step_handlers[chat_id] = []
|
||||
|
||||
self.start_save_next_step_timer()
|
||||
self.next_step_saver.start_save_timer()
|
||||
|
||||
def clear_reply_handlers(self, message):
|
||||
"""
|
||||
@ -1235,7 +1257,7 @@ class TeleBot:
|
||||
"""
|
||||
self.reply_handlers[message_id] = []
|
||||
|
||||
self.start_save_next_step_timer()
|
||||
self.reply_saver.start_save_timer()
|
||||
|
||||
def _notify_next_handlers(self, new_messages):
|
||||
i = 0
|
||||
@ -1250,7 +1272,7 @@ class TeleBot:
|
||||
new_messages.pop(i) # removing message that detects with next_step_handler
|
||||
i += 1
|
||||
|
||||
self.start_save_next_step_timer()
|
||||
self.next_step_saver.start_save_timer()
|
||||
|
||||
@staticmethod
|
||||
def _build_handler_dict(handler, **filters):
|
||||
@ -1441,27 +1463,6 @@ class AsyncTeleBot(TeleBot):
|
||||
def __init__(self, *args, **kwargs):
|
||||
TeleBot.__init__(self, *args, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
@util.async()
|
||||
def dump_handlers(handlers, filename, file_mode="w"):
|
||||
return TeleBot.dump_handlers(handlers, filename, file_mode)
|
||||
|
||||
@util.async()
|
||||
def save_next_step_handlers(self):
|
||||
return TeleBot.save_next_step_handlers(self)
|
||||
|
||||
@util.async()
|
||||
def save_reply_handlers_method(self):
|
||||
return TeleBot.save_reply_handlers_method(self)
|
||||
|
||||
@util.async()
|
||||
def start_save_next_step_timer(self):
|
||||
return TeleBot.start_save_next_step_timer(self)
|
||||
|
||||
@util.async()
|
||||
def start_save_reply_timer(self):
|
||||
return TeleBot.start_save_reply_timer(self)
|
||||
|
||||
@util.async()
|
||||
def enable_save_next_step_handlers(self, delay=120, filename="./handler-saves/step.save"):
|
||||
return TeleBot.enable_save_next_step_handlers(self, delay, filename)
|
||||
@ -1470,10 +1471,13 @@ class AsyncTeleBot(TeleBot):
|
||||
def enable_save_reply_handlers(self, delay=120, filename="./handler-saves/reply.save"):
|
||||
return TeleBot.enable_save_reply_handlers(self, delay, filename)
|
||||
|
||||
@staticmethod
|
||||
@util.async()
|
||||
def load_handlers(filename):
|
||||
return TeleBot.load_handlers(filename)
|
||||
def disable_save_next_step_handlers(self):
|
||||
return TeleBot.disable_save_next_step_handlers(self)
|
||||
|
||||
@util.async()
|
||||
def disable_save_reply_handlers(self):
|
||||
return TeleBot.enable_save_reply_handlers(self)
|
||||
|
||||
@util.async()
|
||||
def load_next_step_handlers(self, filename="./handler-saves/step.save"):
|
||||
|
Loading…
Reference in New Issue
Block a user