Better error handling on project creation

Julien LepillerSun Aug 25 16:43:25+0200 2019

cd91468

Better error handling on project creation

offlate/formats/formatException.py

1717
class UnsupportedFormatException(Exception):
1818
    def __init__(self, message):
1919
        super().__init__('Unsupported format: '+message)
20+
        self.unsupportedFormat = message

offlate/manager.py

7272
            with open(self.basedir + '/projects.json', 'w') as f:
7373
                f.write(json.dumps([]))
7474
75-
    def createProject(self, name, lang, system, data, callback=None):
76-
        if callback is not None:
77-
            callback.progress(0)
75+
    def createProject(self, name, lang, system, data, callback):
76+
        callback.progress(0)
7877
7978
        projectpath = self.basedir + '/' + name
80-
        Path(projectpath).mkdir(parents=True)
79+
        path = Path(projectpath)
80+
        if not path.exists():
81+
            path.mkdir(parents=True)
82+
        else:
83+
            if len([x for x in self.projects if x['name'] == name]) > 0:
84+
                callback.project_exists()
85+
            else:
86+
                callback.project_present(projectpath)
87+
            return False
88+
8189
        try:
8290
            data["version"] = "0.3"
8391
            proj = self.loadProject(name, lang, system, data)
8492
            proj.initialize(projectpath, callback)
8593
            self.projects.append({"name": name, "lang": lang, "system": system,
8694
                "info": data})
87-
        except UnsupportedFormatException:
95+
        except Exception as e:
96+
            callback.project_error(e)
8897
            rmdir(projectpath)
98+
            return False
8999
90-
        if callback is not None:
91-
            callback.progress(100)
100+
        callback.progress(100)
92101
93102
        self.writeProjects()
103+
        return True
94104
95105
    def loadProject(self, name, lang, system, data):
96106
        if not "Generic" in self.settings.conf:

offlate/ui/manager.py

2929
3030
from ..manager import ProjectManager
3131
32+
from ..formats.formatException import UnsupportedFormatException
33+
3234
class ProjectManagerWindow(QMainWindow):
3335
    _instance = None
3436

159161
        worker.signals.finished.connect(self.openProject)
160162
        worker.signals.finished.connect(self.finishReport)
161163
        worker.signals.progress.connect(self.reportProgress)
164+
        worker.signals.error.connect(self.reportError)
162165
        self.threadpool.start(worker)
163166
167+
    def reportError(self, name, msg):
168+
        dialog = QMessageBox()
169+
        dialog.setText(msg)
170+
        dialog.exec_()
171+
        self.finishReport(name)
172+
164173
    def reportProgress(self, name, progress):
165174
        self.actionProgress.setEnabled(True)
166175
        self.actionProgress.setValue(progress)

231240
class NewRunnableSignals(QObject):
232241
    finished = pyqtSignal(str)
233242
    progress = pyqtSignal(str, int)
243+
    error = pyqtSignal(str, str)
234244
235245
class NewRunnable(QRunnable):
236246
    def __init__(self, parent, name, lang, system, info):

244254
        self.oldamount = -1
245255
246256
    def run(self):
247-
        self.parent.manager.createProject(self.name, self.lang, self.system,
257+
        res = self.parent.manager.createProject(self.name, self.lang, self.system,
248258
                self.info, self)
249-
        self.signals.finished.emit(self.name)
259+
        if res:
260+
            self.signals.finished.emit(self.name)
250261
        self.parent.filter()
251262
252263
    def progress(self, amount):

254265
            self.oldamount = int(round(amount))
255266
            self.signals.progress.emit(self.name, amount)
256267
268+
    def project_exists(self):
269+
        self.signals.error.emit(self.name, self.parent.tr('A project with the \
270+
same name already exists. The new project was not created. You should first \
271+
remove the same-named project.'))
272+
273+
    def project_present(self, directory):
274+
        self.signals.error.emit(self.name, self.parent.tr('Your filesystem \
275+
contains a same-named directory for your new project. The new project was not \
276+
created. You should first remove the same-named directory: {}.'.format(directory)))
277+
278+
    def project_error(self, error):
279+
        if isinstance(error, UnsupportedFormatException):
280+
            self.signals.error.emit(self.name, self.parent.tr('The project you \
281+
added uses the {} format, but it is not supported yet by Offlate. You can try to \
282+
update the application, or if you are on the latest version already, report \
283+
it as a bug!'.format(error.unsupportedFormat)))
284+
        else:
285+
            self.signals.error.emit(self.name, self.parent.tr('An unexpected \
286+
error occured while fetching the project: {}. You should report this as a \
287+
bug.'.format(str(error))))
288+
257289
class EditRunnable(QRunnable):
258290
    def __init__(self, parent, name, lang, system, info):
259291
        super().__init__()