TextManager.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. open_header = ""
  34. change_logs_dict[open_version] = collections.OrderedDict()
  35. elif line.startswith("*"):
  36. open_header = line.replace("*", "")
  37. change_logs_dict[cast(Version, open_version)][open_header] = []
  38. elif line != "":
  39. if open_header not in change_logs_dict[cast(Version, open_version)]:
  40. change_logs_dict[cast(Version, open_version)][open_header] = []
  41. change_logs_dict[cast(Version, open_version)][open_header].append(line)
  42. # Format changelog text
  43. content = ""
  44. for version in change_logs_dict:
  45. content += "<h1>" + str(version) + "</h1><br>"
  46. content += ""
  47. for change in change_logs_dict[version]:
  48. if str(change) != "":
  49. content += "<b>" + str(change) + "</b><br>"
  50. for line in change_logs_dict[version][change]:
  51. content += str(line) + "<br>"
  52. content += "<br>"
  53. return content