__init__.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.ConnectionStatus import ConnectionStatus
  7. from cura.API.Interface import Interface
  8. from cura.API.Account import Account
  9. if TYPE_CHECKING:
  10. from cura.CuraApplication import CuraApplication
  11. class CuraAPI(QObject):
  12. """The official Cura API that plug-ins can use to interact with Cura.
  13. Python does not technically prevent talking to other classes as well, but this API provides a version-safe
  14. interface with proper deprecation warnings etc. Usage of any other methods than the ones provided in this API can
  15. cause plug-ins to be unstable.
  16. """
  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. self._account = Account(self._application)
  34. self._backups = Backups(self._application)
  35. self._connectionStatus = ConnectionStatus()
  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. """Accounts API"""
  43. return self._account
  44. @pyqtProperty(QObject, constant = True)
  45. def connectionStatus(self) -> "ConnectionStatus":
  46. return self._connectionStatus
  47. @property
  48. def backups(self) -> "Backups":
  49. """Backups API"""
  50. return self._backups
  51. @property
  52. def interface(self) -> "Interface":
  53. """Interface API"""
  54. return self._interface