# -*- coding: utf-8-*- # # This file is part of stdd, the simple time display daemon, # written by Helmut Pozimski , # licensed under the 3-clause BSD license """ This module reads the configuration from the main configuration file and exposes it to other modules for further processing. """ import datetime import json class Conf(object): """ Represents the configuration and allows accessing it's valued through public attributes. """ def __init__(self): """creates the object prepopulated with some reasonable default values """ self._values = { "hw_address": "", "blink_colon": "0", "brightness_high": "15", "brightness_low": "2", "set_brightness_low": "00:00", "set_brightness_high": "00:00", "syslog_level": "info", "syslog_facility": "user" } """ declare empty variables to write the checked values into """ self.hw_address = 0 self.blink_colon = False self.brightness_high = 0 self.brightness_low = 0 self.set_brightness_low = None self.set_brightness_high = None self.syslog_level = "" self.syslog_facility = "" def read(self, file_path="/etc/stdd.json"): """reads the configuration file from the path given to the function, default to /etc/stdd.conf if empty""" conffile = open(file_path, "r") config_read = json.load(conffile) for value in config_read: if value: self._values[value] = config_read[value] conffile.close() def analyze(self): """takes the values from the list, converts them to the needed data types and writes them into the prepared attributes """ self.hw_address = int(self._values["hw_address"], 16) self.blink_colon = self._values["blink_colon"] self.brightness_high = self._values["brightness_high"] self.brightness_low = self._values["brightness_low"] timetemp = self._values["set_brightness_high"].split(":") self.set_brightness_high = datetime.time(int(timetemp[0]), int(timetemp[1])) timetemp = self._values["set_brightness_low"].split(":") self.set_brightness_low = datetime.time(int(timetemp[0]), int(timetemp[1])) self.syslog_level = self._values["syslog_level"] self.syslog_facility = self._values["syslog_facility"] del self._values