VersionUpgrade27to30.py 8.5 KB

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