23ec51c

Add a progress bar in status and support callbacks when creating new projects

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):
75+
    def createProject(self, name, lang, system, data, callback=None):
76+
        if callback is not None:
77+
            callback.progress(0)
78+
7679
        projectpath = self.basedir + '/' + name
7780
        Path(projectpath).mkdir(parents=True)
7881
        try:
7982
            proj = self.loadProject(name, lang, system, data)
80-
            proj.initialize(projectpath)
83+
            proj.initialize(projectpath, callback)
8184
            if system == TRANSLATION_PROJECT:
8285
                self.projects.append({"name": name, "lang": lang, "system": system,
8386
                    "info": {"version": proj.version}})

8689
                    "info": data})
8790
        except UnsupportedFormatException:
8891
            rmdir(projectpath)
92+
93+
        if callback is not None:
94+
            callback.progress(100)
95+
8996
        self.writeProjects()
9097
9198
    def loadProject(self, name, lang, system, data):

offlate/systems/git.py

1919
from os import rename, remove
2020
2121
class Progress(pygit2.remote.RemoteCallbacks):
22+
    def __init__(self, callback):
23+
        super().__init__()
24+
        self.callback = callback
25+
2226
    def transfer_progress(self, stats):
23-
        try:
24-
            print("{}/{}\r".format(stats.received_objects, stats.total_objects), file=sys.stderr, end='')
25-
        except:
26-
            pass
27+
        if self.callback is not None:
28+
            self.callback.progress((100.0 * stats.received_objects) / \
29+
                    stats.total_objects)
2730
2831
class GitProject:
2932
    def __init__(self, conf, name, lang, data = {}):

3740
        self.updateURI()
3841
        self.updateFiles()
3942
40-
    def initialize(self, basedir):
43+
    def initialize(self, basedir, callback=None):
4144
        self.basedir = basedir
4245
        self.updateURI()
43-
        self.clone(basedir + "/current")
46+
        self.clone(basedir + "/current", callback)
4447
        self.updateFiles()
4548
    
4649
    def updateURI(self):

4952
    def updateFiles(self):
5053
        self.translationfiles = []
5154
52-
    def clone(self, directory):
53-
        print("Cloning {} in {}".format(self.uri, directory))
54-
        pygit2.clone_repository(self.uri, directory, callbacks=Progress())
55+
    def clone(self, directory, callback=None):
56+
        pygit2.clone_repository(self.uri, directory, callbacks=Progress(callback))
5557
        repo = pygit2.Repository(directory)
5658
        branch = repo.lookup_branch(self.branch)
5759
        ref = repo.lookup_reference(branch.name)

offlate/systems/tp.py

4848
        self.updateFileName()
4949
        self.updateGettextNames()
5050
51-
    def initialize(self, basedir):
51+
    def initialize(self, basedir, callback=None):
5252
        self.basedir = basedir
5353
        self.updateVersion()
5454
        self.updateFileName()

offlate/systems/transifex.py

3737
            self.files = json.load(f)
3838
        self.slugs = [x['slug'] for x in self.files]
3939
40-
    def initialize(self, basedir):
40+
    def initialize(self, basedir, callback=None):
4141
        self.basedir = basedir
4242
        self.updateFileList()
4343
        with open(self.basedir + '/project.info', 'w') as f:

offlate/ui/editor.py

433433
        self.next2Shortcut.activated.connect(self.nextItem)
434434
435435
        self.countLabel = QLabel()
436+
        self.actionLabel = QLabel()
437+
        self.actionProgress = QProgressBar()
438+
        self.actionProgress.setEnabled(False)
436439
        self.statusBar()
437440
        self.statusBar().addWidget(self.countLabel)
441+
        self.statusBar().addWidget(self.actionLabel)
442+
        self.statusBar().addWidget(self.actionProgress)
438443
439444
        openMenu = QMenu(self.tr('Open'), self)
440445
        self.initOpenProjects(openMenu)

offlate/ui/manager.py

117117
        hbox.addLayout(global_vbox, 1)
118118
119119
        self.setLayout(hbox)
120+
        self.parent().statusBar()
121+
        self.actionLabel = QLabel()
122+
        self.actionProgress = QProgressBar()
123+
        self.actionProgress.setEnabled(False)
124+
        self.parent().statusBar().addWidget(self.actionLabel)
125+
        self.parent().statusBar().addWidget(self.actionProgress)
120126
121127
        # Actions
122128
        self.searchfield.textChanged.connect(self.filter)

156162
        worker = NewRunnable(self, w.getProjectName(), w.getProjectLang(),
157163
                w.getProjectSystem(), w.getProjectInfo())
158164
        worker.signals.finished.connect(self.openProject)
165+
        worker.signals.finished.connect(self.finishReport)
166+
        worker.signals.progress.connect(self.reportProgress)
159167
        self.threadpool.start(worker)
160168
169+
    def reportProgress(self, name, progress):
170+
        self.actionProgress.setEnabled(True)
171+
        self.actionProgress.setValue(progress)
172+
        self.actionLabel.setText(
173+
                self.tr('Creating project {}??? ({}%)').format(name, progress))
174+
        self.editor.actionProgress.setEnabled(True)
175+
        self.editor.actionProgress.setValue(progress)
176+
        self.editor.actionLabel.setText(
177+
                self.tr('Creating project {}??? ({}%)').format(name, progress))
178+
179+
    def finishReport(self, name):
180+
        self.actionProgress.setValue(0)
181+
        self.actionProgress.setEnabled(False)
182+
        self.actionLabel.setText("")
183+
        self.editor.actionProgress.setValue(0)
184+
        self.editor.actionLabel.setText("")
185+
        self.editor.actionProgress.setEnabled(False)
186+
161187
    def settings(self):
162188
        w = SettingsWindow(self.manager.getConf())
163189
        w.exec_()

209235
210236
class NewRunnableSignals(QObject):
211237
    finished = pyqtSignal(str)
238+
    progress = pyqtSignal(str, int)
212239
213240
class NewRunnable(QRunnable):
214241
    def __init__(self, parent, name, lang, system, info):

219246
        self.info = info
220247
        self.parent = parent
221248
        self.signals = NewRunnableSignals()
249+
        self.oldamount = -1
222250
223251
    def run(self):
224252
        self.parent.manager.createProject(self.name, self.lang, self.system,
225-
                self.info)
226-
        #self.parent.openProject(self.name)
253+
                self.info, self)
227254
        self.signals.finished.emit(self.name)
228255
        self.parent.filter()
229256
257+
    def progress(self, amount):
258+
        if int(round(amount)) > self.oldamount:
259+
            self.oldamount = int(round(amount))
260+
            self.signals.progress.emit(self.name, amount)
261+
230262
class EditRunnable(QRunnable):
231263
    def __init__(self, parent, name, lang, system, info):
232264
        super().__init__()