From 7ae6800a14ada0b2c2ef81a1deaf0b1484b66e85 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Fri, 28 Sep 2018 14:01:28 +0200 Subject: [PATCH 1/4] Fix imports in QualityManager --- cura/CuraApplication.py | 2 +- cura/Machines/QualityManager.py | 12 ++++-------- cura/Scene/ConvexHullDecorator.py | 1 - 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index 04c9ea88db..6fb79403cc 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -681,7 +681,7 @@ class CuraApplication(QtApplication): Logger.log("i", "Initializing quality manager") from cura.Machines.QualityManager import QualityManager - self._quality_manager = QualityManager(container_registry, parent = self) + self._quality_manager = QualityManager(self, parent = self) self._quality_manager.initialize() Logger.log("i", "Initializing machine manager") diff --git a/cura/Machines/QualityManager.py b/cura/Machines/QualityManager.py index d924f4c83e..ce19624c21 100644 --- a/cura/Machines/QualityManager.py +++ b/cura/Machines/QualityManager.py @@ -5,8 +5,6 @@ from typing import TYPE_CHECKING, Optional, cast, Dict, List from PyQt5.QtCore import QObject, QTimer, pyqtSignal, pyqtSlot -from UM.Application import Application - from UM.ConfigurationErrorMessage import ConfigurationErrorMessage from UM.Logger import Logger from UM.Util import parseBool @@ -22,7 +20,6 @@ if TYPE_CHECKING: from cura.Settings.GlobalStack import GlobalStack from .QualityChangesGroup import QualityChangesGroup from cura.CuraApplication import CuraApplication - from UM.Settings.ContainerRegistry import ContainerRegistry # @@ -39,12 +36,11 @@ class QualityManager(QObject): qualitiesUpdated = pyqtSignal() - def __init__(self, container_registry: "ContainerRegistry", parent = None) -> None: + def __init__(self, application: "CuraApplication", parent = None) -> None: super().__init__(parent) - from cura.CuraApplication import CuraApplication - self._application = CuraApplication.getInstance() # type: CuraApplication + self._application = application self._material_manager = self._application.getMaterialManager() - self._container_registry = container_registry + self._container_registry = self._application.getContainerRegistry() self._empty_quality_container = self._application.empty_quality_container self._empty_quality_changes_container = self._application.empty_quality_changes_container @@ -460,7 +456,7 @@ class QualityManager(QObject): # stack and clear the user settings. @pyqtSlot(str) def createQualityChanges(self, base_name: str) -> None: - machine_manager = CuraApplication.getInstance().getMachineManager() + machine_manager = self._application.getMachineManager() global_stack = machine_manager.activeMachine if not global_stack: diff --git a/cura/Scene/ConvexHullDecorator.py b/cura/Scene/ConvexHullDecorator.py index 8532f40890..bdb4cbcba8 100644 --- a/cura/Scene/ConvexHullDecorator.py +++ b/cura/Scene/ConvexHullDecorator.py @@ -9,7 +9,6 @@ from UM.Math.Polygon import Polygon from UM.Scene.SceneNodeDecorator import SceneNodeDecorator from UM.Settings.ContainerRegistry import ContainerRegistry - from cura.Settings.ExtruderManager import ExtruderManager from cura.Scene import ConvexHullNode From 3bc91f15c34814e3de424ec025b2f070ff7a223a Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Fri, 28 Sep 2018 14:17:00 +0200 Subject: [PATCH 2/4] Fix mypy complains --- cura/OAuth2/AuthorizationHelpers.py | 40 +++++++++++++++-------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/cura/OAuth2/AuthorizationHelpers.py b/cura/OAuth2/AuthorizationHelpers.py index 6cb53d2252..0a1447297c 100644 --- a/cura/OAuth2/AuthorizationHelpers.py +++ b/cura/OAuth2/AuthorizationHelpers.py @@ -4,7 +4,7 @@ import json import random from hashlib import sha512 from base64 import b64encode -from typing import Optional +from typing import Dict, Optional import requests @@ -24,37 +24,39 @@ class AuthorizationHelpers: def settings(self) -> "OAuth2Settings": return self._settings + # Gets a dictionary with data that need to be used for any HTTP authorization request. + def getCommonRequestDataDict(self) -> Dict[str, str]: + data_dict = {"client_id": self._settings.CLIENT_ID if self._settings.CLIENT_ID is not None else "", + "redirect_uri": self._settings.CALLBACK_URL if self._settings.CALLBACK_URL is not None else "", + "scope": self._settings.CLIENT_SCOPES if self._settings.CLIENT_SCOPES is not None else "", + } + return data_dict + # Request the access token from the authorization server. # \param authorization_code: The authorization code from the 1st step. # \param verification_code: The verification code needed for the PKCE extension. # \return: An AuthenticationResponse object. - def getAccessTokenUsingAuthorizationCode(self, authorization_code: str, verification_code: str)-> "AuthenticationResponse": - return self.parseTokenResponse(requests.post(self._token_url, data={ - "client_id": self._settings.CLIENT_ID, - "redirect_uri": self._settings.CALLBACK_URL, - "grant_type": "authorization_code", - "code": authorization_code, - "code_verifier": verification_code, - "scope": self._settings.CLIENT_SCOPES - })) # type: ignore + def getAccessTokenUsingAuthorizationCode(self, authorization_code: str, verification_code: str) -> "AuthenticationResponse": + data = self.getCommonRequestDataDict() + data["grant_type"] = "authorization_code" + data["code"] = authorization_code + data["code_verifier"] = verification_code + return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore # Request the access token from the authorization server using a refresh token. # \param refresh_token: # \return: An AuthenticationResponse object. - def getAccessTokenUsingRefreshToken(self, refresh_token: str) -> AuthenticationResponse: - return self.parseTokenResponse(requests.post(self._token_url, data={ - "client_id": self._settings.CLIENT_ID, - "redirect_uri": self._settings.CALLBACK_URL, - "grant_type": "refresh_token", - "refresh_token": refresh_token, - "scope": self._settings.CLIENT_SCOPES - })) # type: ignore + def getAccessTokenUsingRefreshToken(self, refresh_token: str) -> "AuthenticationResponse": + data = self.getCommonRequestDataDict() + data["grant_type"] = "refresh_token" + data["refresh_token"] = refresh_token + return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore @staticmethod # Parse the token response from the authorization server into an AuthenticationResponse object. # \param token_response: The JSON string data response from the authorization server. # \return: An AuthenticationResponse object. - def parseTokenResponse(token_response: requests.models.Response) -> AuthenticationResponse: + def parseTokenResponse(token_response: requests.models.Response) -> "AuthenticationResponse": token_data = None try: From 5761d28f7f8620c4735c80ea442ec7a6ad4de877 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Fri, 28 Sep 2018 14:24:21 +0200 Subject: [PATCH 3/4] Use old data dict code style, more readable --- cura/OAuth2/AuthorizationHelpers.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/cura/OAuth2/AuthorizationHelpers.py b/cura/OAuth2/AuthorizationHelpers.py index 0a1447297c..c149f74ab2 100644 --- a/cura/OAuth2/AuthorizationHelpers.py +++ b/cura/OAuth2/AuthorizationHelpers.py @@ -37,19 +37,27 @@ class AuthorizationHelpers: # \param verification_code: The verification code needed for the PKCE extension. # \return: An AuthenticationResponse object. def getAccessTokenUsingAuthorizationCode(self, authorization_code: str, verification_code: str) -> "AuthenticationResponse": - data = self.getCommonRequestDataDict() - data["grant_type"] = "authorization_code" - data["code"] = authorization_code - data["code_verifier"] = verification_code + data = { + "client_id": self._settings.CLIENT_ID if self._settings.CLIENT_ID is not None else "", + "redirect_uri": self._settings.CALLBACK_URL if self._settings.CALLBACK_URL is not None else "", + "grant_type": "authorization_code", + "code": authorization_code, + "code_verifier": verification_code, + "scope": self._settings.CLIENT_SCOPES if self._settings.CLIENT_SCOPES is not None else "", + } return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore # Request the access token from the authorization server using a refresh token. # \param refresh_token: # \return: An AuthenticationResponse object. def getAccessTokenUsingRefreshToken(self, refresh_token: str) -> "AuthenticationResponse": - data = self.getCommonRequestDataDict() - data["grant_type"] = "refresh_token" - data["refresh_token"] = refresh_token + data = { + "client_id": self._settings.CLIENT_ID if self._settings.CLIENT_ID is not None else "", + "redirect_uri": self._settings.CALLBACK_URL if self._settings.CALLBACK_URL is not None else "", + "grant_type": "refresh_token", + "refresh_token": refresh_token, + "scope": self._settings.CLIENT_SCOPES if self._settings.CLIENT_SCOPES is not None else "", + } return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore @staticmethod From 1bee201cfd2db5453e34e06d98ccd166518bfd26 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Fri, 28 Sep 2018 14:24:40 +0200 Subject: [PATCH 4/4] Remove unused code --- cura/OAuth2/AuthorizationHelpers.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/cura/OAuth2/AuthorizationHelpers.py b/cura/OAuth2/AuthorizationHelpers.py index c149f74ab2..f75ad9c9f9 100644 --- a/cura/OAuth2/AuthorizationHelpers.py +++ b/cura/OAuth2/AuthorizationHelpers.py @@ -24,14 +24,6 @@ class AuthorizationHelpers: def settings(self) -> "OAuth2Settings": return self._settings - # Gets a dictionary with data that need to be used for any HTTP authorization request. - def getCommonRequestDataDict(self) -> Dict[str, str]: - data_dict = {"client_id": self._settings.CLIENT_ID if self._settings.CLIENT_ID is not None else "", - "redirect_uri": self._settings.CALLBACK_URL if self._settings.CALLBACK_URL is not None else "", - "scope": self._settings.CLIENT_SCOPES if self._settings.CLIENT_SCOPES is not None else "", - } - return data_dict - # Request the access token from the authorization server. # \param authorization_code: The authorization code from the 1st step. # \param verification_code: The verification code needed for the PKCE extension.