load.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.buttons = []
  22. self.columns = 0
  23. self.rows = 0
  24. self.padding = 0
  25. self.spacing = 0
  26. self.borderless = False
  27. def get_config(self) -> Config | str:
  28. try:
  29. with open(self.config_path, "r", encoding="utf-8") as config_reader:
  30. yaml_config = yaml.safe_load(config_reader)
  31. self.columns = yaml_config.get("columns")
  32. self.rows = yaml_config.get("rows")
  33. self.buttons = yaml_config.get("buttons")
  34. self.padding = yaml_config.get("padding", 5)
  35. self.spacing = yaml_config.get("spacing", 5)
  36. self.borderless = yaml_config.get("borderless", False)
  37. return self.__interpret_config()
  38. except (FileNotFoundError, PermissionError, IOError) as error:
  39. return f"Error: Could not access config file at {self.config_path}. Reason: {error}"
  40. except (yaml.YAMLError, CustomException) as error:
  41. return f"Error parsing config file. Reason: {error}"
  42. def __interpret_config(self) -> Config:
  43. self.__validate_dimensions()
  44. self.__validate_buttons()
  45. self.__validate_styling()
  46. return Config(
  47. self.columns,
  48. self.rows,
  49. self.buttons,
  50. self.spacing,
  51. self.padding,
  52. self.borderless,
  53. )
  54. def __validate_buttons(self) -> None:
  55. if not isinstance(self.buttons, list):
  56. raise CustomException(
  57. "invalid button config. needs to be a list of dicts."
  58. )
  59. for button in self.buttons:
  60. if not isinstance(button, dict):
  61. raise CustomException(
  62. "invalid button config. needs to be a list of dicts."
  63. )
  64. if (
  65. not isinstance(dimensions := button.get("position", ""), list)
  66. or (not isinstance(dimensions[0], int))
  67. or (not isinstance(dimensions[1], int))
  68. or (0 > dimensions[0] or dimensions[0] > self.rows - 1)
  69. or (0 > dimensions[1] or dimensions[1] > self.columns - 1)
  70. ):
  71. raise CustomException(
  72. f"invalid button 'position' subentry: '{dimensions}'"
  73. )
  74. for entry in ("txt", "cmd"):
  75. if not isinstance(result := button.get(entry, ""), str):
  76. raise CustomException(
  77. f"invalid button '{entry}' subentry: '{result}'"
  78. )
  79. if not isinstance(
  80. bg_color := button.get("bg_color", "cccccc"), str
  81. ) or not is_valid_hexcolor(bg_color):
  82. raise CustomException(
  83. f"invalid button 'bg_color' subentry: '{bg_color}'"
  84. )
  85. if not isinstance(
  86. fg_color := button.get("fg_color", "ffffff"), str
  87. ) or not is_valid_hexcolor(bg_color):
  88. raise CustomException(
  89. f"invalid button 'fg_color' subentry: '{fg_color}'"
  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}")
  99. if not isinstance(self.borderless, bool):
  100. raise CustomException("invalid borderless value, should be boolean")