configuration.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. class Conf(object):
  8. def __init__(self):
  9. """creates the object prepopulated with some reasonable default values"""
  10. self.__values = {
  11. "hw_address": "",
  12. "blink_colon": "0",
  13. "brightness_high": "15",
  14. "brightness_low": "2",
  15. "set_brightness_low": "00:00",
  16. "set_brightness_high": "00:00",
  17. "syslog_level": "info",
  18. "syslog_facility": "user"
  19. }
  20. """ declare empty variables to write the checked values into """
  21. self.hw_address = 0
  22. self.blink_colon = False
  23. self.brightness_high = 0
  24. self.brightness_low = 0
  25. self.set_brightness_low = None
  26. self.set_brightness_high = None
  27. self.syslog_level = ""
  28. self.syslog_facility = ""
  29. def Read(self, file_path="/etc/stdd.conf"):
  30. """reads the configuration file from the path given to the function,
  31. default to /etc/stdd.conf if empty"""
  32. self.__conffile = open(file_path, "r")
  33. for line in self.__conffile:
  34. if "#" in line:
  35. continue
  36. else:
  37. self.__line_stripped = line.strip()
  38. if self.__line_stripped != "":
  39. self.__tmpvalue = self.__line_stripped.split("=")
  40. self.__values[self.__tmpvalue[0].lower()] = self.__tmpvalue[1]
  41. self.__conffile.close()
  42. def Analyze(self):
  43. """takes the values from the list, converts them to the needed data types
  44. and writes them into the prepared attributes
  45. """
  46. self.hw_address = int(self.__values["hw_address"],16)
  47. if self.__values["blink_colon"] == "1":
  48. self.blink_colon = True
  49. else:
  50. self.blink_colon = False
  51. self.brightness_high = int(self.__values["brightness_high"])
  52. self.brightness_low = int(self.__values["brightness_low"])
  53. self.__timetemp = self.__values["set_brightness_high"].split(":")
  54. self.set_brightness_high = datetime.time(int(self.__timetemp[0]),
  55. int(self.__timetemp[1]))
  56. self.__timetemp = self.__values["set_brightness_low"].split(":")
  57. self.set_brightness_low = datetime.time(int(self.__timetemp[0]),
  58. int(self.__timetemp[1]))
  59. self.syslog_level = self.__values["syslog_level"]
  60. self.syslog_facility = self.__values["syslog_facility"]
  61. del self.__values