CuraProfileWriter.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  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 UM.SaveFile import SaveFile
  6. from cura.ProfileWriter import ProfileWriter
  7. import zipfile
  8. ## Writes profiles to Cura's own profile format with config files.
  9. class CuraProfileWriter(ProfileWriter):
  10. ## Writes a profile to the specified file path.
  11. #
  12. # \param path \type{string} The file to output to.
  13. # \param profiles \type{Profile} \type{List} The profile(s) to write to that file.
  14. # \return \code True \endcode if the writing was successful, or \code
  15. # False \endcode if it wasn't.
  16. def write(self, path, profiles):
  17. if type(profiles) != list:
  18. profiles = [profiles]
  19. stream = open(path, "wb") # Open file for writing in binary.
  20. archive = zipfile.ZipFile(stream, "w", compression=zipfile.ZIP_DEFLATED)
  21. try:
  22. # Open the specified file.
  23. for profile in profiles:
  24. serialized = profile.serialize()
  25. profile_file = zipfile.ZipInfo(profile.getId())
  26. archive.writestr(profile_file, serialized)
  27. except Exception as e:
  28. Logger.log("e", "Failed to write profile to %s: %s", path, str(e))
  29. return False
  30. finally:
  31. archive.close()
  32. return True