Browse Source

Config file handling

Sebastian Lohff 6 years ago
parent
commit
d7ca903f01
4 changed files with 86 additions and 16 deletions
  1. 1
    0
      .gitignore
  2. 36
    0
      config.py
  3. 27
    0
      seopardy.conf.dist
  4. 22
    16
      seopardy.py

+ 1
- 0
.gitignore View File

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

+ 36
- 0
config.py View File

@@ -0,0 +1,36 @@
1
+from __future__ import print_function
2
+
3
+import sys
4
+
5
+def _config_error(msg):
6
+	print("Configuration error: %s" % (msg,), file=sys.stderr)
7
+	sys.exit(1)
8
+
9
+def _check_missing_keys(conf, sec, mandatory, users_choice=[]):
10
+	#d = conf[sec] if sec is not None else conf
11
+	#if sec is None:
12
+	#	sec = "main"
13
+
14
+	d = conf
15
+
16
+	allowed = mandatory + users_choice
17
+
18
+	for key in d.iterkeys():
19
+		if key not in allowed:
20
+			_config_error("Key %s is not an allowed config key in section %s (allowed are %s)" % (key, sec, ", ".join(allowed)))
21
+
22
+	for key in mandatory:
23
+		if key not in d.iterkeys():
24
+			_config_error("Key %s is not present in section %s but mandatory" % (key, sec,))
25
+
26
+
27
+def check_config(conf):
28
+	main_keys = ["music", "savedir", "playerInput"]
29
+	music_keys = ["startSong", "questionSong"]
30
+
31
+	_check_missing_keys(conf, "main", main_keys)
32
+	_check_missing_keys(conf["music"], "music", music_keys)
33
+
34
+	if conf["playerInput"]:
35
+		for i, inp in enumerate(conf["playerInput"], 1):
36
+			_check_missing_keys(inp, "playerInput %d" % i, ["Type"], ["Args"])

+ 27
- 0
seopardy.conf.dist View File

@@ -0,0 +1,27 @@
1
+#                                     _       
2
+#  ___  ___  ___  _ __   __ _ _ __ __| |_   _ 
3
+# / __|/ _ \/ _ \| '_ \ / _` | '__/ _` | | | |
4
+# \__ \  __/ (_) | |_) | (_| | | | (_| | |_| |
5
+# |___/\___|\___/| .__/ \__,_|_|  \__,_|\__, |
6
+#                |_|                    |___/ 
7
+# Configuration
8
+#
9
+# Basicly you can configure here:
10
+#   - player inputs
11
+#   - pathes to theme songs
12
+#   - path to savedir (gamestate backups while game is running)
13
+# Note that this is a yamlfile (no tabs -.-)
14
+
15
+# path to music, set to null if you don't have the song
16
+music:
17
+    startSong:       path/to/theme_song.mp3
18
+    questionSong:    path/to/question_song.mp3
19
+
20
+# change to path, default is /tmp/seopardy/
21
+savedir: /tmp/seopardy/
22
+
23
+# player inputs. look in buttonreader.py for available types
24
+playerInput:
25
+# - Type: Fifo
26
+#   Args:
27
+#      - /tmp/seopardy_fifo

+ 22
- 16
seopardy.py View File

@@ -2,21 +2,20 @@
2 2
 from __future__ import print_function
3 3
 
4 4
 import argparse
5
-import os
5
+import yaml
6 6
 import sys
7
-#from PySide import QtCore, QtGui
8 7
 from PySide import QtGui
9 8
 
10 9
 from music import MusicBox
11 10
 from question import Questions
12 11
 from gamestate import GameState
13 12
 from game import SeopardyGame
13
+from config import check_config
14 14
 
15 15
 def _parser():
16 16
 	parser = argparse.ArgumentParser(description="Sebas jeopardy/beopardy clone")
17
-	parser.add_argument("--fifo", action="store", default="/tmp/seopardy-fifo", help="Name of the fifo to create")
18 17
 	parser.add_argument("--gamestate", action="store", default=None, help="Gamestate to load to recover a crashed game")
19
-	parser.add_argument("--savedir", action="store", default="/tmp/seopardy/", help="Directory where to save the temporary gamestates to, in case of game-crash")
18
+	parser.add_argument("--conf", action="store", default="seopardy.conf", help="Path to config file")
20 19
 	parser.add_argument("questions", action="store", help="Path to questionfile")
21 20
 
22 21
 	return parser
@@ -25,19 +24,23 @@ if __name__ == '__main__':
25 24
 	parser = _parser()
26 25
 	args = parser.parse_args()
27 26
 
28
-	fifo = None
29
-	try:
30
-		if os.path.exists(args.fifo):
31
-			os.unlink(args.fifo)
32
-		os.mkfifo(args.fifo)
33
-	except OSError as e:
34
-		print("Error: Could not create fifo: %s" % (str(e),), file=sys.stderr)
35
-		sys.exit(1)
36
-
37 27
 	questions = Questions(args.questions)
38 28
 
39 29
 	# start gui
40
-	app = QtGui.QApplication([])
30
+	app = QtGui.QApplication([sys.argv[0]])
31
+
32
+	# check and load config file
33
+	config = None
34
+	try:
35
+		config = yaml.load(open(args.conf))
36
+	except IOError as e:
37
+		print("Error: Could not load config: %s" % (str(e),), file=sys.stderr)
38
+		sys.exit(1)
39
+	except yaml.scanner.ScannerError as e:
40
+		print("Error: Could not parse config file: %s" % (str(e),), file=sys.stderr)
41
+		sys.exit(1)
42
+
43
+	check_config(config)
41 44
 
42 45
 	# create or load gamestate
43 46
 	gamestate = None
@@ -48,12 +51,15 @@ if __name__ == '__main__':
48 51
 		except IOError as e:
49 52
 			print("Error: Could not load gamestate: %s" % (str(e),), file=sys.stderr)
50 53
 			sys.exit(1)
51
-		gamestate = GameState.load(sstream, args.savedir)
54
+		gamestate = GameState.load(sstream, config["savedir"])
52 55
 	else:
53
-		gamestate = GameState(args.savedir)
56
+		gamestate = GameState(config["savedir"])
54 57
 
55 58
 	# init music box
56 59
 	MusicBox.init()
60
+	for name, path in config["music"].iteritems():
61
+		if path:
62
+			MusicBox.add_music(name, path)
57 63
 
58 64
 	# create board
59 65
 	board = SeopardyGame(questions, gamestate)

Loading…
Cancel
Save