From ed7e33b4c65a6a0d73edb56861cd7b742aae289d Mon Sep 17 00:00:00 2001 From: Waffle Date: Fri, 25 May 2018 02:48:46 +0300 Subject: [PATCH 1/8] Fix loadings funcs --- telebot/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 9b33dbc..a877b69 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -113,7 +113,7 @@ class TeleBot: self.worker_pool = util.ThreadPool(num_threads=num_threads) @staticmethod - def dump_handlers(handlers, filename="./handler-saves/step.save", file_mode="w"): + 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: @@ -165,6 +165,12 @@ class TeleBot: with open(filename, "r") as file: handlers = json.load(file) + for handler in handlers: + name = handler["callback"]["name"] + module = handler["callback"]["module"] + + handler = getattr(sys.modules[module], name) + return handlers def load_next_step_handlers(self, filename="./handler-saves/step.save"): From 00c8dcc19b966d842a4e6437e7460832820492ac Mon Sep 17 00:00:00 2001 From: Waffle Date: Fri, 25 May 2018 03:10:40 +0300 Subject: [PATCH 2/8] Add async methods --- telebot/__init__.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/telebot/__init__.py b/telebot/__init__.py index a877b69..541a844 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -1441,6 +1441,48 @@ 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) + + @util.async() + 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) + + @util.async() + def load_next_step_handlers(self, filename="./handler-saves/step.save"): + return TeleBot.load_next_step_handlers(self, filename) + + @util.async() + def load_reply_handlers(self, filename="./handler-saves/reply.save"): + return TeleBot.load_reply_handlers(self, filename) + @util.async() def get_me(self): return TeleBot.get_me(self) From b1d5cb2129d8b44838ab8ce3984e6320b71157ad Mon Sep 17 00:00:00 2001 From: Waffle Date: Fri, 25 May 2018 08:56:31 +0300 Subject: [PATCH 3/8] 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 --- telebot/__init__.py | 196 ++++++++++++++++++++++---------------------- 1 file changed, 100 insertions(+), 96 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 541a844..73ab328 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -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"): From 4bcfc34a50f2096d1729711e71c0eba2aabb7734 Mon Sep 17 00:00:00 2001 From: Waffle Date: Fri, 25 May 2018 09:07:59 +0300 Subject: [PATCH 4/8] Update _notify_next_handlers and _notify_reply_handlers methods: Now if there wasn't any handler updates, timer willn't start. --- telebot/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 73ab328..f08ebb4 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -1191,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.reply_saver.start_save_timer() + self.reply_saver.start_save_timer() def register_next_step_handler(self, message, callback, *args, **kwargs): """ @@ -1270,9 +1270,9 @@ class TeleBot: self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"]) self.next_step_handlers.pop(chat_id, None) new_messages.pop(i) # removing message that detects with next_step_handler + self.next_step_saver.start_save_timer() i += 1 - self.next_step_saver.start_save_timer() @staticmethod def _build_handler_dict(handler, **filters): From 4facc5f7d736e42e52e5bb532e1cbad028205004 Mon Sep 17 00:00:00 2001 From: Waffle Date: Fri, 25 May 2018 09:30:10 +0300 Subject: [PATCH 5/8] fix unenabled saving handlers. Updated telebot/__init__.py --- telebot/__init__.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index f08ebb4..ab87189 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -1179,8 +1179,8 @@ class TeleBot: self.reply_handlers[message_id].append({"callback": callback, "args": args, "kwargs": kwargs}) else: self.reply_handlers[message_id] = [{"callback": callback, "args": args, "kwargs": kwargs}] - - self.reply_saver.start_save_timer() + if self.reply_saver is not None: + self.reply_saver.start_save_timer() def _notify_reply_handlers(self, new_messages): for message in new_messages: @@ -1191,7 +1191,8 @@ class TeleBot: for handler in handlers: self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"]) self.reply_handlers.pop(reply_msg_id) - self.reply_saver.start_save_timer() + if self.reply_saver is not None: + self.reply_saver.start_save_timer() def register_next_step_handler(self, message, callback, *args, **kwargs): """ @@ -1219,7 +1220,8 @@ class TeleBot: else: self.next_step_handlers[chat_id] = [{"callback": callback, "args": args, "kwargs": kwargs}] - self.next_step_saver.start_save_timer() + if self.next_step_saver is not None: + self.next_step_saver.start_save_timer() def clear_step_handler(self, message): """ @@ -1238,7 +1240,8 @@ class TeleBot: """ self.next_step_handlers[chat_id] = [] - self.next_step_saver.start_save_timer() + if self.next_step_saver is not None: + self.next_step_saver.start_save_timer() def clear_reply_handlers(self, message): """ @@ -1257,7 +1260,8 @@ class TeleBot: """ self.reply_handlers[message_id] = [] - self.reply_saver.start_save_timer() + if self.reply_saver is not None: + self.reply_saver.start_save_timer() def _notify_next_handlers(self, new_messages): i = 0 @@ -1270,7 +1274,8 @@ class TeleBot: self._exec_task(handler["callback"], message, *handler["args"], **handler["kwargs"]) self.next_step_handlers.pop(chat_id, None) new_messages.pop(i) # removing message that detects with next_step_handler - self.next_step_saver.start_save_timer() + if self.next_step_saver is not None: + self.next_step_saver.start_save_timer() i += 1 From 7df6b3d4c98167cbed95bf6cfe2cdff88481dd9f Mon Sep 17 00:00:00 2001 From: Waffle Date: Fri, 25 May 2018 09:35:39 +0300 Subject: [PATCH 6/8] Fix situation where delay <= 0. Update telebot/__init__.py --- telebot/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index ab87189..59ae6ff 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -39,8 +39,11 @@ class Saver: def start_save_timer(self): if not self.timer.is_alive(): - self.timer = threading.Timer(self.delay, self.save_handlers) - self.timer.start() + if self.delay <= 0: + self.save_handlers() + else: + self.timer = threading.Timer(self.delay, self.save_handlers) + self.timer.start() def save_handlers(self): self.dump_handlers(self.handlers, self.filename) From 018e4597a2d6b6e777c19d475b90186fa1bc07f2 Mon Sep 17 00:00:00 2001 From: Waffle Date: Fri, 25 May 2018 09:40:29 +0300 Subject: [PATCH 7/8] Add del_file_after_loading param to Saver.return_load_handlers and Saver.load_handlers methods. Update telebot/__init__.py --- telebot/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 59ae6ff..f89c339 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -48,8 +48,8 @@ class Saver: def save_handlers(self): self.dump_handlers(self.handlers, self.filename) - def load_handlers(self, filename): - tmp = self.return_load_handlers(filename) + def load_handlers(self, filename, del_file_after_loading=True): + tmp = self.return_load_handlers(filename, del_file_after_loading=del_file_after_loading) if tmp is not None: self.handlers.update(tmp) @@ -79,7 +79,7 @@ class Saver: os.rename(filename + ".tmp", filename) @staticmethod - def return_load_handlers(filename): + def return_load_handlers(filename, del_file_after_loading=True): if os.path.isfile(filename) and os.path.getsize(filename) > 0: with open(filename, "r") as file: handlers = json.load(file) @@ -105,7 +105,8 @@ class Saver: callback = getattr(sys.modules["__main__"], "next_") handler["callback"] = callback""" - + if del_file_after_loading: + os.remove(filename) return result From 7e5f51e4ab6436c8f989b04ad7d16d5f2e175c77 Mon Sep 17 00:00:00 2001 From: Waffle Date: Fri, 25 May 2018 09:44:43 +0300 Subject: [PATCH 8/8] Remove old thing. Update telebot/__init__.py --- telebot/__init__.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index f89c339..06bbe30 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -98,13 +98,6 @@ class Saver: 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""" if del_file_after_loading: os.remove(filename) return result