__init__.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # pylint: disable=missing-docstring
  2. import argparse
  3. import os
  4. import acpi_backlight.evaluate
  5. _ACPI_BACKLIGHT_ROOT_DIR_PATH = "/sys/class/backlight"
  6. class Backlight:
  7. # pylint: disable=too-few-public-methods; does not count properties
  8. def __init__(self, name: str = "intel_backlight"):
  9. self._acpi_dir_path = os.path.join(_ACPI_BACKLIGHT_ROOT_DIR_PATH, name)
  10. @property
  11. def _brightness_path(self) -> str:
  12. return os.path.join(self._acpi_dir_path, "brightness")
  13. @property
  14. def _max_brightness_path(self) -> str:
  15. return os.path.join(self._acpi_dir_path, "max_brightness")
  16. @property
  17. def _brightness_absolute(self) -> int:
  18. with open(self._brightness_path, "r") as brightness_file:
  19. return int(brightness_file.read())
  20. @_brightness_absolute.setter
  21. def _brightness_absolute(self, brightness_absolute: int):
  22. with open(self._brightness_path, "w") as brightness_file:
  23. return brightness_file.write(str(brightness_absolute))
  24. @property
  25. def _max_brightness_absolute(self) -> int:
  26. with open(self._max_brightness_path, "r") as max_brightness_file:
  27. return int(max_brightness_file.read())
  28. @property
  29. def brightness_relative(self) -> float:
  30. return self._brightness_absolute / self._max_brightness_absolute
  31. @brightness_relative.setter
  32. def brightness_relative(self, brightness_relative: float) -> None:
  33. self._brightness_absolute = round(
  34. max(0, min(1, brightness_relative)) * self._max_brightness_absolute
  35. )
  36. def backlight_eval(expr_str: str) -> None:
  37. backlight = acpi_backlight.Backlight()
  38. backlight.brightness_relative = acpi_backlight.evaluate.evaluate_expression(
  39. expr_str=expr_str, names={"b": backlight.brightness_relative}
  40. )
  41. print(backlight.brightness_relative)
  42. def main() -> None:
  43. argparser = argparse.ArgumentParser()
  44. argparser.add_argument("expr_str")
  45. args = argparser.parse_args()
  46. backlight_eval(expr_str=args.expr_str)
  47. if __name__ == "__main__":
  48. main()