1
0

__init__.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import argparse
  2. import pathlib
  3. import acpi_backlight.evaluate
  4. _ACPI_BACKLIGHT_ROOT_DIR_PATH = pathlib.Path("/sys/class/backlight")
  5. class Backlight:
  6. # pylint: disable=too-few-public-methods; does not count properties
  7. def __init__(self, name: str = "intel_backlight"):
  8. self._device_name = name
  9. self._acpi_dir_path = _ACPI_BACKLIGHT_ROOT_DIR_PATH.joinpath(name)
  10. @property
  11. def _brightness_path(self) -> pathlib.Path:
  12. return self._acpi_dir_path.joinpath("brightness")
  13. @property
  14. def _max_brightness_path(self) -> pathlib.Path:
  15. return self._acpi_dir_path.joinpath("max_brightness")
  16. @property
  17. def _brightness_absolute(self) -> int:
  18. return int(self._brightness_path.read_text(encoding="ascii"))
  19. @_brightness_absolute.setter
  20. def _brightness_absolute(self, brightness_absolute: int):
  21. try:
  22. self._brightness_path.write_text(str(brightness_absolute), encoding="ascii")
  23. except PermissionError as exc:
  24. raise PermissionError(
  25. "Insufficient permissions to set brightness of backlight."
  26. "\nConsider adding the following udev rules:"
  27. f'\nACTION=="add", SUBSYSTEM=="backlight"'
  28. f', KERNEL=="{self._device_name}"'
  29. f', RUN+="/bin/chgrp video /sys$devpath/brightness"'
  30. f'\nACTION=="add", SUBSYSTEM=="backlight"'
  31. f', KERNEL=="{self._device_name}"'
  32. f', RUN+="/bin/chmod g+w /sys$devpath/brightness"'
  33. ) from exc
  34. @property
  35. def _max_brightness_absolute(self) -> int:
  36. return int(self._max_brightness_path.read_text(encoding="ascii"))
  37. @property
  38. def brightness_relative(self) -> float:
  39. return self._brightness_absolute / self._max_brightness_absolute
  40. @brightness_relative.setter
  41. def brightness_relative(self, brightness_relative: float) -> None:
  42. self._brightness_absolute = round(
  43. max(0, min(1, brightness_relative)) * self._max_brightness_absolute
  44. )
  45. def backlight_eval(expr_str: str) -> None:
  46. backlight = acpi_backlight.Backlight()
  47. backlight.brightness_relative = acpi_backlight.evaluate.evaluate_expression(
  48. expr_str=expr_str, names={"b": backlight.brightness_relative}
  49. )
  50. print(backlight.brightness_relative)
  51. def _main() -> None:
  52. argparser = argparse.ArgumentParser()
  53. argparser.add_argument("expr_str")
  54. args = argparser.parse_args()
  55. backlight_eval(expr_str=args.expr_str)