|
| 1 | +""" |
| 2 | +Copyright (C) 2020 Mats Klepsland <mats.klepsland@gmail.com> |
| 3 | +
|
| 4 | +This file is part of Tracker. |
| 5 | +
|
| 6 | +This program is free software: you can redistribute it and/or modify it under |
| 7 | +the terms of the GNU General Public License as published by the Free Software |
| 8 | +Foundation, either version 3 of the License, or (at your option) any later |
| 9 | +version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 | +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU General Public License along with |
| 16 | +this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +""" |
| 18 | + |
| 19 | +import sys |
| 20 | +import yaml |
| 21 | + |
| 22 | +DEFAULT_STATE_HOME = "home" |
| 23 | +DEFAULT_STATE_NOT_HOME = "not_home" |
| 24 | +DEFAULT_MQTT_BROKER = "localhost" |
| 25 | +DEFAULT_MQTT_PORT = "1833" |
| 26 | +DEFAULT_MQTT_TOPIC_PREFIX = "tracker/location/" |
| 27 | + |
| 28 | + |
| 29 | +def _dict_nested_set(d, keys, value): |
| 30 | + """Set a nested key in a dictionary. |
| 31 | +
|
| 32 | + Args: |
| 33 | + d (dict): The dictionary. |
| 34 | + keys (list): The nested key. |
| 35 | + value: The value to set the key to. |
| 36 | +
|
| 37 | + """ |
| 38 | + for key in keys[:-1]: |
| 39 | + d = d.setdefault(key, {}) |
| 40 | + d[keys[-1]] = value |
| 41 | + |
| 42 | + |
| 43 | +def _dict_nested_isset(d, keys): |
| 44 | + """Check if nested key is set in dictionary. |
| 45 | +
|
| 46 | + Args: |
| 47 | + d (dict): The dictionary. |
| 48 | + keys (list): The nested key. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + True if key is set, False otherwise. |
| 52 | +
|
| 53 | + """ |
| 54 | + _d = d |
| 55 | + |
| 56 | + for key in keys: |
| 57 | + try: |
| 58 | + _d = _d[key] |
| 59 | + except KeyError: |
| 60 | + return False |
| 61 | + |
| 62 | + return True |
| 63 | + |
| 64 | + |
| 65 | +def config_apply_defaults(conf): |
| 66 | + """Apply defaults to the config where key is not set. |
| 67 | +
|
| 68 | + Args: |
| 69 | + conf (dict): The loaded config. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + Dictionary containing config, with added defaults. |
| 73 | +
|
| 74 | + """ |
| 75 | + |
| 76 | + if not _dict_nested_isset(conf, ["state", "home"]): |
| 77 | + _dict_nested_set(conf, ["state", "home"], DEFAULT_STATE_HOME) |
| 78 | + |
| 79 | + if not _dict_nested_isset(conf, ["state", "not_home"]): |
| 80 | + _dict_nested_set(conf, ["state", "not_home"], DEFAULT_STATE_NOT_HOME) |
| 81 | + |
| 82 | + if not _dict_nested_isset(conf, ["mqtt", "broker"]): |
| 83 | + _dict_nested_set(conf, ["mqtt", "broker"], DEFAULT_MQTT_BROKER) |
| 84 | + |
| 85 | + if not _dict_nested_isset(conf, ["mqtt", "port"]): |
| 86 | + _dict_nested_set(conf, ["mqtt", "port"], DEFAULT_MQTT_PORT) |
| 87 | + |
| 88 | + if not _dict_nested_isset(conf, ["mqtt", "topic_prefix"]): |
| 89 | + _dict_nested_set(conf, ["mqtt", "topic_prefix"], |
| 90 | + DEFAULT_MQTT_TOPIC_PREFIX) |
| 91 | + |
| 92 | + return conf |
| 93 | + |
| 94 | + |
| 95 | +def config_load(config_file): |
| 96 | + """Load configuration file (YAML). |
| 97 | +
|
| 98 | + Args: |
| 99 | + config_file (str): Configuration file. |
| 100 | +
|
| 101 | + Returns: |
| 102 | + Dictionary containing config. |
| 103 | +
|
| 104 | + """ |
| 105 | + try: |
| 106 | + with open(config_file, "r") as f: |
| 107 | + conf = yaml.safe_load(f) |
| 108 | + except yaml.YAMLError as ex: |
| 109 | + sys.exit("ERROR: could not parse config file '%s': %s" % |
| 110 | + (config_file, ex)) |
| 111 | + except FileNotFoundError: |
| 112 | + sys.exit("ERROR: could not find config file '%s'" % config_file) |
| 113 | + |
| 114 | + config_apply_defaults(conf) |
| 115 | + |
| 116 | + if "device_track" not in conf: |
| 117 | + sys.exit("Config does not contain any devices to track. Exiting.") |
| 118 | + |
| 119 | + return conf |
0 commit comments