Profile.py 6.8 KB

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