updated json object hook to convert string keys back to ints

Signed-off-by: Konrad <klux2@hs-mittweida.de>
This commit is contained in:
Konrad 2019-09-27 20:03:00 +02:00
parent 4bf7266951
commit 0bb014efec

View file

@ -373,11 +373,16 @@ def get_now() -> str:
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def as_enum(d): def process_json(d: dict) -> Any:
if "Language" in d: if "Language" in d:
lang = d["Language"] lang = d["Language"]
return getattr(Language, lang) return getattr(Language, lang)
else: else:
if isinstance(d, dict):
try:
return {int(k): v for k, v in d.items()}
except ValueError:
return d
return d return d
@ -393,7 +398,7 @@ translator = Translator()
try: try:
with open(config_filename, 'r') as config_file: with open(config_filename, 'r') as config_file:
config = json.load(config_file, object_hook=as_enum) config = json.load(config_file, object_hook=process_json)
except FileNotFoundError as e: except FileNotFoundError as e:
print('Error: config file "{}" not found: {}'.format(config_filename, e)) print('Error: config file "{}" not found: {}'.format(config_filename, e))
sys.exit(-1) sys.exit(-1)