made it possible to en-/decode enum values to/from json

This commit is contained in:
Konrad 2019-09-27 18:48:01 +02:00
parent 5c548e0aab
commit 99de5d26c0

View file

@ -58,6 +58,13 @@ datum = None
botID = -1
class EnumEncode(json.JSONEncoder):
def default(self, obj):
if type(obj) is Language:
return {"Language": str(obj.name)}
return json.JSONEncoder.default(self, obj)
class HSMensaW(telepot.aio.helper.ChatHandler):
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[InlineKeyboardButton(text="Deutsch", callback_data=Language.GERMAN.value)],
@ -228,7 +235,7 @@ async def send_message(bot_obj: DelegatorBot, chat_id: int, msg: str, parse_mode
def write_config() -> None:
with open(config_filename, 'w') as outfile:
json.dump(config, outfile)
json.dump(config, outfile, cls=EnumEncode)
async def get_essen(only_today: bool) -> None:
@ -360,6 +367,14 @@ def get_now() -> str:
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def as_enum(d):
if "Language" in d:
lang = d["Language"]
return getattr(Language, lang)
else:
return d
sys.stdout = open("out.log", "a")
sys.stderr = open("err.log", "a")
message_log = open("msg.log", "a")
@ -372,7 +387,7 @@ translator = Translator()
try:
with open(config_filename, 'r') as config_file:
config = json.load(config_file)
config = json.load(config_file, object_hook=as_enum)
except FileNotFoundError as e:
print('Error: config file "{}" not found: {}'.format(config_filename, e))
sys.exit(-1)