BackupsManager.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. auto_save = self._application.getAutoSave()
  45. # The auto save is only not created if the application has not yet started.
  46. if auto_save:
  47. auto_save.setEnabled(False)
  48. else:
  49. Logger.log("e", "Unable to disable the autosave as application init has not been completed")
  50. ## Re-enable auto-save after we're done.
  51. def _enableAutoSave(self) -> None:
  52. auto_save = self._application.getAutoSave()
  53. # The auto save is only not created if the application has not yet started.
  54. if auto_save:
  55. auto_save.setEnabled(True)
  56. else:
  57. Logger.log("e", "Unable to enable the autosave as application init has not been completed")