VersionUpgrade27to30.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. ## Gets the version number from a CFG file in Uranium's 2.7 format.
  32. #
  33. # Since the format may change, this is implemented for the 2.7 format only
  34. # and needs to be included in the version upgrade system rather than
  35. # globally in Uranium.
  36. #
  37. # \param serialised The serialised form of a CFG file.
  38. # \return The version number stored in the CFG file.
  39. # \raises ValueError The format of the version number in the file is
  40. # incorrect.
  41. # \raises KeyError The format of the file is incorrect.
  42. def getCfgVersion(self, serialised: str) -> int:
  43. parser = configparser.ConfigParser(interpolation = None)
  44. parser.read_string(serialised)
  45. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  46. setting_version = int(parser.get("metadata", "setting_version", fallback = "0"))
  47. return format_version * 1000000 + setting_version
  48. ## Upgrades a preferences file from version 2.7 to 3.0.
  49. #
  50. # \param serialised The serialised form of a preferences file.
  51. # \param filename The name of the file to upgrade.
  52. def upgradePreferences(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  53. parser = configparser.ConfigParser(interpolation = None)
  54. parser.read_string(serialised)
  55. # Update version numbers
  56. if "general" not in parser:
  57. parser["general"] = {}
  58. parser["general"]["version"] = "5"
  59. if "metadata" not in parser:
  60. parser["metadata"] = {}
  61. parser["metadata"]["setting_version"] = "3"
  62. #Renamed themes.
  63. if "theme" in parser["general"]:
  64. if parser["general"]["theme"] in _renamed_themes:
  65. parser["general"]["theme"] = _renamed_themes[parser["general"]["theme"]]
  66. #Renamed languages.
  67. if "language" in parser["general"]:
  68. if parser["general"]["language"] in _renamed_i18n:
  69. parser["general"]["language"] = _renamed_i18n[parser["general"]["language"]]
  70. # Renamed settings for skin pre-shrink settings
  71. if parser.has_section("general") and "visible_settings" in parser["general"]:
  72. visible_settings = parser["general"]["visible_settings"].split(";")
  73. new_visible_settings = []
  74. renamed_skin_preshrink_names = {"expand_upper_skins": "top_skin_expand_distance",
  75. "expand_lower_skins": "bottom_skin_expand_distance"}
  76. for setting in visible_settings:
  77. if setting == "expand_skins_into_infill":
  78. continue # this one is removed
  79. if setting in renamed_skin_preshrink_names:
  80. new_visible_settings.append(renamed_skin_preshrink_names[setting])
  81. continue #Don't add the original.
  82. new_visible_settings.append(setting) #No special handling, so just add the original visible setting back.
  83. parser["general"]["visible_settings"] = ";".join(new_visible_settings)
  84. # Re-serialise the file.
  85. output = io.StringIO()
  86. parser.write(output)
  87. return [filename], [output.getvalue()]
  88. ## Upgrades the given quality changes container file from version 2.7 to 3.0.
  89. #
  90. # \param serialised The serialised form of the container file.
  91. # \param filename The name of the file to upgrade.
  92. def upgradeQualityChangesContainer(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  93. parser = configparser.ConfigParser(interpolation = None)
  94. parser.read_string(serialised)
  95. # Update the skin pre-shrink settings:
  96. # - Remove the old ones
  97. # - Do not add the new ones. The default values will be used for them.
  98. if parser.has_section("values"):
  99. for remove_key in ["expand_skins_into_infill", "expand_upper_skins", "expand_lower_skins"]:
  100. if remove_key in parser["values"]:
  101. del parser["values"][remove_key]
  102. for each_section in ("general", "metadata"):
  103. if not parser.has_section(each_section):
  104. parser.add_section(each_section)
  105. # Set the definition to "ultimaker2" for Ultimaker 2 quality changes
  106. if not parser.has_section("general"):
  107. parser.add_section("general")
  108. # Clean up the filename
  109. file_base_name = os.path.basename(filename)
  110. file_base_name = urllib.parse.unquote_plus(file_base_name)
  111. um2_pattern = re.compile(r"^ultimaker[^a-zA-Z\\d\\s:]2_.*$")
  112. # The ultimaker 2 family
  113. ultimaker2_prefix_list = ["ultimaker2_extended_",
  114. "ultimaker2_go_",
  115. "ultimaker2_"]
  116. # ultimaker 2+ is a different family, so don't do anything with those
  117. exclude_prefix_list = ["ultimaker2_extended_plus_",
  118. "ultimaker2_plus_"]
  119. # set machine definition to "ultimaker2" for the custom quality profiles that can be for the ultimaker 2 family
  120. is_ultimaker2_family = um2_pattern.match(file_base_name) is not None
  121. if not is_ultimaker2_family and not any(file_base_name.startswith(ep) for ep in exclude_prefix_list):
  122. is_ultimaker2_family = any(file_base_name.startswith(ep) for ep in ultimaker2_prefix_list)
  123. # ultimaker2 family quality profiles used to set as "fdmprinter" profiles
  124. if is_ultimaker2_family and parser["general"]["definition"] == "fdmprinter":
  125. parser["general"]["definition"] = "ultimaker2"
  126. # Update version numbers
  127. parser["general"]["version"] = "2"
  128. parser["metadata"]["setting_version"] = "3"
  129. # Re-serialise the file.
  130. output = io.StringIO()
  131. parser.write(output)
  132. return [filename], [output.getvalue()]
  133. ## Upgrades the given instance container file from version 2.7 to 3.0.
  134. #
  135. # \param serialised The serialised form of the container file.
  136. # \param filename The name of the file to upgrade.
  137. def upgradeOtherContainer(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  138. parser = configparser.ConfigParser(interpolation = None)
  139. parser.read_string(serialised)
  140. # Update the skin pre-shrink settings:
  141. # - Remove the old ones
  142. # - Do not add the new ones. The default values will be used for them.
  143. if parser.has_section("values"):
  144. for remove_key in ["expand_skins_into_infill", "expand_upper_skins", "expand_lower_skins"]:
  145. if remove_key in parser["values"]:
  146. del parser["values"][remove_key]
  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. parser["general"]["version"] = "2"
  152. parser["metadata"]["setting_version"] = "3"
  153. # Re-serialise the file.
  154. output = io.StringIO()
  155. parser.write(output)
  156. return [filename], [output.getvalue()]
  157. ## Upgrades a container stack from version 2.7 to 3.0.
  158. #
  159. # \param serialised The serialised form of a container stack.
  160. # \param filename The name of the file to upgrade.
  161. def upgradeStack(self, serialised: str, filename: str) -> Tuple[List[str], List[str]]:
  162. parser = configparser.ConfigParser(interpolation=None)
  163. parser.read_string(serialised)
  164. for each_section in ("general", "metadata"):
  165. if not parser.has_section(each_section):
  166. parser.add_section(each_section)
  167. # Update version numbers
  168. if "general" not in parser:
  169. parser["general"] = {}
  170. parser["general"]["version"] = "3"
  171. if "metadata" not in parser:
  172. parser["metadata"] = {}
  173. parser["metadata"]["setting_version"] = "3"
  174. # Re-serialise the file.
  175. output = io.StringIO()
  176. parser.write(output)
  177. return [filename], [output.getvalue()]