Skip to content

Instantly share code, notes, and snippets.

@wonder-sk
Created February 17, 2020 22:15
Show Gist options
  • Save wonder-sk/3150e77d3ec0912d4c4e0c8242033aea to your computer and use it in GitHub Desktop.
Save wonder-sk/3150e77d3ec0912d4c4e0c8242033aea to your computer and use it in GitHub Desktop.
Mergin GUI download sample code
import os
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QProgressBar, QApplication, QPushButton, QLabel, QMessageBox
from mergin import (
MerginClient,
MerginProject,
InvalidProject
)
from mergin.client_pull import download_project_async, download_project_is_running, download_project_finalize, download_project_cancel
# config
project = "martin/fibre"
mergin_user = "martin"
mergin_pass = "xxx"
c = MerginClient("https://public.cloudmergin.com/", None, mergin_user, mergin_pass)
directory = os.path.basename(project)
app = QApplication([])
class MyWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.bar = QProgressBar(self)
self.status_label = QLabel(self)
self.start_button = QPushButton(self)
self.start_button.setText("Start!")
self.cancel_button = QPushButton(self)
self.cancel_button.setText("Cancel!")
main_layout = QVBoxLayout()
main_layout.addWidget(self.bar)
main_layout.addWidget(self.status_label)
main_layout.addWidget(self.start_button)
main_layout.addWidget(self.cancel_button)
self.setLayout(main_layout)
self.timer = QTimer(self)
self.timer.setInterval(100)
self.start_button.clicked.connect(self.start_download)
self.cancel_button.clicked.connect(self.cancel_download)
self.timer.timeout.connect(self.timer_timeout)
self.job = None
def start_download(self):
if self.job:
QMessageBox.information(self, "msg", "already downloading")
return
self.job = download_project_async(c, project, directory)
self.status_label.setText("downloading!")
self.bar.setMaximum(self.job.total_size)
self.bar.setValue(0)
self.timer.start()
def timer_timeout(self):
self.bar.setValue(self.job.transferred_size)
if not download_project_is_running(self.job):
self.timer.stop()
download_project_finalize(self.job)
self.job = None
QMessageBox.information(self, "msg", "DONE!")
self.status_label.setText("")
return
def cancel_download(self):
if not self.job:
QMessageBox.information(self, "msg", "download not active")
return
self.timer.stop()
self.status_label.setText("cancelled!")
download_project_cancel(self.job)
self.job = None
w = MyWidget()
w.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment