VersionUpgrade21to22.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import configparser #To get version numbers from config files.
  4. from UM.VersionUpgrade import VersionUpgrade # Superclass of the plugin.
  5. from . import MachineInstance # To upgrade machine instances.
  6. from . import Preferences #To upgrade preferences.
  7. from . import Profile # To upgrade profiles.
  8. ## How to translate printer names from the old version to the new.
  9. _printer_translation = {
  10. "ultimaker2plus": "ultimaker2_plus"
  11. }
  12. ## How to translate profile names from the old version to the new.
  13. _profile_translation = {
  14. "PLA": "generic_pla",
  15. "ABS": "generic_abs",
  16. "CPE": "generic_cpe"
  17. }
  18. ## How to translate setting names from the old version to the new.
  19. _setting_name_translation = {
  20. "speed_support_lines": "speed_support_infill"
  21. }
  22. ## How to translate variants of specific machines from the old version to the
  23. # new.
  24. _variant_translation = {
  25. "ultimaker2_plus": {
  26. "0.25 mm": "ultimaker2_plus_0.25",
  27. "0.4 mm": "ultimaker2_plus_0.4",
  28. "0.6 mm": "ultimaker2_plus_0.6",
  29. "0.8 mm": "ultimaker2_plus_0.8"
  30. },
  31. "ultimaker2_extended_plus": {
  32. "0.25 mm": "ultimaker2_extended_plus_0.25",
  33. "0.4 mm": "ultimaker2_extended_plus_0.4",
  34. "0.6 mm": "ultimaker2_extended_plus_0.6",
  35. "0.8 mm": "ultimaker2_extended_plus_0.8"
  36. }
  37. }
  38. ## Converts configuration from Cura 2.1's file formats to Cura 2.2's.
  39. #
  40. # It converts the machine instances and profiles.
  41. class VersionUpgrade21to22(VersionUpgrade):
  42. ## Gets the version number from a config file.
  43. #
  44. # In all config files that concern this version upgrade, the version
  45. # number is stored in general/version, so get the data from that key.
  46. #
  47. # \param serialised The contents of a config file.
  48. # \return \type{int} The version number of that config file.
  49. def getCfgVersion(self, serialised):
  50. parser = configparser.ConfigParser(interpolation = None)
  51. parser.read_string(serialised)
  52. return int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  53. ## Converts machine instances from format version 1 to version 2.
  54. #
  55. # \param serialised The serialised machine instance in version 1.
  56. # \return The serialised machine instance in version 2, or None if the
  57. # input was not of the correct format.
  58. def upgradeMachineInstance(self, serialised):
  59. machine_instance = MachineInstance.importFrom(serialised)
  60. if not machine_instance: #Invalid file format.
  61. return None
  62. return machine_instance.export()
  63. ## Converts preferences from format version 2 to version 3.
  64. #
  65. # \param serialised The serialised preferences file in version 2.
  66. # \return The serialised preferences in version 3, or None if the input
  67. # was not of the correct format.
  68. def upgradePreferences(self, serialised):
  69. preferences = Preferences.importFrom(serialised)
  70. if not preferences: #Invalid file format.
  71. return None
  72. return preferences.export()
  73. ## Converts profiles from format version 1 to version 2.
  74. #
  75. # \param serialised The serialised profile in version 1.
  76. # \return The serialised profile in version 2, or None if the input was
  77. # not of the correct format.
  78. def upgradeProfile(self, serialised):
  79. profile = Profile.importFrom(serialised)
  80. if not profile: # Invalid file format.
  81. return None
  82. return profile.export()
  83. ## Translates a printer name that might have changed since the last
  84. # version.
  85. #
  86. # \param printer A printer name in Cura 2.1.
  87. # \return The name of the corresponding printer in Cura 2.2.
  88. @staticmethod
  89. def translatePrinter(printer):
  90. if printer in _printer_translation:
  91. return _printer_translation[printer]
  92. return printer #Doesn't need to be translated.
  93. ## Translates a built-in profile name that might have changed since the
  94. # last version.
  95. #
  96. # \param profile A profile name in the old version.
  97. # \return The corresponding profile name in the new version.
  98. @staticmethod
  99. def translateProfile(profile):
  100. if profile in _profile_translation:
  101. return _profile_translation[profile]
  102. return profile #Doesn't need to be translated.
  103. ## Updates settings for the change from Cura 2.1 to 2.2.
  104. #
  105. # The keys and values of settings are changed to what they should be in
  106. # the new version. Each setting is changed in-place in the provided
  107. # dictionary. This changes the input parameter.
  108. #
  109. # \param settings A dictionary of settings (as key-value pairs) to update.
  110. # \return The same dictionary.
  111. @staticmethod
  112. def translateSettings(settings):
  113. for key, value in settings.items():
  114. if key == "speed_support_lines": # Setting key was changed for 2.2.
  115. del settings[key]
  116. settings["speed_support_infill"] = value
  117. if key == "retraction_combing": # Combing was made into an enum instead of a boolean.
  118. settings[key] = "off" if (value == "False") else "all"
  119. return settings
  120. ## Translates a setting name for the change from Cura 2.1 to 2.2.
  121. #
  122. # \param setting The name of a setting in Cura 2.1.
  123. # \return The name of the corresponding setting in Cura 2.2.
  124. @staticmethod
  125. def translateSettingName(setting):
  126. if setting in _setting_name_translation:
  127. return _setting_name_translation[setting]
  128. return setting #Doesn't need to be translated.
  129. ## Translates a variant name for the change from Cura 2.1 to 2.2
  130. #
  131. # \param variant The name of a variant in Cura 2.1.
  132. # \param machine The name of the machine this variant is part of in Cura
  133. # 2.2's naming.
  134. # \return The name of the corresponding variant in Cura 2.2.
  135. @staticmethod
  136. def translateVariant(variant, machine):
  137. if machine in _variant_translation and variant in _variant_translation[machine]:
  138. return _variant_translation[machine][variant]
  139. return variant