CuraProfileReader.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 UM.Settings.Profile import Profile
  6. from UM.Settings.ProfileReader import ProfileReader
  7. ## A plugin that reads profile data from Cura profile files.
  8. #
  9. # It reads a profile from a .curaprofile file, and returns it as a profile
  10. # instance.
  11. class CuraProfileReader(ProfileReader):
  12. ## Initialises the cura profile reader.
  13. # This does nothing since the only other function is basically stateless.
  14. def __init__(self):
  15. super().__init__()
  16. ## Reads a cura profile from a file and returns it.
  17. #
  18. # \param file_name The file to read the cura profile from.
  19. # \return The cura profile that was in the file, if any. If the file could
  20. # not be read or didn't contain a valid profile, \code None \endcode is
  21. # returned.
  22. def read(self, file_name):
  23. # Create an empty profile.
  24. profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False)
  25. serialised = ""
  26. try:
  27. with open(file_name) as f: # Open file for reading.
  28. serialised = f.read()
  29. except IOError as e:
  30. Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
  31. return None
  32. try:
  33. profile.unserialise(serialised)
  34. except Exception as e: # Parsing error. This is not a (valid) Cura profile then.
  35. Logger.log("e", "Error while trying to parse profile: %s", str(e))
  36. return None
  37. return profile