TextManager.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. file_path = Resources.getPath(Resources.Texts, "change_log.txt")
  23. change_logs_dict = {} # type: Dict[Version, Dict[str, List[str]]]
  24. with open(file_path, "r", encoding = "utf-8") as f:
  25. open_version = None # type: Optional[Version]
  26. open_header = "" # Initialise to an empty header in case there is no "*" in the first line of the changelog
  27. for line in f:
  28. line = line.replace("\n", "")
  29. if "[" in line and "]" in line:
  30. line = line.replace("[", "")
  31. line = line.replace("]", "")
  32. open_version = Version(line)
  33. if open_version > Version([14, 99, 99]): # Bit of a hack: We released the 15.x.x versions before 2.x
  34. open_version = Version([0, open_version.getMinor(), open_version.getRevision(), open_version.getPostfixVersion()])
  35. open_header = ""
  36. change_logs_dict[open_version] = collections.OrderedDict()
  37. elif line.startswith("*"):
  38. open_header = line.replace("*", "")
  39. change_logs_dict[cast(Version, open_version)][open_header] = []
  40. elif line != "":
  41. if open_header not in change_logs_dict[cast(Version, open_version)]:
  42. change_logs_dict[cast(Version, open_version)][open_header] = []
  43. change_logs_dict[cast(Version, open_version)][open_header].append(line)
  44. # Format changelog text
  45. content = ""
  46. for version in sorted(change_logs_dict.keys(), reverse = True):
  47. text_version = version
  48. if version < Version([1, 0, 0]): # Bit of a hack: We released the 15.x.x versions before 2.x
  49. text_version = Version([15, version.getMinor(), version.getRevision(), version.getPostfixVersion()])
  50. content += "<h1>" + str(text_version) + "</h1><br>"
  51. content += ""
  52. for change in change_logs_dict[version]:
  53. if str(change) != "":
  54. content += "<b>" + str(change) + "</b><br>"
  55. for line in change_logs_dict[version][change]:
  56. content += str(line) + "<br>"
  57. content += "<br>"
  58. return content