VersionUpgrade33to34.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. from typing import Dict, List, Tuple
  6. from UM.VersionUpgrade import VersionUpgrade #We're inheriting from this.
  7. _renamed_settings = {
  8. "infill_hollow": "infill_support_enabled"
  9. } # type: Dict[str, str]
  10. ## Upgrades configurations from the state they were in at version 3.3 to the
  11. # state they should be in at version 3.4.
  12. class VersionUpgrade33to34(VersionUpgrade):
  13. ## Gets the version number from a CFG file in Uranium's 3.3 format.
  14. #
  15. # Since the format may change, this is implemented for the 3.3 format only
  16. # and needs to be included in the version upgrade system rather than
  17. # globally in Uranium.
  18. #
  19. # \param serialised The serialised form of a CFG file.
  20. # \return The version number stored in the CFG file.
  21. # \raises ValueError The format of the version number in the file is
  22. # incorrect.
  23. # \raises KeyError The format of the file is incorrect.
  24. def getCfgVersion(self, serialised: str) -> int:
  25. parser = configparser.ConfigParser(interpolation = None)
  26. parser.read_string(serialised)
  27. format_version = int(parser.get("general", "version")) #Explicitly give an exception when this fails. That means that the file format is not recognised.
  28. setting_version = int(parser.get("metadata", "setting_version", fallback = "0"))
  29. return format_version * 1000000 + setting_version
  30. ## Upgrades instance containers to have the new version
  31. # number.
  32. def upgradeInstanceContainer(self, serialized: str, filename: str) -> Tuple[List[str], List[str]]:
  33. parser = configparser.ConfigParser(interpolation = None)
  34. parser.read_string(serialized)
  35. # Update version number.
  36. parser["general"]["version"] = "4"
  37. if "values" in parser:
  38. #If infill_hollow was enabled and the overhang angle was adjusted, copy that overhang angle to the new infill support angle.
  39. if "infill_hollow" in parser["values"] and parser["values"]["infill_hollow"] and "support_angle" in parser["values"]:
  40. parser["values"]["infill_support_angle"] = parser["values"]["support_angle"]
  41. #Renamed settings.
  42. for original, replacement in _renamed_settings.items():
  43. if original in parser["values"]:
  44. parser["values"][replacement] = parser["values"][original]
  45. del parser["values"][original]
  46. result = io.StringIO()
  47. parser.write(result)
  48. return [filename], [result.getvalue()]