configuration.py 2.7 KB

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