__init__.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, TYPE_CHECKING
  4. from PyQt5.QtCore import QObject, pyqtProperty
  5. from cura.API.Backups import Backups
  6. from cura.API.Interface import Interface
  7. from cura.API.Account import Account
  8. if TYPE_CHECKING:
  9. from cura.CuraApplication import CuraApplication
  10. ## The official Cura API that plug-ins can use to interact with Cura.
  11. #
  12. # Python does not technically prevent talking to other classes as well, but
  13. # this API provides a version-safe interface with proper deprecation warnings
  14. # etc. Usage of any other methods than the ones provided in this API can cause
  15. # plug-ins to be unstable.
  16. class CuraAPI(QObject):
  17. # For now we use the same API version to be consistent.
  18. __instance = None # type: "CuraAPI"
  19. _application = None # type: CuraApplication
  20. # This is done to ensure that the first time an instance is created, it's forced that the application is set.
  21. # The main reason for this is that we want to prevent consumers of API to have a dependency on CuraApplication.
  22. # Since the API is intended to be used by plugins, the cura application should have already created this.
  23. def __new__(cls, application: Optional["CuraApplication"] = None):
  24. if cls.__instance is None:
  25. if application is None:
  26. raise Exception("Upon first time creation, the application must be set.")
  27. cls.__instance = super(CuraAPI, cls).__new__(cls)
  28. cls._application = application
  29. return cls.__instance
  30. def __init__(self, application: Optional["CuraApplication"] = None) -> None:
  31. super().__init__(parent = CuraAPI._application)
  32. # Accounts API
  33. self._account = Account(self._application)
  34. # Backups API
  35. self._backups = Backups(self._application)
  36. # Interface API
  37. self._interface = Interface(self._application)
  38. def initialize(self) -> None:
  39. self._account.initialize()
  40. @pyqtProperty(QObject, constant = True)
  41. def account(self) -> "Account":
  42. return self._account
  43. @property
  44. def backups(self) -> "Backups":
  45. return self._backups
  46. @property
  47. def interface(self) -> "Interface":
  48. return self._interface