Fixes for the callback system

Julien LepillerSat Nov 14 02:55:26+0100 2020

bfa24fa

Fixes for the callback system

offlate/core/manager.py

7171
                f.write(json.dumps([]))
7272
7373
    def createProject(self, name, lang, system, data, callback):
74-
        callback.progress(0)
74+
        callback.reportProgress(0)
7575
7676
        projectpath = self.basedir + '/' + name
7777
        path = Path(projectpath)

9797
            rmdir(projectpath)
9898
            return False
9999
100-
        callback.progress(100)
100+
        callback.reportProgress(100)
101101
102102
        self.writeProjects()
103103
        return True

offlate/systems/callback.py

2020
    """
2121
    The base class for callbacks used in the different systems.
2222
    """
23-
    def reportProgress(progress):
23+
    def reportProgress(self, progress):
2424
        """
2525
        Report progress in some operation.  This method is called during download,
2626
        update and upload of translation projects.
2727
        """
2828
        raise Exception("Unimplemented method in concrete class: reportProgress")
2929
30+
    def askPassword(self):
31+
        """
32+
        Get a password (interactively or not) and return it to the caller.
33+
        """
34+
        raise Exception("Unimplemented method in concrete class: askPassword")
35+
3036
    def githubBranchError(branch):
3137
        """
3238
        Report an error while accessing a branch

offlate/systems/git.py

4646
4747
    def transfer_progress(self, stats):
4848
        if self.callback is not None:
49-
            self.callback.progress((100.0 * stats.received_objects) / \
49+
            self.callback.reportProgress((100.0 * stats.received_objects) / \
5050
                    stats.total_objects)
5151
5252
class GitProject(Project):

offlate/ui/editor.py

2121
from .spellcheckedit import SpellCheckEdit
2222
from .tagclickedit import TagClickEdit
2323
from .parallel import RunnableCallback, RunnableSignals
24+
from ..systems.callback import SystemCallback
2425
2526
import math
2627
import platform

3536
    def __init__(self, parent = None):
3637
        super(ProjectTab, self).__init__(parent)
3738
38-
class Interface:
39+
class Interface(SystemCallback):
3940
    def __init__(self):
4041
        self.value = None
4142
4243
    def ok(self):
4344
        self.value = self.qd.textValue()
4445
46+
    def mergeConflict(self, base, oldTranslation, newTranslation):
47+
        # TODO: Actually do something more intelligent
48+
        return newTranslation
49+
4550
    def askPassword(self):
4651
        self.qd = QInputDialog()
4752
        self.qd.setLabelText(self.qd.tr("Please enter your password:"))

5055
        self.qd.exec_()
5156
        return self.value
5257
53-
    def gitlabTokenNotFound(self, server):
58+
    def gitlabTokenNotFoundError(self, server):
5459
        self.qd = QErrorMessage()
5560
        self.qd.showMessage(self.qd.tr("Token for {} not found. Have you added this server to your settings?.").format(server))
5661
        self.qd.exec_()
5762
58-
    def gitlabTokenBranchError(self, branch):
63+
    def gitlabBranchError(self, branch):
5964
        self.qd = QErrorMessage()
6065
        self.qd.showMessage(self.qd.tr("Error while creating branch {}.").format(branch))
6166
        self.qd.exec_()

366371
        self.project.save()
367372
        self.project.send(Interface())
368373
369-
    def askmerge(self, msgid, oldstr, newstr):
370-
        # TODO: Actually do something more intelligent
371-
        return newstr
372-
373-
    def update(self, callback=None):
374+
    def update(self, callback=Interface()):
374375
        self.project.save()
375-
        self.project.update(self.askmerge, callback)
376+
        self.project.update(callback)
376377
        self.content = self.project.content()
377378
        self.updateContent()
378379

399400
        l = self.manager.listProjects()
400401
        for p in l:
401402
            name = p['name']
402-
            act = QAction(name, self) 
403+
            act = QAction(name, self)
403404
            act.triggered.connect((lambda name: (lambda : self.open(name)))(name))
404405
            menu.addAction(act)
405406

621622
        self.oldamount = -1
622623
        self.error = None
623624
        self.name = self.widget.project.name
624-
    
625+
625626
    def run(self):
626627
        self.widget.send()
627628
        self.signals.finished.emit(self.name)

634635
        self.oldamount = -1
635636
        self.error = None
636637
        self.name = self.widget.project.name
637-
    
638+
638639
    def run(self):
639-
        self.widget.update(self)
640+
        self.widget.update(Interface())
640641
        self.signals.finished.emit(self.name)
641642
642643
class ExternalRunnable(QRunnable, RunnableCallback):

647648
        self.oldamount = -1
648649
        self.error = None
649650
        self.name = ""
650-
    
651+
651652
    def run(self):
652653
        # Try to open file with the default application
653654
        try:

687688
            files.extend(f)
688689
        for f in files:
689690
            self.observer.schedule(event_handler, str(Path(f).parent), recursive=True)
690-
    
691+
691692
    def run(self):
692693
        self.observer.start()
693694
        while self.size > 0:

offlate/ui/parallel.py

1717
from PyQt5.QtCore import *
1818
from ..formats.exception import UnsupportedFormatException
1919
from ..systems.exception import ProjectNotFoundSystemException
20+
from ..systems.callback import SystemCallback
2021
21-
class RunnableCallback:
22-
    def progress(self, amount):
22+
class RunnableCallback(SystemCallback):
23+
    def reportProgress(self, amount):
2324
        if int(round(amount)) > self.oldamount:
2425
            self.oldamount = int(round(amount))
2526
            self.signals.progress.emit(self.name, amount)