VersionUpgrade27to30.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import configparser #To parse preference files.
  4. import io #To serialise the preference files afterwards.
  5. import os
  6. from typing import Dict, List, Tuple
  7. import urllib.parse
  8. import re
  9. from UM.VersionUpgrade import VersionUpgrade #We're inheriting from this.
  10. _renamed_themes = {
  11. "cura": "cura-light"
  12. } # type: Dict[str, str]
  13. _renamed_i18n = {
  14. "7s": "en_7S",
  15. "de": "de_DE",
  16. "en": "en_US",
  17. "es": "es_ES",
  18. "fi": "fi_FI",
  19. "fr": "fr_FR",
  20. "hu": "hu_HU",
  21. "it": "it_IT",
  22. "jp": "ja_JP",
  23. "ko": "ko_KR",
  24. "nl": "nl_NL",
  25. "pl": "pl_PL",
  26. "ptbr": "pt_BR",
  27. "ru": "ru_RU",
  28. "tr": "tr_TR"
  29. } # type: Dict[str, str]
  30. class VersionUpgrade27to30(VersionUpgrade):
  31. ## Upgrades a preferences file from version 2.7 to 3.0.
  32. #
  33. # \param serialised The serialised form of a preferences file.
  34. # \param filename The name of the file to upgrade.
  35. def upgradePreferences(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  36. parser = configparser.ConfigParser(interpolation = None)
  37. parser.read_string(serialised)
  38. # Update version numbers
  39. if "general" not in parser:
  40. parser["general"] = {}
  41. parser["general"]["version"] = "5"
  42. if "metadata" not in parser:
  43. parser["metadata"] = {}
  44. parser["metadata"]["setting_version"] = "3"
  45. #Renamed themes.
  46. if "theme" in parser["general"]:
  47. if parser["general"]["theme"] in _renamed_themes:
  48. parser["general"]["theme"] = _renamed_themes[parser["general"]["theme"]]
  49. #Renamed languages.
  50. if "language" in parser["general"]:
  51. if parser["general"]["language"] in _renamed_i18n:
  52. parser["general"]["language"] = _renamed_i18n[parser["general"]["language"]]
  53. # Renamed settings for skin pre-shrink settings
  54. if parser.has_section("general") and "visible_settings" in parser["general"]:
  55. visible_settings = parser["general"]["visible_settings"].split(";")
  56. new_visible_settings = []
  57. renamed_skin_preshrink_names = {"expand_upper_skins": "top_skin_expand_distance",
  58. "expand_lower_skins": "bottom_skin_expand_distance"}
  59. for setting in visible_settings:
  60. if setting == "expand_skins_into_infill":
  61. continue # this one is removed
  62. if setting in renamed_skin_preshrink_names:
  63. new_visible_settings.append(renamed_skin_preshrink_names[setting])
  64. continue #Don't add the original.
  65. new_visible_settings.append(setting) #No special handling, so just add the original visible setting back.
  66. parser["general"]["visible_settings"] = ";".join(new_visible_settings)
  67. # Re-serialise the file.
  68. output = io.StringIO()
  69. parser.write(output)
  70. return [filename], [output.getvalue()]
  71. ## Upgrades the given quality changes container file from version 2.7 to 3.0.
  72. #
  73. # \param serialised The serialised form of the container file.
  74. # \param filename The name of the file to upgrade.
  75. def upgradeQualityChangesContainer(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  76. parser = configparser.ConfigParser(interpolation = None)
  77. parser.read_string(serialised)
  78. # Update the skin pre-shrink settings:
  79. # - Remove the old ones
  80. # - Do not add the new ones. The default values will be used for them.
  81. if parser.has_section("values"):
  82. for remove_key in ["expand_skins_into_infill", "expand_upper_skins", "expand_lower_skins"]:
  83. if remove_key in parser["values"]:
  84. del parser["values"][remove_key]
  85. for each_section in ("general", "metadata"):
  86. if not parser.has_section(each_section):
  87. parser.add_section(each_section)
  88. # Set the definition to "ultimaker2" for Ultimaker 2 quality changes
  89. if not parser.has_section("general"):
  90. parser.add_section("general")
  91. # Clean up the filename
  92. file_base_name = os.path.basename(filename)
  93. file_base_name = urllib.parse.unquote_plus(file_base_name)
  94. um2_pattern = re.compile(r"^ultimaker[^a-zA-Z\\d\\s:]2_.*$")
  95. # The ultimaker 2 family
  96. ultimaker2_prefix_list = ["ultimaker2_extended_",
  97. "ultimaker2_go_",
  98. "ultimaker2_"]
  99. # ultimaker 2+ is a different family, so don't do anything with those
  100. exclude_prefix_list = ["ultimaker2_extended_plus_",
  101. "ultimaker2_plus_"]
  102. # set machine definition to "ultimaker2" for the custom quality profiles that can be for the ultimaker 2 family
  103. is_ultimaker2_family = um2_pattern.match(file_base_name) is not None
  104. if not is_ultimaker2_family and not any(file_base_name.startswith(ep) for ep in exclude_prefix_list):
  105. is_ultimaker2_family = any(file_base_name.startswith(ep) for ep in ultimaker2_prefix_list)
  106. # ultimaker2 family quality profiles used to set as "fdmprinter" profiles
  107. if is_ultimaker2_family and parser["general"]["definition"] == "fdmprinter":
  108. parser["general"]["definition"] = "ultimaker2"
  109. # Update version numbers
  110. parser["general"]["version"] = "2"
  111. parser["metadata"]["setting_version"] = "3"
  112. # Re-serialise the file.
  113. output = io.StringIO()
  114. parser.write(output)
  115. return [filename], [output.getvalue()]
  116. ## Upgrades the given instance container file from version 2.7 to 3.0.
  117. #
  118. # \param serialised The serialised form of the container file.
  119. # \param filename The name of the file to upgrade.
  120. def upgradeOtherContainer(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  121. parser = configparser.ConfigParser(interpolation = None)
  122. parser.read_string(serialised)
  123. # Update the skin pre-shrink settings:
  124. # - Remove the old ones
  125. # - Do not add the new ones. The default values will be used for them.
  126. if parser.has_section("values"):
  127. for remove_key in ["expand_skins_into_infill", "expand_upper_skins", "expand_lower_skins"]:
  128. if remove_key in parser["values"]:
  129. del parser["values"][remove_key]
  130. for each_section in ("general", "metadata"):
  131. if not parser.has_section(each_section):
  132. parser.add_section(each_section)
  133. # Update version numbers
  134. parser["general"]["version"] = "2"
  135. parser["metadata"]["setting_version"] = "3"
  136. # Re-serialise the file.
  137. output = io.StringIO()
  138. parser.write(output)
  139. return [filename], [output.getvalue()]
  140. ## Upgrades a container stack from version 2.7 to 3.0.
  141. #
  142. # \param serialised The serialised form of a container stack.
  143. # \param filename The name of the file to upgrade.
  144. def upgradeStack(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  145. parser = configparser.ConfigParser(interpolation=None)
  146. parser.read_string(serialised)
  147. for each_section in ("general", "metadata"):
  148. if not parser.has_section(each_section):
  149. parser.add_section(each_section)
  150. # Update version numbers
  151. if "general" not in parser:
  152. parser["general"] = {}
  153. parser["general"]["version"] = "3"
  154. if "metadata" not in parser:
  155. parser["metadata"] = {}
  156. parser["metadata"]["setting_version"] = "3"
  157. # Re-serialise the file.
  158. output = io.StringIO()
  159. parser.write(output)
  160. return [filename], [output.getvalue()]