load.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. from dataclasses import dataclass
  13. import yaml
  14. from util import CustomException
  15. from .classes import Config
  16. from .validate import is_valid_hexcolor
  17. @dataclass
  18. class ConfigLoader:
  19. config_path: str
  20. def __post_init__(self) -> None:
  21. self.columns = 0
  22. self.rows = 0
  23. self.buttons = []
  24. self.padding = 0
  25. self.spacing = 0
  26. def get_config(self) -> Config | str:
  27. try:
  28. with open(self.config_path, "r", encoding="utf-8") as config_reader:
  29. yaml_config = yaml.safe_load(config_reader)
  30. self.columns = yaml_config.get("columns")
  31. self.rows = yaml_config.get("rows")
  32. self.buttons = yaml_config.get("buttons")
  33. self.padding = yaml_config.get("padding", 5)
  34. self.spacing = yaml_config.get("spacing", 5)
  35. return self.__interpret_config()
  36. except (FileNotFoundError, PermissionError, IOError) as error:
  37. return f"Error: Could not access config file at {self.config_path}. Reason: {error}"
  38. except (yaml.YAMLError, CustomException) as error:
  39. return f"Error parsing config file. Reason: {error}"
  40. def __interpret_config(self) -> Config:
  41. self.__validate_dimensions()
  42. self.__validate_buttons()
  43. self.__validate_styling()
  44. return Config(
  45. self.columns, self.rows, self.buttons, self.spacing, self.padding
  46. )
  47. def __validate_buttons(self) -> None:
  48. if not isinstance(self.buttons, list):
  49. raise CustomException(
  50. "invalid button config. needs to be a list of dicts."
  51. )
  52. for button in self.buttons:
  53. if not isinstance(button, dict):
  54. raise CustomException(
  55. "invalid button config. needs to be a list of dicts."
  56. )
  57. if (
  58. not isinstance(dimensions := button.get("position", ""), list)
  59. or (not isinstance(dimensions[0], int))
  60. or (not isinstance(dimensions[1], int))
  61. or (0 > dimensions[0] or dimensions[0] > self.rows - 1)
  62. or (0 > dimensions[1] or dimensions[1] > self.columns - 1)
  63. ):
  64. raise CustomException(
  65. f"invalid button 'position' subentry: '{dimensions}'"
  66. )
  67. for entry in ("txt", "cmd"):
  68. if not isinstance(result := button.get(entry, ""), str):
  69. raise CustomException(
  70. f"invalid button '{entry}' subentry: '{result}'"
  71. )
  72. if not isinstance(
  73. bg_color := button.get("bg_color", "cccccc"), str
  74. ) or not is_valid_hexcolor(bg_color):
  75. raise CustomException(
  76. f"invalid button 'bg_color' subentry: '{bg_color}'"
  77. )
  78. if not isinstance(
  79. fg_color := button.get("fg_color", "ffffff"), str
  80. ) or not is_valid_hexcolor(bg_color):
  81. raise CustomException(
  82. f"invalid button 'fg_color' subentry: '{fg_color}'"
  83. )
  84. if (
  85. not isinstance(fontsize := button.get("fontsize", ""), int)
  86. or 0 > fontsize
  87. ):
  88. raise CustomException(
  89. f"invalid button 'fontsize' subentry: '{fontsize}'"
  90. )
  91. def __validate_dimensions(self) -> None:
  92. for dimension in (self.columns, self.rows):
  93. if not isinstance(dimension, int) or (dimension <= 0):
  94. raise CustomException(f"invalid dimension: {dimension}")
  95. def __validate_styling(self) -> None:
  96. for styling in (self.spacing, self.padding):
  97. if not isinstance(styling, int) or (styling <= 0):
  98. raise CustomException(f"invalid styling: {styling}")