1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Move from json to pickle.

Update: relebot/__init__.py
This commit is contained in:
Waffle 2018-05-26 12:19:01 +03:00
parent b989b7601b
commit 17971ff48b

View File

@ -9,6 +9,7 @@ import six
import os import os
import json import json
import pickle
import logging import logging
@ -31,7 +32,7 @@ Module : telebot
class Handler: class Handler:
def __init__(self, callback: {dict, function}, *args, **kwargs): def __init__(self, callback: {dict, 'function'}, *args, **kwargs):
if type(callback) == dict: if type(callback) == dict:
self.callback = getattr(sys.modules[callback["module"]], callback["name"]) self.callback = getattr(sys.modules[callback["module"]], callback["name"])
else: else:
@ -73,7 +74,7 @@ class Saver:
self.handlers.update(tmp) self.handlers.update(tmp)
@staticmethod @staticmethod
def dump_handlers(handlers, filename, file_mode="w"): def dump_handlers(handlers, filename, file_mode="wb"):
dirs = filename.rsplit('/', maxsplit=1)[0] dirs = filename.rsplit('/', maxsplit=1)[0]
os.makedirs(dirs, exist_ok=True) os.makedirs(dirs, exist_ok=True)
to_dump = {} to_dump = {}
@ -85,7 +86,8 @@ class Saver:
else: else:
to_dump[id_] = [handler.copy_to_dump()] to_dump[id_] = [handler.copy_to_dump()]
json.dump(to_dump, file) # json.dump(to_dump, file)
pickle.dump(to_dump, file)
if os.path.isfile(filename): if os.path.isfile(filename):
os.remove(filename) os.remove(filename)
@ -95,14 +97,14 @@ class Saver:
@staticmethod @staticmethod
def return_load_handlers(filename, del_file_after_loading=True): def return_load_handlers(filename, del_file_after_loading=True):
if os.path.isfile(filename) and os.path.getsize(filename) > 0: if os.path.isfile(filename) and os.path.getsize(filename) > 0:
with open(filename, "r") as file: with open(filename, "rb") as file:
handlers = json.load(file) # handlers = json.load(file)
handlers = pickle.load(file)
result = {} result = {}
for id_, handlers_ in handlers.items(): for id_, handlers_ in handlers.items():
for handler in handlers_: for handler in handlers_:
tmp = Handler(handler['callback'], handler["args"], handler["kwargs"]) tmp = Handler(handler['callback'], *handler["args"], **handler["kwargs"])
if int(id_) in result.keys(): if int(id_) in result.keys():
result[int(id_)].append(tmp) result[int(id_)].append(tmp)