CuraProfileReader.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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.Settings.Profile import Profile
  5. from UM.Settings.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. #
  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. profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False) #Create an empty profile.
  24. serialised = ""
  25. try:
  26. with open(file_name) as f: #Open file for reading.
  27. serialised = f.read()
  28. except IOError as e:
  29. Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
  30. return None
  31. try:
  32. profile.unserialise(serialised)
  33. except Exception as e: #Parsing error. This is not a (valid) Cura profile then.
  34. return None
  35. return profile