__init__.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 not None:
  25. raise RuntimeError("Tried to create singleton '{class_name}' more than once.".format(class_name = CuraAPI.__name__))
  26. if application is None:
  27. raise RuntimeError("Upon first time creation, the application must be set.")
  28. cls.__instance = super(CuraAPI, cls).__new__(cls)
  29. cls._application = application
  30. return cls.__instance
  31. def __init__(self, application: Optional["CuraApplication"] = None) -> None:
  32. super().__init__(parent = CuraAPI._application)
  33. # Accounts API
  34. self._account = Account(self._application)
  35. # Backups API
  36. self._backups = Backups(self._application)
  37. # Interface API
  38. self._interface = Interface(self._application)
  39. def initialize(self) -> None:
  40. self._account.initialize()
  41. @pyqtProperty(QObject, constant = True)
  42. def account(self) -> "Account":
  43. return self._account
  44. @property
  45. def backups(self) -> "Backups":
  46. return self._backups
  47. @property
  48. def interface(self) -> "Interface":
  49. return self._interface