Upload on a separate thread

Julien LepillerSat Nov 09 16:10:17+0100 2019

5afaf7e

Upload on a separate thread

offlate/ui/editor.py

2020
2121
from .spellcheckedit import SpellCheckEdit
2222
from .tagclickedit import TagClickEdit
23+
from .parallel import RunnableCallback, RunnableSignals
2324
2425
import math
2526

278279
        super().__init__()
279280
        self.manager = manager
280281
        self.projectManagerWindow = projectManagerWindow
282+
        self.threadpool = QThreadPool()
281283
        self.initUI()
282284
283285
    def initOpenProjects(self, menu):

327329
        self.projectManagerWindow.new()
328330
329331
    def send(self):
330-
        self.tabs.currentWidget().send()
332+
        self.actionLabel.setText(self.tr("Uploading {}...").format(self.tabs.currentWidget().project.name))
333+
        worker = UploadRunnable(self.tabs.currentWidget())
334+
        worker.signals.finished.connect(self.sent)
335+
        self.threadpool.start(worker)
336+
337+
    def sent(self, name):
338+
        self.actionLabel.setText(self.tr("Upload of {} finished!").format(name))
331339
332340
    def update(self):
333341
        self.tabs.currentWidget().update()

472480
473481
        self.setGeometry(0, 0, 800, 600)
474482
        self.setWindowTitle('Offlate')
483+
484+
class UploadRunnable(QRunnable, RunnableCallback):
485+
    def __init__(self, widget):
486+
        super().__init__()
487+
        self.widget = widget
488+
        self.signals = RunnableSignals()
489+
    
490+
    def run(self):
491+
        self.widget.send()
492+
        self.signals.finished.emit(self.widget.project.name)

offlate/ui/manager.py

2626
from .new import NewWindow
2727
from .settings import SettingsWindow
2828
from .editor import EditorWindow
29+
from .parallel import RunnableCallback, RunnableSignals
2930
3031
from ..manager import ProjectManager
3132

273274
        worker.signals.finished.connect(self.finishReport)
274275
        self.threadpool.start(worker)
275276
276-
class NewRunnableSignals(QObject):
277-
    finished = pyqtSignal(str)
278-
    progress = pyqtSignal(str, int)
279-
    error = pyqtSignal(str, str)
280-
    restart_required = pyqtSignal(str, str, int, dict, str)
281-
282-
class RunnableCallback:
283-
    def progress(self, amount):
284-
        if int(round(amount)) > self.oldamount:
285-
            self.oldamount = int(round(amount))
286-
            self.signals.progress.emit(self.name, amount)
287-
288-
    def project_exists(self):
289-
        self.error = self.parent.tr('A project with the same name already exists. \
290-
The new project was not created. You should first remove the same-named project.')
291-
292-
    def project_present(self, directory):
293-
        self.error = self.parent.tr('Your filesystem contains a same-named \
294-
directory for your new project. The new project was not created. You should \
295-
first remove the same-named directory: "{}".'.format(directory))
296-
297-
    def project_error(self, error):
298-
        if isinstance(error, UnsupportedFormatException):
299-
            self.error = self.parent.tr('The project you added uses the {} format, \
300-
but it is not supported yet by Offlate. You can try to update the application, \
301-
or if you are on the latest version already, report it as a bug.'.format(error.unsupportedFormat))
302-
        elif isinstance(error, ProjectNotFoundSystemException):
303-
            self.error = self.parent.tr('The project {} you added could not be found \
304-
in the translation platform you selected. Did you make a typo while entering the \
305-
name or other parameters?'.format(error.projectNotFound))
306-
        else:
307-
            self.error = self.parent.tr('An unexpected error occured while \
308-
fetching the project: {}. You should report this as a bug.'.format(str(error)))
309-
310277
class NewRunnable(QRunnable, RunnableCallback):
311278
    def __init__(self, parent, name, lang, system, info):
312279
        super().__init__()

315282
        self.system = system
316283
        self.info = info
317284
        self.parent = parent
318-
        self.signals = NewRunnableSignals()
285+
        self.signals = RunnableSignals()
319286
        self.oldamount = -1
320287
        self.error = None
321288

337304
        self.system = system
338305
        self.info = info
339306
        self.parent = parent
340-
        self.signals = NewRunnableSignals()
307+
        self.signals = RunnableSignals()
341308
        self.oldamount = -1
342309
        self.error = None
343310

offlate/ui/parallel.py unknown status 1

1+
#   Copyright (c) 2019 Julien Lepiller <julien@lepiller.eu>
2+
#
3+
#   This program is free software: you can redistribute it and/or modify
4+
#   it under the terms of the GNU Affero General Public License as
5+
#   published by the Free Software Foundation, either version 3 of the
6+
#   License, or (at your option) any later version.
7+
#
8+
#   This program is distributed in the hope that it will be useful,
9+
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11+
#   GNU Affero General Public License for more details.
12+
#
13+
#   You should have received a copy of the GNU Affero General Public License
14+
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.
15+
####
16+
17+
from PyQt5.QtCore import *
18+
19+
class RunnableCallback:
20+
    def progress(self, amount):
21+
        if int(round(amount)) > self.oldamount:
22+
            self.oldamount = int(round(amount))
23+
            self.signals.progress.emit(self.name, amount)
24+
25+
    def project_exists(self):
26+
        self.error = self.parent.tr('A project with the same name already exists. \
27+
The new project was not created. You should first remove the same-named project.')
28+
29+
    def project_present(self, directory):
30+
        self.error = self.parent.tr('Your filesystem contains a same-named \
31+
directory for your new project. The new project was not created. You should \
32+
first remove the same-named directory: "{}".'.format(directory))
33+
34+
    def project_error(self, error):
35+
        if isinstance(error, UnsupportedFormatException):
36+
            self.error = self.parent.tr('The project you added uses the {} format, \
37+
but it is not supported yet by Offlate. You can try to update the application, \
38+
or if you are on the latest version already, report it as a bug.'.format(error.unsupportedFormat))
39+
        elif isinstance(error, ProjectNotFoundSystemException):
40+
            self.error = self.parent.tr('The project {} you added could not be found \
41+
in the translation platform you selected. Did you make a typo while entering the \
42+
name or other parameters?'.format(error.projectNotFound))
43+
        else:
44+
            self.error = self.parent.tr('An unexpected error occured while \
45+
fetching the project: {}. You should report this as a bug.'.format(str(error)))
46+
47+
class RunnableSignals(QObject):
48+
    finished = pyqtSignal(str)
49+
    progress = pyqtSignal(str, int)
50+
    error = pyqtSignal(str, str)
51+
    restart_required = pyqtSignal(str, str, int, dict, str)