seopardy/seopardy.py

105 lines
3.1 KiB
Python
Raw Permalink Normal View History

2013-11-06 20:42:00 +01:00
#!/usr/bin/python
2015-08-19 11:20:51 +02:00
# Licensed under GPLv3
# Written by Sebastian Lohff (seba@someserver.de)
# http://seba-geek.de/projects/seopardy/
2013-11-06 20:42:00 +01:00
from __future__ import print_function
import argparse
2013-12-02 01:06:46 +01:00
import yaml
2013-11-06 20:42:00 +01:00
import sys
2013-12-02 22:55:10 +01:00
from PySide import QtGui, QtCore
2013-11-06 20:42:00 +01:00
2013-12-02 00:23:05 +01:00
from music import MusicBox
from question import Questions, QuestionException
2013-11-06 20:42:00 +01:00
from gamestate import GameState
from game import SeopardyGame
2013-12-02 01:06:46 +01:00
from config import check_config
2013-12-07 20:42:08 +01:00
from buttonreader import get_input, InputException
2013-11-06 20:42:00 +01:00
def _parser():
parser = argparse.ArgumentParser(description="Sebas jeopardy/beopardy clone")
parser.add_argument("--gamestate", action="store", default=None, help="Gamestate to load to recover a crashed game")
2013-12-02 01:06:46 +01:00
parser.add_argument("--conf", action="store", default="seopardy.conf", help="Path to config file")
parser.add_argument("--no-cwd", action="store_true", default=False, help="Do not change working directory to question folder")
2014-12-27 13:11:21 +01:00
parser.add_argument("-v", "--verbose", action="store_true", default=False, help="Be more verbose, print filenames of missing files")
2013-11-06 20:42:00 +01:00
parser.add_argument("questions", action="store", help="Path to questionfile")
return parser
if __name__ == '__main__':
parser = _parser()
args = parser.parse_args()
questions = None
try:
2014-12-27 13:11:21 +01:00
questions = Questions(args.questions, appendPath=not args.no_cwd, verbose=args.verbose)
except QuestionException as e:
print(e.message, file=sys.stderr)
sys.exit(1)
2013-11-06 20:42:00 +01:00
# start gui
2013-12-02 01:06:46 +01:00
app = QtGui.QApplication([sys.argv[0]])
2013-12-02 22:55:10 +01:00
QtCore.QCoreApplication.setApplicationName("Seopardy")
2013-12-02 01:06:46 +01:00
# check and load config file
config = None
try:
config = yaml.safe_load(open(args.conf))
2013-12-02 01:06:46 +01:00
except IOError as e:
print("Error: Could not load config: %s" % (e,), file=sys.stderr)
2013-12-02 01:06:46 +01:00
sys.exit(1)
except yaml.scanner.ScannerError as e:
print("Error: Could not parse config file: %s" % (e,), file=sys.stderr)
2013-12-02 01:06:46 +01:00
sys.exit(1)
check_config(config)
2013-12-01 22:41:44 +01:00
# create or load gamestate
gamestate = None
if args.gamestate:
sstream = None
try:
sstream = open(args.gamestate, "r")
except IOError as e:
print("Error: Could not load gamestate: %s" % (e,), file=sys.stderr)
2013-12-01 22:41:44 +01:00
sys.exit(1)
2013-12-02 01:06:46 +01:00
gamestate = GameState.load(sstream, config["savedir"])
2013-12-01 22:41:44 +01:00
else:
2013-12-02 01:06:46 +01:00
gamestate = GameState(config["savedir"])
2013-12-01 22:41:44 +01:00
2013-12-02 00:23:05 +01:00
# init music box
MusicBox.init()
2013-12-02 01:06:46 +01:00
for name, path in config["music"].iteritems():
if path:
if not MusicBox.add_music(name, path):
print("Error: Could not load music %s (file %s)" % (name, path), file=sys.stderr)
sys.exit(1)
2017-02-09 16:38:08 +01:00
# BUG workaround: phonon is on 100% volume for some machines. try playing one song and immediately stopping it
MusicBox.play_music(MusicBox.media.keys()[0])
MusicBox.stop_music()
2013-12-02 00:23:05 +01:00
2013-12-07 20:42:08 +01:00
# create and start input threads
inputs = []
2013-12-08 00:56:37 +01:00
if config["playerInput"] is not None:
for playerInput in config["playerInput"]:
args = playerInput.get("Args", [])
if args is None:
args = []
2013-12-07 20:42:08 +01:00
2013-12-08 00:56:37 +01:00
try:
inp = get_input(playerInput["Type"], args, app)
inputs.append(inp)
except InputException as e:
print("Error: %s" % e, file=sys.stderr)
sys.exit(1)
2013-12-07 20:42:08 +01:00
2013-12-08 00:56:37 +01:00
inputs[-1].start()
2013-12-07 20:42:08 +01:00
2013-12-01 22:41:44 +01:00
# create board
board = SeopardyGame(questions, gamestate, inputs)
2013-11-06 20:42:00 +01:00
board.show()
app.exec_()
sys.exit()