__init__.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 PyQt6.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. instance = super(CuraAPI, cls).__new__(cls)
  29. cls._application = application
  30. return instance
  31. def __init__(self, application: Optional["CuraApplication"] = None) -> None:
  32. super().__init__(parent = CuraAPI._application)
  33. CuraAPI.__instance = self
  34. self._account = Account(self._application)
  35. self._backups = Backups(self._application)
  36. self._connectionStatus = ConnectionStatus()
  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. """Accounts API"""
  44. return self._account
  45. @pyqtProperty(QObject, constant = True)
  46. def connectionStatus(self) -> "ConnectionStatus":
  47. return self._connectionStatus
  48. @property
  49. def backups(self) -> "Backups":
  50. """Backups API"""
  51. return self._backups
  52. @property
  53. def interface(self) -> "Interface":
  54. """Interface API"""
  55. return self._interface