seopardy/config.py

37 lines
1020 B
Python
Raw Normal View History

2013-12-02 01:06:46 +01:00
from __future__ import print_function
import sys
def _config_error(msg):
print("Configuration error: %s" % (msg,), file=sys.stderr)
sys.exit(1)
def _check_missing_keys(conf, sec, mandatory, users_choice=[]):
#d = conf[sec] if sec is not None else conf
#if sec is None:
# sec = "main"
d = conf
allowed = mandatory + users_choice
for key in d.iterkeys():
if key not in allowed:
_config_error("Key %s is not an allowed config key in section %s (allowed are %s)" % (key, sec, ", ".join(allowed)))
for key in mandatory:
if key not in d.iterkeys():
_config_error("Key %s is not present in section %s but mandatory" % (key, sec,))
def check_config(conf):
main_keys = ["music", "savedir", "playerInput"]
music_keys = ["startSong", "questionSong"]
_check_missing_keys(conf, "main", main_keys)
_check_missing_keys(conf["music"], "music", music_keys)
if conf["playerInput"]:
for i, inp in enumerate(conf["playerInput"], 1):
_check_missing_keys(inp, "playerInput %d" % i, ["Type"], ["Args"])