__init__.py 2.3 KB

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