BackupsManager.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Dict, Optional, Tuple, TYPE_CHECKING
  4. from UM.Logger import Logger
  5. from cura.Backups.Backup import Backup
  6. if TYPE_CHECKING:
  7. from cura.CuraApplication import CuraApplication
  8. ## The BackupsManager is responsible for managing the creating and restoring of
  9. # back-ups.
  10. #
  11. # Back-ups themselves are represented in a different class.
  12. class BackupsManager:
  13. def __init__(self, application: "CuraApplication") -> None:
  14. self._application = application
  15. ## Get a back-up of the current configuration.
  16. # \return A tuple containing a ZipFile (the actual back-up) and a dict
  17. # containing some metadata (like version).
  18. def createBackup(self) -> Tuple[Optional[bytes], Optional[Dict[str, str]]]:
  19. self._disableAutoSave()
  20. backup = Backup(self._application)
  21. backup.makeFromCurrent()
  22. self._enableAutoSave()
  23. # We don't return a Backup here because we want plugins only to interact with our API and not full objects.
  24. return backup.zip_file, backup.meta_data
  25. ## Restore a back-up from a given ZipFile.
  26. # \param zip_file A bytes object containing the actual back-up.
  27. # \param meta_data A dict containing some metadata that is needed to
  28. # restore the back-up correctly.
  29. def restoreBackup(self, zip_file: bytes, meta_data: Dict[str, str]) -> None:
  30. if not meta_data.get("cura_release", None):
  31. # If there is no "cura_release" specified in the meta data, we don't execute a backup restore.
  32. Logger.log("w", "Tried to restore a backup without specifying a Cura version number.")
  33. return
  34. self._disableAutoSave()
  35. backup = Backup(self._application, zip_file = zip_file, meta_data = meta_data)
  36. restored = backup.restore()
  37. if restored:
  38. # At this point, Cura will need to restart for the changes to take effect.
  39. # We don't want to store the data at this point as that would override the just-restored backup.
  40. self._application.windowClosed(save_data = False)
  41. ## Here we try to disable the auto-save plug-in as it might interfere with
  42. # restoring a back-up.
  43. def _disableAutoSave(self) -> None:
  44. self._application.setSaveDataEnabled(False)
  45. ## Re-enable auto-save after we're done.
  46. def _enableAutoSave(self) -> None:
  47. self._application.setSaveDataEnabled(True)