MachineInstance.py 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import UM.VersionUpgrade #To indicate that a file is of incorrect format.
  4. import configparser #To read config files.
  5. import io #To write config files to strings as if they were files.
  6. ## Creates a new machine instance instance by parsing a serialised machine
  7. # instance in version 1 of the file format.
  8. #
  9. # \param serialised The serialised form of a machine instance in version 1.
  10. # \param filename The supposed file name of this machine instance.
  11. # \return A machine instance instance, or None if the file format is
  12. # incorrect.
  13. def importFrom(serialised, filename):
  14. try:
  15. return MachineInstance(serialised, filename)
  16. except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
  17. return None
  18. ## A representation of a machine instance used as intermediary form for
  19. # conversion from one format to the other.
  20. class MachineInstance:
  21. ## Reads version 1 of the file format, storing it in memory.
  22. #
  23. # \param serialised A string with the contents of a machine instance file.
  24. # \param filename The supposed file name of this machine instance.
  25. def __init__(self, serialised, filename):
  26. self._filename = filename
  27. config = configparser.ConfigParser(interpolation = None)
  28. config.read_string(serialised) # Read the input string as config file.
  29. # Checking file correctness.
  30. if not config.has_section("general"):
  31. raise UM.VersionUpgrade.FormatException("No \"general\" section.")
  32. if not config.has_option("general", "version"):
  33. raise UM.VersionUpgrade.FormatException("No \"version\" in \"general\" section.")
  34. if not config.has_option("general", "name"):
  35. raise UM.VersionUpgrade.FormatException("No \"name\" in \"general\" section.")
  36. if not config.has_option("general", "type"):
  37. raise UM.VersionUpgrade.FormatException("No \"type\" in \"general\" section.")
  38. if int(config.get("general", "version")) != 1: # Explicitly hard-code version 1, since if this number changes the programmer MUST change this entire function.
  39. raise UM.VersionUpgrade.InvalidVersionException("The version of this machine instance is wrong. It must be 1.")
  40. self._type_name = config.get("general", "type")
  41. self._variant_name = config.get("general", "variant", fallback = None)
  42. self._name = config.get("general", "name")
  43. self._key = config.get("general", "key", fallback = None)
  44. self._active_profile_name = config.get("general", "active_profile", fallback = None)
  45. self._active_material_name = config.get("general", "material", fallback = None)
  46. self._machine_setting_overrides = {}
  47. for key, value in config["machine_settings"].items():
  48. self._machine_setting_overrides[key] = value
  49. ## Serialises this machine instance as file format version 2.
  50. #
  51. # This is where the actual translation happens in this case.
  52. #
  53. # \return A tuple containing the new filename and a serialised form of
  54. # this machine instance, serialised in version 2 of the file format.
  55. def export(self):
  56. config = configparser.ConfigParser(interpolation = None) # Build a config file in the form of version 2.
  57. config.add_section("general")
  58. config.set("general", "name", self._name)
  59. config.set("general", "id", self._name)
  60. config.set("general", "type", self._type_name)
  61. config.set("general", "version", "2") # Hard-code version 2, since if this number changes the programmer MUST change this entire function.
  62. import VersionUpgrade21to22 # Import here to prevent circular dependencies.
  63. type_name = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translatePrinter(self._type_name)
  64. active_profile = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateProfile(self._active_profile_name)
  65. active_material = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateProfile(self._active_material_name)
  66. variant = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateVariant(self._variant_name, type_name)
  67. containers = [
  68. self._name + "_current_settings",
  69. active_profile,
  70. active_material,
  71. variant,
  72. type_name
  73. ]
  74. config.set("general", "containers", ",".join(containers))
  75. config.add_section("metadata")
  76. config.set("metadata", "type", "machine")
  77. VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettings(self._machine_setting_overrides)
  78. config.add_section("values")
  79. for key, value in self._machine_setting_overrides.items():
  80. config.set("values", key, str(value))
  81. output = io.StringIO()
  82. config.write(output)
  83. return self._filename, output.getvalue()