RemovableDrivePlugin.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Uranium is released under the terms of the LGPLv3 or higher.
  3. import threading
  4. import time
  5. from UM.Message import Message
  6. from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
  7. from UM.Logger import Logger
  8. from . import RemovableDriveOutputDevice
  9. from UM.Logger import Logger
  10. from UM.i18n import i18nCatalog
  11. catalog = i18nCatalog("cura")
  12. class RemovableDrivePlugin(OutputDevicePlugin):
  13. def __init__(self):
  14. super().__init__()
  15. self._update_thread = threading.Thread(target = self._updateThread)
  16. self._update_thread.setDaemon(True)
  17. self._check_updates = True
  18. self._drives = {}
  19. def start(self):
  20. self._update_thread.start()
  21. def stop(self):
  22. self._check_updates = False
  23. self._update_thread.join()
  24. self._addRemoveDrives({})
  25. def checkRemovableDrives(self):
  26. raise NotImplementedError()
  27. def ejectDevice(self, device):
  28. try:
  29. Logger.log("i", "Attempting to eject the device")
  30. result = self.performEjectDevice(device)
  31. except Exception as e:
  32. Logger.log("e", "Ejection failed due to: %s" % str(e))
  33. result = False
  34. if result:
  35. Logger.log("i", "Succesfully ejected the device")
  36. return result
  37. def performEjectDevice(self, device):
  38. raise NotImplementedError()
  39. def _updateThread(self):
  40. while self._check_updates:
  41. result = self.checkRemovableDrives()
  42. self._addRemoveDrives(result)
  43. time.sleep(5)
  44. def _addRemoveDrives(self, drives):
  45. # First, find and add all new or changed keys
  46. for key, value in drives.items():
  47. if key not in self._drives:
  48. self.getOutputDeviceManager().addOutputDevice(RemovableDriveOutputDevice.RemovableDriveOutputDevice(key, value))
  49. continue
  50. if self._drives[key] != value:
  51. self.getOutputDeviceManager().removeOutputDevice(key)
  52. self.getOutputDeviceManager().addOutputDevice(RemovableDriveOutputDevice.RemovableDriveOutputDevice(key, value))
  53. # Then check for keys that have been removed
  54. for key in self._drives.keys():
  55. if key not in drives:
  56. self.getOutputDeviceManager().removeOutputDevice(key)
  57. self._drives = drives