BackupsManager.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 UM.Version import Version
  6. from cura.Backups.Backup import Backup
  7. if TYPE_CHECKING:
  8. from cura.CuraApplication import CuraApplication
  9. class BackupsManager:
  10. """
  11. The BackupsManager is responsible for managing the creating and restoring of
  12. back-ups.
  13. Back-ups themselves are represented in a different class.
  14. """
  15. def __init__(self, application: "CuraApplication") -> None:
  16. self._application = application
  17. def createBackup(self) -> Tuple[Optional[bytes], Optional[Dict[str, str]]]:
  18. """
  19. Get a back-up of the current configuration.
  20. :return: A tuple containing a ZipFile (the actual back-up) and a dict containing some metadata (like version).
  21. """
  22. self._disableAutoSave()
  23. backup = Backup(self._application)
  24. backup.makeFromCurrent()
  25. self._enableAutoSave()
  26. # We don't return a Backup here because we want plugins only to interact with our API and not full objects.
  27. return backup.zip_file, backup.meta_data
  28. def restoreBackup(self, zip_file: bytes, meta_data: Dict[str, str]) -> None:
  29. """
  30. Restore a back-up from a given ZipFile.
  31. :param zip_file: A bytes object containing the actual back-up.
  32. :param meta_data: A dict containing some metadata that is needed to restore the back-up correctly.
  33. """
  34. if not meta_data.get("cura_release", None):
  35. # If there is no "cura_release" specified in the meta data, we don't execute a backup restore.
  36. Logger.log("w", "Tried to restore a backup without specifying a Cura version number.")
  37. return
  38. self._disableAutoSave()
  39. backup = Backup(self._application, zip_file = zip_file, meta_data = meta_data)
  40. restored = backup.restore()
  41. if restored:
  42. # At this point, Cura will need to restart for the changes to take effect.
  43. # We don't want to store the data at this point as that would override the just-restored backup.
  44. self._application.windowClosed(save_data = False)
  45. def _disableAutoSave(self) -> None:
  46. """Here we (try to) disable the saving as it might interfere with restoring a back-up."""
  47. self._application.enableSave(False)
  48. auto_save = self._application.getAutoSave()
  49. # The auto save is only not created if the application has not yet started.
  50. if auto_save:
  51. auto_save.setEnabled(False)
  52. else:
  53. Logger.log("e", "Unable to disable the autosave as application init has not been completed")
  54. def _enableAutoSave(self) -> None:
  55. """Re-enable auto-save and other saving after we're done."""
  56. self._application.enableSave(True)
  57. auto_save = self._application.getAutoSave()
  58. # The auto save is only not created if the application has not yet started.
  59. if auto_save:
  60. auto_save.setEnabled(True)
  61. else:
  62. Logger.log("e", "Unable to enable the autosave as application init has not been completed")