configuration.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8-*-
  2. #
  3. # This file is part of stdd, the simple time display daemon,
  4. # written by Helmut Pozimski <helmut@pozimski.eu>,
  5. # licensed under the 3-clause BSD license
  6. import datetime
  7. import json
  8. class Conf(object):
  9. def __init__(self):
  10. """creates the object prepopulated with some reasonable default
  11. values
  12. """
  13. self.__values = {
  14. "hw_address": "",
  15. "blink_colon": "0",
  16. "brightness_high": "15",
  17. "brightness_low": "2",
  18. "set_brightness_low": "00:00",
  19. "set_brightness_high": "00:00",
  20. "syslog_level": "info",
  21. "syslog_facility": "user"
  22. }
  23. """ declare empty variables to write the checked values into """
  24. self.hw_address = 0
  25. self.blink_colon = False
  26. self.brightness_high = 0
  27. self.brightness_low = 0
  28. self.set_brightness_low = None
  29. self.set_brightness_high = None
  30. self.syslog_level = ""
  31. self.syslog_facility = ""
  32. def Read(self, file_path="/etc/stdd.json"):
  33. """reads the configuration file from the path given to the function,
  34. default to /etc/stdd.conf if empty"""
  35. self.__conffile = open(file_path, "r")
  36. config_read = json.load(self.__conffile)
  37. for value in config_read:
  38. if value:
  39. self.__values[value] = config_read[value]
  40. self.__conffile.close()
  41. def Analyze(self):
  42. """takes the values from the list, converts them to the needed data
  43. types and writes them into the prepared attributes
  44. """
  45. self.hw_address = int(self.__values["hw_address"], 16)
  46. self.blink_colon = self.__values["blink_colon"]
  47. self.brightness_high = self.__values["brightness_high"]
  48. self.brightness_low = self.__values["brightness_low"]
  49. self.__timetemp = self.__values["set_brightness_high"].split(":")
  50. self.set_brightness_high = datetime.time(int(self.__timetemp[0]),
  51. int(self.__timetemp[1]))
  52. self.__timetemp = self.__values["set_brightness_low"].split(":")
  53. self.set_brightness_low = datetime.time(int(self.__timetemp[0]),
  54. int(self.__timetemp[1]))
  55. self.syslog_level = self.__values["syslog_level"]
  56. self.syslog_facility = self.__values["syslog_facility"]
  57. del self.__values