TextManager.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import collections
  4. from typing import Optional, Dict, List, cast
  5. from PyQt6.QtCore import QObject, pyqtSlot
  6. from UM.i18n import i18nCatalog
  7. from UM.Resources import Resources
  8. from UM.Version import Version
  9. catalog = i18nCatalog("cura")
  10. #
  11. # This manager provides means to load texts to QML.
  12. #
  13. class TextManager(QObject):
  14. def __init__(self, parent: Optional["QObject"] = None) -> None:
  15. super().__init__(parent)
  16. self._change_log_text = ""
  17. @pyqtSlot(result = str)
  18. def getChangeLogText(self) -> str:
  19. if not self._change_log_text:
  20. self._change_log_text = self._loadChangeLogText()
  21. return self._change_log_text
  22. def _loadChangeLogText(self) -> str:
  23. # Load change log texts and organize them with a dict
  24. try:
  25. file_path = Resources.getPath(Resources.Texts, "change_log.txt")
  26. except FileNotFoundError as e:
  27. # I have no idea how / when this happens, but we're getting crash reports about it.
  28. return catalog.i18nc("@text:window", "The release notes could not be opened.") + "<br>" + str(e)
  29. change_logs_dict = {} # type: Dict[Version, Dict[str, List[str]]]
  30. try:
  31. with open(file_path, "r", encoding = "utf-8") as f:
  32. open_version = None # type: Optional[Version]
  33. open_header = "" # Initialise to an empty header in case there is no "*" in the first line of the changelog
  34. for line in f:
  35. line = line.replace("\n", "")
  36. if "[" in line and "]" in line:
  37. line = line.replace("[", "")
  38. line = line.replace("]", "")
  39. open_version = Version(line)
  40. if open_version < Version([0, 0, 1]): # Something went wrong with parsing, assume non-numerical alternate version that should be on top.
  41. open_version = Version([99, 99, 99])
  42. if Version([14, 99, 99]) < open_version < Version([16, 0, 0]): # Bit of a hack: We released the 15.x.x versions before 2.x
  43. open_version = Version([0, open_version.getMinor(), open_version.getRevision(), open_version.getPostfixVersion()])
  44. open_header = ""
  45. change_logs_dict[open_version] = collections.OrderedDict()
  46. elif line.startswith("*"):
  47. open_header = line.replace("*", "")
  48. change_logs_dict[cast(Version, open_version)][open_header] = []
  49. elif line != "":
  50. if open_header not in change_logs_dict[cast(Version, open_version)]:
  51. change_logs_dict[cast(Version, open_version)][open_header] = []
  52. change_logs_dict[cast(Version, open_version)][open_header].append(line)
  53. except EnvironmentError as e:
  54. return catalog.i18nc("@text:window", "The release notes could not be opened.") + "<br>" + str(e)
  55. # Format changelog text
  56. content = ""
  57. for version in sorted(change_logs_dict.keys(), reverse = True):
  58. text_version = version
  59. if version < Version([1, 0, 0]): # Bit of a hack: We released the 15.x.x versions before 2.x
  60. text_version = Version([15, version.getMinor(), version.getRevision(), version.getPostfixVersion()])
  61. if version > Version([99, 0, 0]): # Leave it out altogether if it was originally a non-numbered version.
  62. text_version = ""
  63. content += ("<h1>" + str(text_version) + "</h1><br>") if text_version else ""
  64. content += ""
  65. for change in change_logs_dict[version]:
  66. if str(change) != "":
  67. content += "<b>" + str(change) + "</b><br>"
  68. for line in change_logs_dict[version][change]:
  69. content += str(line) + "<br>"
  70. content += "<br>"
  71. return content