BackupsManager.py 2.3 KB

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