CuraProfileWriter.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Copyright (c) 2013 David Braam
  3. # Uranium is released under the terms of the LGPLv3 or higher.
  4. from UM.Logger import Logger
  5. from cura.ReaderWriters.ProfileWriter import ProfileWriter
  6. import zipfile
  7. class CuraProfileWriter(ProfileWriter):
  8. """Writes profiles to Cura's own profile format with config files."""
  9. def write(self, path, profiles):
  10. """Writes a profile to the specified file path.
  11. :param path: :type{string} The file to output to.
  12. :param profiles: :type{Profile} :type{List} The profile(s) to write to that file.
  13. :return: True if the writing was successful, or
  14. False if it wasn't.
  15. """
  16. if type(profiles) != list:
  17. profiles = [profiles]
  18. stream = open(path, "wb") # Open file for writing in binary.
  19. archive = zipfile.ZipFile(stream, "w", compression=zipfile.ZIP_DEFLATED)
  20. try:
  21. # Open the specified file.
  22. for profile in profiles:
  23. serialized = profile.serialize()
  24. profile_file = zipfile.ZipInfo(profile.getId())
  25. archive.writestr(profile_file, serialized)
  26. except Exception as e:
  27. Logger.log("e", "Failed to write profile to %s: %s", path, str(e))
  28. return False
  29. finally:
  30. archive.close()
  31. return True