GCodeGzReader.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import gzip
  4. from UM.Mesh.MeshReader import MeshReader #The class we're extending/implementing.
  5. from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType #To add the .gcode.gz files to the MIME type database.
  6. from UM.PluginRegistry import PluginRegistry
  7. class GCodeGzReader(MeshReader):
  8. """A file reader that reads gzipped g-code.
  9. If you're zipping g-code, you might as well use gzip!
  10. """
  11. def __init__(self) -> None:
  12. super().__init__()
  13. MimeTypeDatabase.addMimeType(
  14. MimeType(
  15. name = "application/x-cura-compressed-gcode-file",
  16. comment = "Cura Compressed G-code File",
  17. suffixes = ["gcode.gz"]
  18. )
  19. )
  20. self._supported_extensions = [".gcode.gz"]
  21. def _read(self, file_name):
  22. with open(file_name, "rb") as file:
  23. file_data = file.read()
  24. uncompressed_gcode = gzip.decompress(file_data).decode("utf-8")
  25. PluginRegistry.getInstance().getPluginObject("GCodeReader").preReadFromStream(uncompressed_gcode)
  26. result = PluginRegistry.getInstance().getPluginObject("GCodeReader").readFromStream(uncompressed_gcode, file_name)
  27. return result