From 18361b94343dd1f443aa4074c6f8c5e28b38c904 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Tue, 2 Oct 2018 10:31:05 +0200 Subject: [PATCH] Ensure that CuraAPI can be called in the same way as before This should prevent another API break. CURA-5744 --- cura/API/__init__.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cura/API/__init__.py b/cura/API/__init__.py index e9aba86a41..ad07452c1a 100644 --- a/cura/API/__init__.py +++ b/cura/API/__init__.py @@ -23,10 +23,22 @@ class CuraAPI(QObject): # For now we use the same API version to be consistent. VERSION = PluginRegistry.APIVersion + __instance = None # type: "CuraAPI" + _application = None # type: CuraApplication - def __init__(self, application: "CuraApplication") -> None: - super().__init__(parent = application) - self._application = application + # This is done to ensure that the first time an instance is created, it's forced that the application is set. + # The main reason for this is that we want to prevent consumers of API to have a dependency on CuraApplication. + # Since the API is intended to be used by plugins, the cura application should have already created this. + def __new__(cls, application: Optional["CuraApplication"] = None): + if cls.__instance is None: + if application is None: + raise Exception("Upon first time creation, the application must be set.") + cls.__instance = super(CuraAPI, cls).__new__(cls) + cls._application = application + return cls.__instance + + def __init__(self, application: Optional["CuraApplication"] = None) -> None: + super().__init__(parent = CuraAPI._application) # Accounts API self._account = Account(self._application)