#!/opt/homebrew/bin/python3 # -*- coding:utf-8 -*- import json import os import sys from shutil import copyfile class Version(): def __init__(self): super().__init__() if not os.path.exists("version.json"): try: copyfile("default_version.json", "version.json") except IOError as e: print("Unable to copy file. %s" % e) except: print("Unexpected error:", sys.exc_info()) self.data = dict() self.path = os.path.join(os.path.dirname(__file__), 'version.json') self.load() def load(self): with open(self.path, 'r',encoding='utf-8') as file: self.data = json.load(file) def reload(self): self.load() def update(self, content): if isinstance(content, str): json_dict = json.loads(content) with open(self.path, 'w', encoding='utf-8') as file: file.write(json.dumps(json_dict, indent=4,ensure_ascii=False)) elif isinstance(content, dict): with open(self.path, 'w', encoding='utf-8') as file: file.write(json.dumps(content, indent=4,ensure_ascii=False)) self.reload() return True def toDict(self): return self.data def toJson(self): return json.dumps(self.data, indent=4) app_version = Version()