configuration.py 2.6 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. """ This module reads the configuration from the main configuration file and
  7. exposes it to other modules for further processing.
  8. """
  9. import datetime
  10. import json
  11. class Conf(object):
  12. """ Represents the configuration and allows accessing it's valued through
  13. public attributes.
  14. """
  15. def __init__(self):
  16. """creates the object prepopulated with some reasonable default
  17. values
  18. """
  19. self._values = {
  20. "hw_address": "",
  21. "blink_colon": "0",
  22. "brightness_high": "15",
  23. "brightness_low": "2",
  24. "set_brightness_low": "00:00",
  25. "set_brightness_high": "00:00",
  26. "syslog_level": "info",
  27. "syslog_facility": "user"
  28. }
  29. """ declare empty variables to write the checked values into """
  30. self.hw_address = 0
  31. self.blink_colon = False
  32. self.brightness_high = 0
  33. self.brightness_low = 0
  34. self.set_brightness_low = None
  35. self.set_brightness_high = None
  36. self.syslog_level = ""
  37. self.syslog_facility = ""
  38. def read(self, file_path="/etc/stdd.json"):
  39. """reads the configuration file from the path given to the function,
  40. default to /etc/stdd.conf if empty"""
  41. conffile = open(file_path, "r")
  42. config_read = json.load(conffile)
  43. for value in config_read:
  44. if value:
  45. self._values[value] = config_read[value]
  46. conffile.close()
  47. def analyze(self):
  48. """takes the values from the list, converts them to the needed data
  49. types and writes them into the prepared attributes
  50. """
  51. self.hw_address = int(self._values["hw_address"], 16)
  52. self.blink_colon = self._values["blink_colon"]
  53. self.brightness_high = self._values["brightness_high"]
  54. self.brightness_low = self._values["brightness_low"]
  55. timetemp = self._values["set_brightness_high"].split(":")
  56. self.set_brightness_high = datetime.time(int(timetemp[0]),
  57. int(timetemp[1]))
  58. timetemp = self._values["set_brightness_low"].split(":")
  59. self.set_brightness_low = datetime.time(int(timetemp[0]),
  60. int(timetemp[1]))
  61. self.syslog_level = self._values["syslog_level"]
  62. self.syslog_facility = self._values["syslog_facility"]
  63. del self._values