Browse Source

New (backwards compatible) round file format

Sebastian Lohff 4 years ago
parent
commit
d4255a7971
1 changed files with 31 additions and 6 deletions
  1. 31
    6
      question.py

+ 31
- 6
question.py View File

@@ -108,19 +108,39 @@ class Questions(object):
108 108
 
109 109
 		# read sections
110 110
 		basedir = os.path.dirname(filename)
111
-		for n, sec_filename in enumerate(ysrc["Sections"], 1):
112
-			if type(sec_filename) not in (unicode, str):
113
-				self._gen_error(filename, "Section element %d is not a string (type %s found)" % (n, type(sec_filename)))
114
-
111
+		for n, sec_data in enumerate(ysrc["Sections"], 1):
112
+			jeopardyOverride = None
113
+			if type(sec_data) not in (unicode, str, dict):
114
+				self._gen_error(filename, "Section element %d is neither a string nor a dict (type %s found)" % (n, type(sec_data)))
115
+
116
+			sec_filename = None
117
+			if type(sec_data) == dict:
118
+				if "File" not in sec_data:
119
+					self._gen_error(filename, "Section element %d is a dictionary, but has no key 'File' pointing to a file to load" % (n,))
120
+
121
+				sec_filename = sec_data["File"]
122
+
123
+				if "Double-Jeopardies" in sec_data and sec_data["Double-Jeopardies"] != None:
124
+					# TODO: load (override) double jeopardies for section
125
+					if type(sec_data["Double-Jeopardies"]) != list:
126
+						self._gen_error(filename, "Section %d: Double-Jeopardies has to be a list or null (type %s found)" % (n, type(sec_data["Double-Jeopardies"])))
127
+
128
+					# take care that jeopardyOverride is a list of 5 bools
129
+					jeopardyOverride = map(bool, sec_data["Double-Jeopardies"])
130
+					jeopardyOverride = jeopardyOverride[0:5]
131
+					while len(jeopardyOverride) < 5:
132
+						jeopardyOverride.append(False)
133
+			else:
134
+				sec_filename = sec_data
115 135
 			fpath = os.path.join(basedir, sec_filename)
116 136
 			sec_ysrc = None
117 137
 			try:
118 138
 				sec_ysrc = self._get_yaml(fpath)
119 139
 			except (OSError, IOError) as e:
120 140
 				raise QuestionException("Error reading question file %s: %s" % (fpath, str(e)))
121
-			self._read_sections(sec_ysrc, fpath)
141
+			self._read_sections(sec_ysrc, fpath, jeopardyOverride)
122 142
 
123
-	def _read_sections(self, ysrc, filename):
143
+	def _read_sections(self, ysrc, filename, jeopardyOverride=None):
124 144
 		basedir = os.path.dirname(filename)
125 145
 
126 146
 		# now to check the integrity of the question file
@@ -147,11 +167,16 @@ class Questions(object):
147 167
 						self._gen_error(filename, "Qestion %d from section %d (%s) has invalid keyword '%s'" % (j, i, sec["Section"], key))
148 168
 
149 169
 				# check Double-Jeopardy is a bool and is set to false if non-existant
170
+
150 171
 				if "Double-Jeopardy" not in q.keys():
151 172
 					q["Double-Jeopardy"] = False
152 173
 				elif type(q["Double-Jeopardy"]) != bool:
153 174
 					self._gen_error(filename, "The Double-Jeopardy key from question %d from section %d (%s) must be either true or false" % (j, i, sec["Section"]))
154 175
 
176
+				# handle Double-Jeopardy override by round file
177
+				if jeopardyOverride:
178
+					q["Double-Jeopardy"] = jeopardyOverride[i-1]
179
+
155 180
 				# check Audio is a bool and is set to false if non-existant
156 181
 				if "Audio" not in q.keys():
157 182
 					q["Audio"] = False

Loading…
Cancel
Save