BackupsManager.py 3.1 KB

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