Convert SYNC_STATES to Enum

CURA-7290
This commit is contained in:
Nino van Hooff 2020-05-04 17:26:38 +02:00
parent 637a241d99
commit eac884fcd2
3 changed files with 23 additions and 18 deletions

View file

@ -1,6 +1,7 @@
# Copyright (c) 2018 Ultimaker B.V. # Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from datetime import datetime from datetime import datetime
from enum import Enum
from typing import Optional, Dict, TYPE_CHECKING from typing import Optional, Dict, TYPE_CHECKING
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, pyqtProperty from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, pyqtProperty
@ -28,6 +29,14 @@ i18n_catalog = i18nCatalog("cura")
# api.account.userProfile # Who is logged in`` # api.account.userProfile # Who is logged in``
# #
class Account(QObject): class Account(QObject):
class SyncState(Enum):
"""Caution: values used in qml (eg. UserOperations.qml)"""
SYNCING = "syncing",
SUCCESS = "success",
ERROR = "error"
# Signal emitted when user logged in or out. # Signal emitted when user logged in or out.
loginStateChanged = pyqtSignal(bool) loginStateChanged = pyqtSignal(bool)
accessTokenChanged = pyqtSignal() accessTokenChanged = pyqtSignal()
@ -36,8 +45,6 @@ class Account(QObject):
lastSyncDateTimeChanged = pyqtSignal() lastSyncDateTimeChanged = pyqtSignal()
syncStateChanged = pyqtSignal(str) syncStateChanged = pyqtSignal(str)
SYNC_STATES = ["syncing", "success", "error"]
def __init__(self, application: "CuraApplication", parent = None) -> None: def __init__(self, application: "CuraApplication", parent = None) -> None:
super().__init__(parent) super().__init__(parent)
self._application = application self._application = application
@ -45,7 +52,7 @@ class Account(QObject):
self._error_message = None # type: Optional[Message] self._error_message = None # type: Optional[Message]
self._logged_in = False self._logged_in = False
self._sync_state = "idle" self._sync_state = self.SyncState.SUCCESS
self._last_sync_str = "-" self._last_sync_str = "-"
self._callback_port = 32118 self._callback_port = 32118
@ -67,7 +74,7 @@ class Account(QObject):
self._authorization_service = AuthorizationService(self._oauth_settings) self._authorization_service = AuthorizationService(self._oauth_settings)
self._sync_clients = {} self._sync_clients = {}
"""contains entries "client_name" : "state["success"|"error|"syncing"]""" """contains entries "client_name" : "SyncState"""
def initialize(self) -> None: def initialize(self) -> None:
self._authorization_service.initialize(self._application.getPreferences()) self._authorization_service.initialize(self._application.getPreferences())
@ -76,32 +83,29 @@ class Account(QObject):
self._authorization_service.accessTokenChanged.connect(self._onAccessTokenChanged) self._authorization_service.accessTokenChanged.connect(self._onAccessTokenChanged)
self._authorization_service.loadAuthDataFromPreferences() self._authorization_service.loadAuthDataFromPreferences()
def setSyncState(self, service_name: str, state: str) -> None: def setSyncState(self, service_name: str, state: SyncState) -> None:
""" Can be used to register and update account sync states """ Can be used to register and update account sync states
Example: `setSyncState("packages", "syncing")` Example: `setSyncState("packages", Account.SyncState.SYNCING)`
:param service_name: A unique name for your service, such as `plugins` or `backups` :param service_name: A unique name for your service, such as `plugins` or `backups`
:param state: One of Account.SYNC_STATES :param state: One of Account.SYNC_STATES
""" """
prev_state = self._sync_state prev_state = self._sync_state
if state not in Account.SYNC_STATES:
raise AttributeError("Invalid state parameter: {}".format(state))
self._sync_clients[service_name] = state self._sync_clients[service_name] = state
if any(val == "syncing" for val in self._sync_clients.values()): if any(val == self.SyncState.SYNCING for val in self._sync_clients.values()):
self._sync_state = "syncing" self._sync_state = self.SyncState.SYNCING
elif any(val == "error" for val in self._sync_clients.values()): elif any(val == self.SyncState.ERROR for val in self._sync_clients.values()):
self._sync_state = "error" self._sync_state = self.SyncState.ERROR
else: else:
self._sync_state = "success" self._sync_state = self.SyncState.SUCCESS
if self._sync_state != prev_state: if self._sync_state != prev_state:
self.syncStateChanged.emit(self._sync_state) self.syncStateChanged.emit(self._sync_state.value[0])
if self._sync_state == "success": if self._sync_state == self.SyncState.SUCCESS:
self._last_sync_str = datetime.now().strftime("%d/%m/%Y %H:%M") self._last_sync_str = datetime.now().strftime("%d/%m/%Y %H:%M")
self.lastSyncDateTimeChanged.emit() self.lastSyncDateTimeChanged.emit()

View file

@ -13,6 +13,7 @@ from UM.Logger import Logger
from UM.Message import Message from UM.Message import Message
from UM.Signal import Signal from UM.Signal import Signal
from UM.TaskManagement.HttpRequestScope import JsonDecoratorScope from UM.TaskManagement.HttpRequestScope import JsonDecoratorScope
from cura.API import Account
from cura.CuraApplication import CuraApplication, ApplicationMetadata from cura.CuraApplication import CuraApplication, ApplicationMetadata
from cura.UltimakerCloud.UltimakerCloudScope import UltimakerCloudScope from cura.UltimakerCloud.UltimakerCloudScope import UltimakerCloudScope
from .SubscribedPackagesModel import SubscribedPackagesModel from .SubscribedPackagesModel import SubscribedPackagesModel
@ -53,7 +54,7 @@ class CloudPackageChecker(QObject):
self._hideSyncMessage() self._hideSyncMessage()
def _getUserSubscribedPackages(self) -> None: def _getUserSubscribedPackages(self) -> None:
self._application.getCuraAPI().account.setSyncState(self.SYNC_SERVICE_NAME, "syncing") self._application.getCuraAPI().account.setSyncState(self.SYNC_SERVICE_NAME, Account.SyncState.SYNCING)
Logger.debug("Requesting subscribed packages metadata from server.") Logger.debug("Requesting subscribed packages metadata from server.")
url = CloudApiModel.api_url_user_packages url = CloudApiModel.api_url_user_packages
self._application.getHttpRequestManager().get(url, self._application.getHttpRequestManager().get(url,

View file

@ -104,7 +104,7 @@ class CloudOutputDeviceManager:
self._update_timer.stop() self._update_timer.stop()
self._syncing = True self._syncing = True
self._account.setSyncState(self.SYNC_SERVICE_NAME, "syncing") self._account.setSyncState(self.SYNC_SERVICE_NAME, Account.SyncState.SYNCING)
self._api.getClusters(self._onGetRemoteClustersFinished, self._onGetRemoteClusterFailed) self._api.getClusters(self._onGetRemoteClustersFinished, self._onGetRemoteClusterFailed)
def _onGetRemoteClustersFinished(self, clusters: List[CloudClusterResponse]) -> None: def _onGetRemoteClustersFinished(self, clusters: List[CloudClusterResponse]) -> None: