Config file handling

master
Sebastian Lohff hace 10 años
padre 645745ed7c
commit d7ca903f01

1
.gitignore vendido

@ -4,3 +4,4 @@
*.pyo
*~
*.o
seopardy.conf

@ -0,0 +1,36 @@
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"])

@ -0,0 +1,27 @@
# _
# ___ ___ ___ _ __ __ _ _ __ __| |_ _
# / __|/ _ \/ _ \| '_ \ / _` | '__/ _` | | | |
# \__ \ __/ (_) | |_) | (_| | | | (_| | |_| |
# |___/\___|\___/| .__/ \__,_|_| \__,_|\__, |
# |_| |___/
# Configuration
#
# Basicly you can configure here:
# - player inputs
# - pathes to theme songs
# - path to savedir (gamestate backups while game is running)
# Note that this is a yamlfile (no tabs -.-)
# path to music, set to null if you don't have the song
music:
startSong: path/to/theme_song.mp3
questionSong: path/to/question_song.mp3
# change to path, default is /tmp/seopardy/
savedir: /tmp/seopardy/
# player inputs. look in buttonreader.py for available types
playerInput:
# - Type: Fifo
# Args:
# - /tmp/seopardy_fifo

@ -2,21 +2,20 @@
from __future__ import print_function
import argparse
import os
import yaml
import sys
#from PySide import QtCore, QtGui
from PySide import QtGui
from music import MusicBox
from question import Questions
from gamestate import GameState
from game import SeopardyGame
from config import check_config
def _parser():
parser = argparse.ArgumentParser(description="Sebas jeopardy/beopardy clone")
parser.add_argument("--fifo", action="store", default="/tmp/seopardy-fifo", help="Name of the fifo to create")
parser.add_argument("--gamestate", action="store", default=None, help="Gamestate to load to recover a crashed game")
parser.add_argument("--savedir", action="store", default="/tmp/seopardy/", help="Directory where to save the temporary gamestates to, in case of game-crash")
parser.add_argument("--conf", action="store", default="seopardy.conf", help="Path to config file")
parser.add_argument("questions", action="store", help="Path to questionfile")
return parser
@ -25,19 +24,23 @@ if __name__ == '__main__':
parser = _parser()
args = parser.parse_args()
fifo = None
try:
if os.path.exists(args.fifo):
os.unlink(args.fifo)
os.mkfifo(args.fifo)
except OSError as e:
print("Error: Could not create fifo: %s" % (str(e),), file=sys.stderr)
sys.exit(1)
questions = Questions(args.questions)
# start gui
app = QtGui.QApplication([])
app = QtGui.QApplication([sys.argv[0]])
# check and load config file
config = None
try:
config = yaml.load(open(args.conf))
except IOError as e:
print("Error: Could not load config: %s" % (str(e),), file=sys.stderr)
sys.exit(1)
except yaml.scanner.ScannerError as e:
print("Error: Could not parse config file: %s" % (str(e),), file=sys.stderr)
sys.exit(1)
check_config(config)
# create or load gamestate
gamestate = None
@ -48,12 +51,15 @@ if __name__ == '__main__':
except IOError as e:
print("Error: Could not load gamestate: %s" % (str(e),), file=sys.stderr)
sys.exit(1)
gamestate = GameState.load(sstream, args.savedir)
gamestate = GameState.load(sstream, config["savedir"])
else:
gamestate = GameState(args.savedir)
gamestate = GameState(config["savedir"])
# init music box
MusicBox.init()
for name, path in config["music"].iteritems():
if path:
MusicBox.add_music(name, path)
# create board
board = SeopardyGame(questions, gamestate)

Cargando…
Cancelar
Guardar