|
| 1 | +import json |
| 2 | +from json import JSONDecodeError |
| 3 | + |
| 4 | +from resources.runtime import savestate |
| 5 | +from resources.runtime.Settings.logfunctions import logWrite |
| 6 | + |
| 7 | + |
| 8 | +def writeToAutosave(name, write): |
| 9 | + templist = savestate.autosave_standard |
| 10 | + try: |
| 11 | + templist = readAutosave() |
| 12 | + # print("Got ", templist) |
| 13 | + except JSONDecodeError: |
| 14 | + logWrite("Saving to autosave failed because the json could not be decoded!") |
| 15 | + print("There was a problem decoding the JSON. Please resort to a backup or try to fix it manually!") |
| 16 | + |
| 17 | + autosave = open(savestate.standardFilePath + savestate.symbol + "autosave.json", "w+", encoding="utf-8") |
| 18 | + |
| 19 | + templist[name] = write |
| 20 | + # print("Data saved without key!") |
| 21 | + |
| 22 | + autosave.write(json.dumps(templist, indent=4)) |
| 23 | + autosave.close() |
| 24 | + |
| 25 | + |
| 26 | +def readAutosave(*filterkey): |
| 27 | + """ |
| 28 | + This method is globally used to get data from the autosave |
| 29 | +
|
| 30 | + :type filterkey: str |
| 31 | + :return: dict |
| 32 | + """ |
| 33 | + if savestate.AUTOSAVE_LOG_LEVEL_OUTPUT == 4: |
| 34 | + logWrite("Parsing autosave...") |
| 35 | + savestate.AUTOSAVE_LOG_LEVEL_OUTPUT -= 1 |
| 36 | + else: |
| 37 | + if savestate.AUTOSAVE_LOG_LEVEL_OUTPUT == 0: |
| 38 | + savestate.AUTOSAVE_LOG_LEVEL_OUTPUT = 4 |
| 39 | + else: |
| 40 | + savestate.AUTOSAVE_LOG_LEVEL_OUTPUT -= 1 |
| 41 | + try: |
| 42 | + with open(savestate.standardFilePath + savestate.symbol + "autosave.json", "r", encoding="utf-8") as autosave: |
| 43 | + json_object = json.loads(autosave.read()) |
| 44 | + autosave.close() |
| 45 | + if filterkey: |
| 46 | + try: |
| 47 | + print("Seaching for key:", filterkey[0]) |
| 48 | + return json_object[filterkey[0]] |
| 49 | + except KeyError: |
| 50 | + return json_object |
| 51 | + except TypeError: |
| 52 | + print("Type error occurred while processing" + filterkey[0], json_object) |
| 53 | + else: |
| 54 | + if json_object: |
| 55 | + print("Returning json object!") |
| 56 | + return json_object |
| 57 | + else: |
| 58 | + return savestate.autosave_standard |
| 59 | + except FileNotFoundError: |
| 60 | + print("Autosave not found. Try restarting the program. Continuing for now...") |
| 61 | + logWrite("Autosave not found. This is not a critical error and a new one will be created...") |
| 62 | + return savestate.autosave_standard |
0 commit comments