Ensure that CuraAPI can be called in the same way as before

This should prevent another API break.

CURA-5744
This commit is contained in:
Jaime van Kessel 2018-10-02 10:31:05 +02:00
parent acb7df710c
commit 18361b9434

View File

@ -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)