Fix editing projects

Julien LepillerTue Aug 27 23:48:47+0200 2019

d68c945

Fix editing projects

CHANGELOG

2121
* Unsupported formats raises a specific exception that can be caught by the UI
2222
* Stop crashing when an error occurs during project fetching
2323
* Properly load empty translations in ts format
24+
* Fix crash when editing a project
2425
2526
Changes since 0.2
2627
-----------------

offlate/manager.py

159159
        self.projects = [x for x in self.projects if x['name'] != name]
160160
        self.writeProjects()
161161
162-
    def updateProject(self, name, lang, system, info):
163-
        rmdir(self.basedir + '/' + name)
162+
    def updateProject(self, name, lang, system, info, callback):
163+
        try:
164+
            rmdir(self.basedir + '/' + name)
165+
        except:
166+
            pass
164167
        self.projects = [x for x in self.projects if x['name'] != name]
165-
        self.createProject(name, lang, system, info)
168+
        return self.createProject(name, lang, system, info, callback)

offlate/ui/manager.py

249249
            return
250250
        worker = EditRunnable(self, w.getProjectName(), w.getProjectLang(),
251251
                w.getProjectSystem(), w.getProjectInfo())
252+
        worker.signals.progress.connect(self.reportProgress)
253+
        worker.signals.restart_required.connect(self.restartNew)
254+
        worker.signals.finished.connect(self.finishReport)
252255
        self.threadpool.start(worker)
253256
254257
class NewRunnableSignals(QObject):

257260
    error = pyqtSignal(str, str)
258261
    restart_required = pyqtSignal(str, str, int, dict, str)
259262
260-
class NewRunnable(QRunnable):
261-
    def __init__(self, parent, name, lang, system, info):
262-
        super().__init__()
263-
        self.name = name
264-
        self.lang = lang
265-
        self.system = system
266-
        self.info = info
267-
        self.parent = parent
268-
        self.signals = NewRunnableSignals()
269-
        self.oldamount = -1
270-
        self.error = None
271-
272-
    def run(self):
273-
        res = self.parent.manager.createProject(self.name, self.lang, self.system,
274-
                self.info, self)
275-
        if res:
276-
            self.signals.finished.emit(self.name)
277-
        else:
278-
            self.signals.restart_required.emit(self.name, self.lang, self.system,
279-
                    self.info, self.error)
280-
        self.parent.filter()
281-
263+
class RunnableCallback:
282264
    def progress(self, amount):
283265
        if int(round(amount)) > self.oldamount:
284266
            self.oldamount = int(round(amount))

302284
            self.error = self.parent.tr('An unexpected error occured while \
303285
fetching the project: {}. You should report this as a bug.'.format(str(error)))
304286
305-
class EditRunnable(QRunnable):
287+
class NewRunnable(QRunnable, RunnableCallback):
288+
    def __init__(self, parent, name, lang, system, info):
289+
        super().__init__()
290+
        self.name = name
291+
        self.lang = lang
292+
        self.system = system
293+
        self.info = info
294+
        self.parent = parent
295+
        self.signals = NewRunnableSignals()
296+
        self.oldamount = -1
297+
        self.error = None
298+
299+
    def run(self):
300+
        res = self.parent.manager.createProject(self.name, self.lang, self.system,
301+
                self.info, self)
302+
        if res:
303+
            self.signals.finished.emit(self.name)
304+
        else:
305+
            self.signals.restart_required.emit(self.name, self.lang, self.system,
306+
                    self.info, self.error)
307+
        self.parent.filter()
308+
309+
class EditRunnable(QRunnable, RunnableCallback):
306310
    def __init__(self, parent, name, lang, system, info):
307311
        super().__init__()
308312
        self.name = name

310314
        self.system = system
311315
        self.info = info
312316
        self.parent = parent
317+
        self.signals = NewRunnableSignals()
318+
        self.oldamount = -1
319+
        self.error = None
313320
314321
    def run(self):
315-
        self.parent.manager.updateProject(self.name, self.lang, self.system,
316-
                self.info)
322+
        res = self.parent.manager.updateProject(self.name, self.lang, self.system,
323+
                self.info, self)
324+
        if res:
325+
            self.signals.finished.emit(self.name)
326+
        else:
327+
            self.signals.restart_required.emit(self.name, self.lang, self.system,
328+
                    self.info, self.error)
317329
        self.parent.filter()