Profile.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser #To read config files.
  4. import io #To write config files to strings as if they were files.
  5. from typing import Dict
  6. from typing import List
  7. import UM.VersionUpgrade
  8. from UM.Logger import Logger
  9. ## Creates a new profile instance by parsing a serialised profile in version 1
  10. # of the file format.
  11. #
  12. # \param serialised The serialised form of a profile in version 1.
  13. # \param filename The supposed filename of the profile, without extension.
  14. # \return A profile instance, or None if the file format is incorrect.
  15. def importFrom(serialised, filename):
  16. try:
  17. return Profile(serialised, filename)
  18. except (configparser.Error, UM.VersionUpgrade.FormatException, UM.VersionUpgrade.InvalidVersionException):
  19. return None
  20. ## A representation of a profile used as intermediary form for conversion from
  21. # one format to the other.
  22. class Profile:
  23. ## Reads version 1 of the file format, storing it in memory.
  24. #
  25. # \param serialised A string with the contents of a profile.
  26. # \param filename The supposed filename of the profile, without extension.
  27. def __init__(self, serialised: str, filename: str) -> None:
  28. self._filename = filename
  29. parser = configparser.ConfigParser(interpolation = None)
  30. parser.read_string(serialised)
  31. # Check correctness.
  32. if not parser.has_section("general"):
  33. raise UM.VersionUpgrade.FormatException("No \"general\" section.")
  34. if not parser.has_option("general", "version"):
  35. raise UM.VersionUpgrade.FormatException("No \"version\" in the \"general\" section.")
  36. if int(parser.get("general", "version")) != 1: # Hard-coded profile version here. If this number changes the entire function needs to change.
  37. raise UM.VersionUpgrade.InvalidVersionException("The version of this profile is wrong. It must be 1.")
  38. # Parse the general section.
  39. self._name = parser.get("general", "name")
  40. self._type = parser.get("general", "type", fallback = None)
  41. if "weight" in parser["general"]:
  42. self._weight = int(parser.get("general", "weight"))
  43. else:
  44. self._weight = None
  45. self._machine_type_id = parser.get("general", "machine_type", fallback = None)
  46. self._machine_variant_name = parser.get("general", "machine_variant", fallback = None)
  47. self._machine_instance_name = parser.get("general", "machine_instance", fallback = None)
  48. if "material" in parser["general"]: #Note: Material name is unused in this upgrade.
  49. self._material_name = parser.get("general", "material")
  50. elif self._type == "material":
  51. self._material_name = parser.get("general", "name", fallback = None)
  52. else:
  53. self._material_name = None
  54. # Parse the settings.
  55. self._settings = {} # type: Dict[str,str]
  56. if parser.has_section("settings"):
  57. for key, value in parser["settings"].items():
  58. self._settings[key] = value
  59. # Parse the defaults and the disabled defaults.
  60. self._changed_settings_defaults = {} # type: Dict[str,str]
  61. if parser.has_section("defaults"):
  62. for key, value in parser["defaults"].items():
  63. self._changed_settings_defaults[key] = value
  64. self._disabled_settings_defaults = [] # type: List[str]
  65. if parser.has_section("disabled_defaults"):
  66. disabled_defaults_string = parser.get("disabled_defaults", "values")
  67. self._disabled_settings_defaults = [item for item in disabled_defaults_string.split(",") if item != ""] # Split by comma.
  68. ## Serialises this profile as file format version 2.
  69. #
  70. # \return A tuple containing the new filename and a serialised form of
  71. # this profile, serialised in version 2 of the file format.
  72. def export(self):
  73. import VersionUpgrade21to22 # Import here to prevent circular dependencies.
  74. if self._name == "Current settings":
  75. return None, None #Can't upgrade these, because the new current profile needs to specify the definition ID and the old file only had the machine instance, not the definition.
  76. config = configparser.ConfigParser(interpolation = None)
  77. config.add_section("general")
  78. config.set("general", "version", "2") #Hard-coded profile version 2.
  79. config.set("general", "name", self._name)
  80. if self._machine_type_id:
  81. translated_machine = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translatePrinter(self._machine_type_id)
  82. config.set("general", "definition", translated_machine)
  83. else:
  84. config.set("general", "definition", "fdmprinter") #In this case, the machine definition is unknown, and it might now have machine-specific profiles, in which case this will fail.
  85. config.add_section("metadata")
  86. config.set("metadata", "quality_type", "normal") #This feature doesn't exist in 2.1 yet, so we don't know the actual quality type. For now, always base it on normal.
  87. config.set("metadata", "type", "quality")
  88. if self._weight:
  89. config.set("metadata", "weight", str(self._weight))
  90. if self._machine_variant_name:
  91. if self._machine_type_id:
  92. config.set("metadata", "variant", VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateVariant(self._machine_variant_name, self._machine_type_id))
  93. else:
  94. config.set("metadata", "variant", self._machine_variant_name)
  95. if self._settings:
  96. self._settings = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettings(self._settings)
  97. config.add_section("values")
  98. for key, value in self._settings.items():
  99. config.set("values", key, str(value))
  100. if self._changed_settings_defaults:
  101. self._changed_settings_defaults = VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettings(self._changed_settings_defaults)
  102. config.add_section("defaults")
  103. for key, value in self._changed_settings_defaults.items():
  104. config.set("defaults", key, str(value))
  105. if self._disabled_settings_defaults:
  106. disabled_settings_defaults = [VersionUpgrade21to22.VersionUpgrade21to22.VersionUpgrade21to22.translateSettingName(setting)
  107. for setting in self._disabled_settings_defaults]
  108. config.add_section("disabled_defaults")
  109. disabled_defaults_string = str(disabled_settings_defaults[0]) #Must be at least 1 item, otherwise we wouldn't enter this if statement.
  110. for item in disabled_settings_defaults[1:]:
  111. disabled_defaults_string += "," + str(item)
  112. output = io.StringIO()
  113. config.write(output)
  114. return [self._filename], [output.getvalue()]