TextManager.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (c) 2019 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 PyQt5.QtCore import QObject, pyqtSlot
  6. from UM.Resources import Resources
  7. from UM.Version import Version
  8. #
  9. # This manager provides means to load texts to QML.
  10. #
  11. class TextManager(QObject):
  12. def __init__(self, parent: Optional["QObject"] = None) -> None:
  13. super().__init__(parent)
  14. self._change_log_text = ""
  15. @pyqtSlot(result = str)
  16. def getChangeLogText(self) -> str:
  17. if not self._change_log_text:
  18. self._change_log_text = self._loadChangeLogText()
  19. return self._change_log_text
  20. def _loadChangeLogText(self) -> str:
  21. # Load change log texts and organize them with a dict
  22. try:
  23. file_path = Resources.getPath(Resources.Texts, "change_log.txt")
  24. except FileNotFoundError:
  25. # I have no idea how / when this happens, but we're getting crash reports about it.
  26. return ""
  27. change_logs_dict = {} # type: Dict[Version, Dict[str, List[str]]]
  28. with open(file_path, "r", encoding = "utf-8") as f:
  29. open_version = None # type: Optional[Version]
  30. open_header = "" # Initialise to an empty header in case there is no "*" in the first line of the changelog
  31. for line in f:
  32. line = line.replace("\n", "")
  33. if "[" in line and "]" in line:
  34. line = line.replace("[", "")
  35. line = line.replace("]", "")
  36. open_version = Version(line)
  37. if open_version > Version([14, 99, 99]): # Bit of a hack: We released the 15.x.x versions before 2.x
  38. open_version = Version([0, open_version.getMinor(), open_version.getRevision(), open_version.getPostfixVersion()])
  39. open_header = ""
  40. change_logs_dict[open_version] = collections.OrderedDict()
  41. elif line.startswith("*"):
  42. open_header = line.replace("*", "")
  43. change_logs_dict[cast(Version, open_version)][open_header] = []
  44. elif line != "":
  45. if open_header not in change_logs_dict[cast(Version, open_version)]:
  46. change_logs_dict[cast(Version, open_version)][open_header] = []
  47. change_logs_dict[cast(Version, open_version)][open_header].append(line)
  48. # Format changelog text
  49. content = ""
  50. for version in sorted(change_logs_dict.keys(), reverse = True):
  51. text_version = version
  52. if version < Version([1, 0, 0]): # Bit of a hack: We released the 15.x.x versions before 2.x
  53. text_version = Version([15, version.getMinor(), version.getRevision(), version.getPostfixVersion()])
  54. content += "<h1>" + str(text_version) + "</h1><br>"
  55. content += ""
  56. for change in change_logs_dict[version]:
  57. if str(change) != "":
  58. content += "<b>" + str(change) + "</b><br>"
  59. for line in change_logs_dict[version][change]:
  60. content += str(line) + "<br>"
  61. content += "<br>"
  62. return content