CuraProfileReader.py 1.6 KB

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