CuraProfileReader.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import os.path
  4. from UM.Application import Application #To get the machine manager to create the new profile in.
  5. from UM.Logger import Logger
  6. from UM.Settings.InstanceContainer import InstanceContainer #The new profile to make.
  7. from cura.ProfileReader import ProfileReader
  8. ## A plugin that reads profile data from Cura profile files.
  9. #
  10. # It reads a profile from a .curaprofile file, and returns it as a profile
  11. # instance.
  12. class CuraProfileReader(ProfileReader):
  13. ## Initialises the cura profile reader.
  14. # This does nothing since the only other function is basically stateless.
  15. def __init__(self):
  16. super().__init__()
  17. ## Reads a cura profile from a file and returns it.
  18. #
  19. # \param file_name The file to read the cura profile from.
  20. # \return The cura profile that was in the file, if any. If the file could
  21. # not be read or didn't contain a valid profile, \code None \endcode is
  22. # returned.
  23. def read(self, file_name):
  24. # Create an empty profile.
  25. profile = InstanceContainer(os.path.basename(os.path.splitext(file_name)[0]))
  26. profile.addMetaDataEntry("type", "quality")
  27. try:
  28. with open(file_name) as f: # Open file for reading.
  29. serialized = f.read()
  30. except IOError as e:
  31. Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
  32. return None
  33. try:
  34. profile.deserialize(serialized)
  35. except Exception as e: # Parsing error. This is not a (valid) Cura profile then.
  36. Logger.log("e", "Error while trying to parse profile: %s", str(e))
  37. return None
  38. return profile