diff --git a/pesterchum.py b/pesterchum.py index 17cf3fc..d3fa7c7 100755 --- a/pesterchum.py +++ b/pesterchum.py @@ -3188,7 +3188,7 @@ class PesterWindow(MovingWindow): msgbox = QtWidgets.QMessageBox() msgbox.setStyleSheet("QMessageBox{" + self.theme["main/defaultwindow/style"] + "}") msgbox.setWindowIcon(PesterIcon(self.theme["main/icon"])) - msgbox.setInformativeText("Failed to load server list, do you wish to revert to defaults?\n" \ + msgbox.setInformativeText("Failed to load server list, do you want to revert to defaults?\n" \ + "If you choose no, Pesterchum will most likely crash unless you manually fix serverlist.json\n" \ + "Please tell me if this error occurs :'3") msgbox.addButton(QtWidgets.QPushButton("Yes"), QtWidgets.QMessageBox.YesRole) diff --git a/profile.py b/profile.py index fea075d..4deb24a 100644 --- a/profile.py +++ b/profile.py @@ -4,11 +4,14 @@ _datadir = ostools.getDataDir() logging.config.fileConfig(_datadir + "logging.ini") PchumLog = logging.getLogger('pchumLogger') import os +import sys from string import Template import json import re import codecs import platform +import datetime +import shutil from datetime import * from time import strftime, time from PyQt5 import QtCore, QtGui, QtWidgets @@ -101,8 +104,22 @@ class userConfig(object): self.NEWCONVO = 8 self.INITIALS = 16 self.filename = _datadir+"pesterchum.js" - with open(self.filename) as fp: - self.config = json.load(fp) + try: + with open(self.filename) as fp: + self.config = json.load(fp) + except json.decoder.JSONDecodeError as e: + PchumLog.critical("ohno :(") + PchumLog.critical("failed to load pesterchum.js") + PchumLog.critical(e) + msgbox = QtWidgets.QMessageBox() + msgbox.setWindowTitle(":(") + msgbox.setTextFormat(QtCore.Qt.RichText) # Clickable html links + msgbox.setInformativeText("

Failed to load pesterchum.js, this might require manual intervention.

\ +Consider overriding: %s
\ +with a backup from: %s

" % (_datadir, self.filename, os.path.join(_datadir, "backup"), os.path.join(_datadir, "backup"))) + ret = msgbox.exec_() + sys.exit() + # Trying to fix: # IOError: [Errno 2] # No such file or directory: @@ -128,6 +145,69 @@ class userConfig(object): with open("%s/groups.js" % (self.logpath), 'w') as fp: json.dump(self.groups, fp) + self.backup() + + def backup(self): + # Backup pesterchum.js file. + # Useful because it seems to randomly get blanked for people. + + try: + backup_path = os.path.join(_datadir, "backup") + if os.path.exists(backup_path) == False: + os.makedirs(backup_path) + + current_backup = datetime.now().day % 5 + shutil.copyfile(self.filename, os.path.join(backup_path, "pesterchum.js.backup." + str(current_backup))) + + except OSError as e: + PchumLog.warning("Failed to make backup, no permission?") + PchumLog.warning(e) + except shutil.Error as e: + PchumLog.warning("Failed to make backup, shutil error?") + PchumLog.warning(e) + + # Yeah,,, we really don't need this nvm