TextManager.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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([14, 99, 99]): # Bit of a hack: We released the 15.x.x versions before 2.x
  41. open_version = Version([0, open_version.getMinor(), open_version.getRevision(), open_version.getPostfixVersion()])
  42. open_header = ""
  43. change_logs_dict[open_version] = collections.OrderedDict()
  44. elif line.startswith("*"):
  45. open_header = line.replace("*", "")
  46. change_logs_dict[cast(Version, open_version)][open_header] = []
  47. elif line != "":
  48. if open_header not in change_logs_dict[cast(Version, open_version)]:
  49. change_logs_dict[cast(Version, open_version)][open_header] = []
  50. change_logs_dict[cast(Version, open_version)][open_header].append(line)
  51. except EnvironmentError as e:
  52. return catalog.i18nc("@text:window", "The release notes could not be opened.") + "<br>" + str(e)
  53. # Format changelog text
  54. content = ""
  55. for version in sorted(change_logs_dict.keys(), reverse = True):
  56. text_version = version
  57. if version < Version([1, 0, 0]): # Bit of a hack: We released the 15.x.x versions before 2.x
  58. text_version = Version([15, version.getMinor(), version.getRevision(), version.getPostfixVersion()])
  59. content += "<h1>" + str(text_version) + "</h1><br>"
  60. content += ""
  61. for change in change_logs_dict[version]:
  62. if str(change) != "":
  63. content += "<b>" + str(change) + "</b><br>"
  64. for line in change_logs_dict[version][change]:
  65. content += str(line) + "<br>"
  66. content += "<br>"
  67. return content