MachineInstance.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 UM.VersionUpgradeManager #To schedule more files to be upgraded.
  5. from UM.Resources import Resources #To get the config storage path.
  6. import configparser #To read config files.
  7. import io #To write config files to strings as if they were files.
  8. import os.path #To get the path to write new user profiles to.
  9. import urllib #To serialise the user container file name properly.
  10. ## Creates a new machine instance instance by parsing a serialised machine
  11. # instance in version 1 of the file format.
  12. #
  13. # \param serialised The serialised form of a machine instance in version 1.
  14. # \param filename The supposed file name of this machine instance, without
  15. # extension.
  16. # \return A machine instance instance, or None if the file format is
  17. # incorrect.
  18. def importFrom(serialised, filename):
  19. try:
  20. return MachineInstance(serialised, filename)
  21. except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
  22. return None
  23. ## A representation of a machine instance used as intermediary form for
  24. # conversion from one format to the other.
  25. class MachineInstance:
  26. ## Reads version 1 of the file format, storing it in memory.
  27. #
  28. # \param serialised A string with the contents of a machine instance file,
  29. # without extension.
  30. # \param filename The supposed file name of this machine instance.
  31. def __init__(self, serialised, filename):
  32. self._filename = filename
  33. config = configparser.ConfigParser(interpolation = None)
  34. config.read_string(serialised) # Read the input string as config file.
  35. # Checking file correctness.
  36. if not config.has_section("general"):
  37. raise UM.VersionUpgrade.FormatException("No \"general\" section.")
  38. if not config.has_option("general", "version"):
  39. raise UM.VersionUpgrade.FormatException("No \"version\" in \"general\" section.")
  40. if not config.has_option("general", "name"):
  41. raise UM.VersionUpgrade.FormatException("No \"name\" in \"general\" section.")
  42. if not config.has_option("general", "type"):
  43. raise UM.VersionUpgrade.FormatException("No \"type\" in \"general\" section.")
  44. if int(config.get("general", "version")) != 1: # Explicitly hard-code version 1, since if this number changes the programmer MUST change this entire function.
  45. raise UM.VersionUpgrade.InvalidVersionException("The version of this machine instance is wrong. It must be 1.")
  46. self._type_name = config.get("general", "type")
  47. self._variant_name = config.get("general", "variant", fallback = "empty_variant")
  48. self._name = config.get("general", "name", fallback = "")
  49. self._key = config.get("general", "key", fallback = None)
  50. self._active_profile_name = config.get("general", "active_profile", fallback = "empty_quality")
  51. self._active_material_name = config.get("general", "material", fallback = "empty_material")
  52. self._machine_setting_overrides = {}
  53. for key, value in config["machine_settings"].items():
  54. self._machine_setting_overrides[key] = value
  55. ## Serialises this machine instance as file format version 2.
  56. #
  57. # This is where the actual translation happens in this case.
  58. #
  59. # \return A tuple containing the new filename and a serialised form of
  60. # this machine instance, serialised in version 2 of the file format.
  61. def export(self):
  62. config = configparser.ConfigParser(interpolation = None) # Build a config file in the form of version 2.
  63. config.add_section("general")
  64. config.set("general", "name", self._name)
  65. config.set("general", "id", self._name)
  66. config.set("general", "version", "2") # Hard-code version 2, since if this number changes the programmer MUST change this entire function.
  67. import VersionUpgrade21to22 # Import here to prevent circular dependencies.
  68. has_machine_qualities = self._type_name in VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.machinesWithMachineQuality()
  69. type_name = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translatePrinter(self._type_name)
  70. active_material = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateMaterial(self._active_material_name)
  71. variant = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateVariant(self._variant_name, type_name)
  72. variant_materials = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateVariantForMaterials(self._variant_name, type_name)
  73. #Convert to quality profile if we have one of the built-in profiles, otherwise convert to a quality-changes profile.
  74. if self._active_profile_name in VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.builtInProfiles():
  75. active_quality = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateProfile(self._active_profile_name)
  76. active_quality_changes = "empty_quality_changes"
  77. else:
  78. active_quality = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.getQualityFallback(type_name, variant, active_material)
  79. active_quality_changes = self._active_profile_name
  80. if has_machine_qualities: #This machine now has machine-quality profiles.
  81. active_material += "_" + variant_materials
  82. #Create a new user profile and schedule it to be upgraded.
  83. user_profile = configparser.ConfigParser(interpolation = None)
  84. user_profile["general"] = {
  85. "version": "2",
  86. "name": "Current settings",
  87. "definition": type_name
  88. }
  89. user_profile["metadata"] = {
  90. "type": "user",
  91. "machine": self._name
  92. }
  93. user_profile["values"] = {}
  94. version_upgrade_manager = UM.VersionUpgradeManager.VersionUpgradeManager.getInstance()
  95. user_storage = os.path.join(Resources.getDataStoragePath(), next(iter(version_upgrade_manager.getStoragePaths("user"))))
  96. user_profile_file = os.path.join(user_storage, urllib.parse.quote_plus(self._name) + "_current_settings.inst.cfg")
  97. if not os.path.exists(user_storage):
  98. os.makedirs(user_storage)
  99. with open(user_profile_file, "w", encoding = "utf-8") as file_handle:
  100. user_profile.write(file_handle)
  101. version_upgrade_manager.upgradeExtraFile(user_storage, urllib.parse.quote_plus(self._name), "user")
  102. containers = [
  103. self._name + "_current_settings", #The current profile doesn't know the definition ID when it was upgraded, only the instance ID, so it will be invalid. Sorry, your current settings are lost now.
  104. active_quality_changes,
  105. active_quality,
  106. active_material,
  107. variant,
  108. type_name
  109. ]
  110. config.set("general", "containers", ",".join(containers))
  111. config.add_section("metadata")
  112. config.set("metadata", "type", "machine")
  113. VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettings(self._machine_setting_overrides)
  114. config.add_section("values")
  115. for key, value in self._machine_setting_overrides.items():
  116. config.set("values", key, str(value))
  117. output = io.StringIO()
  118. config.write(output)
  119. return [self._filename], [output.getvalue()]