ExportFileJob.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import re # Filtering out invalid characters.
  4. from typing import List, Optional
  5. from UM.FileHandler.FileHandler import FileHandler
  6. from UM.FileHandler.WriteFileJob import WriteFileJob
  7. from UM.Logger import Logger
  8. from UM.Scene.SceneNode import SceneNode
  9. from cura.CuraApplication import CuraApplication
  10. from .MeshFormatHandler import MeshFormatHandler
  11. class ExportFileJob(WriteFileJob):
  12. """Job that exports the build plate to the correct file format for the target cluster."""
  13. def __init__(self, file_handler: Optional[FileHandler], nodes: List[SceneNode], firmware_version: str) -> None:
  14. self._mesh_format_handler = MeshFormatHandler(file_handler, firmware_version)
  15. if not self._mesh_format_handler.is_valid:
  16. Logger.log("e", "Missing file or mesh writer!")
  17. return
  18. super().__init__(self._mesh_format_handler.writer, self._mesh_format_handler.createStream(), nodes,
  19. self._mesh_format_handler.file_mode)
  20. # Determine the filename.
  21. job_name = CuraApplication.getInstance().getPrintInformation().jobName
  22. job_name = re.sub("[^\w\-. ()]", "-", job_name)
  23. extension = self._mesh_format_handler.preferred_format.get("extension", "")
  24. self.setFileName("{}.{}".format(job_name, extension))
  25. def getMimeType(self) -> str:
  26. """Get the mime type of the selected export file type."""
  27. return self._mesh_format_handler.mime_type
  28. def getOutput(self) -> bytes:
  29. """Get the job result as bytes as that is what we need to upload to the cluster."""
  30. output = self.getStream().getvalue()
  31. if isinstance(output, str):
  32. output = output.encode("utf-8")
  33. return output