Better error handling on project creation
offlate/formats/formatException.py
| 17 | 17 | class UnsupportedFormatException(Exception): | |
| 18 | 18 | def __init__(self, message): | |
| 19 | 19 | super().__init__('Unsupported format: '+message) | |
| 20 | + | self.unsupportedFormat = message |
offlate/manager.py
| 72 | 72 | with open(self.basedir + '/projects.json', 'w') as f: | |
| 73 | 73 | f.write(json.dumps([])) | |
| 74 | 74 | ||
| 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) | |
| 78 | 77 | ||
| 79 | 78 | 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 | + | ||
| 81 | 89 | try: | |
| 82 | 90 | data["version"] = "0.3" | |
| 83 | 91 | proj = self.loadProject(name, lang, system, data) | |
| 84 | 92 | proj.initialize(projectpath, callback) | |
| 85 | 93 | self.projects.append({"name": name, "lang": lang, "system": system, | |
| 86 | 94 | "info": data}) | |
| 87 | - | except UnsupportedFormatException: | |
| 95 | + | except Exception as e: | |
| 96 | + | callback.project_error(e) | |
| 88 | 97 | rmdir(projectpath) | |
| 98 | + | return False | |
| 89 | 99 | ||
| 90 | - | if callback is not None: | |
| 91 | - | callback.progress(100) | |
| 100 | + | callback.progress(100) | |
| 92 | 101 | ||
| 93 | 102 | self.writeProjects() | |
| 103 | + | return True | |
| 94 | 104 | ||
| 95 | 105 | def loadProject(self, name, lang, system, data): | |
| 96 | 106 | if not "Generic" in self.settings.conf: |
offlate/ui/manager.py
| 29 | 29 | ||
| 30 | 30 | from ..manager import ProjectManager | |
| 31 | 31 | ||
| 32 | + | from ..formats.formatException import UnsupportedFormatException | |
| 33 | + | ||
| 32 | 34 | class ProjectManagerWindow(QMainWindow): | |
| 33 | 35 | _instance = None | |
| 34 | 36 | ||
… | |||
| 159 | 161 | worker.signals.finished.connect(self.openProject) | |
| 160 | 162 | worker.signals.finished.connect(self.finishReport) | |
| 161 | 163 | worker.signals.progress.connect(self.reportProgress) | |
| 164 | + | worker.signals.error.connect(self.reportError) | |
| 162 | 165 | self.threadpool.start(worker) | |
| 163 | 166 | ||
| 167 | + | def reportError(self, name, msg): | |
| 168 | + | dialog = QMessageBox() | |
| 169 | + | dialog.setText(msg) | |
| 170 | + | dialog.exec_() | |
| 171 | + | self.finishReport(name) | |
| 172 | + | ||
| 164 | 173 | def reportProgress(self, name, progress): | |
| 165 | 174 | self.actionProgress.setEnabled(True) | |
| 166 | 175 | self.actionProgress.setValue(progress) | |
… | |||
| 231 | 240 | class NewRunnableSignals(QObject): | |
| 232 | 241 | finished = pyqtSignal(str) | |
| 233 | 242 | progress = pyqtSignal(str, int) | |
| 243 | + | error = pyqtSignal(str, str) | |
| 234 | 244 | ||
| 235 | 245 | class NewRunnable(QRunnable): | |
| 236 | 246 | def __init__(self, parent, name, lang, system, info): | |
… | |||
| 244 | 254 | self.oldamount = -1 | |
| 245 | 255 | ||
| 246 | 256 | 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, | |
| 248 | 258 | self.info, self) | |
| 249 | - | self.signals.finished.emit(self.name) | |
| 259 | + | if res: | |
| 260 | + | self.signals.finished.emit(self.name) | |
| 250 | 261 | self.parent.filter() | |
| 251 | 262 | ||
| 252 | 263 | def progress(self, amount): | |
… | |||
| 254 | 265 | self.oldamount = int(round(amount)) | |
| 255 | 266 | self.signals.progress.emit(self.name, amount) | |
| 256 | 267 | ||
| 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 | + | ||
| 257 | 289 | class EditRunnable(QRunnable): | |
| 258 | 290 | def __init__(self, parent, name, lang, system, info): | |
| 259 | 291 | super().__init__() | |