tools.py: protect against crazy headers

This commit is contained in:
J. Patrick Avery, Jr 2017-02-07 16:09:41 -07:00
parent f76dd321af
commit b85cd306aa
1 changed files with 11 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import contextlib
import glob import glob
import io import io
import importlib.machinery import importlib.machinery
import logging
import mistune import mistune
import os import os
import random import random
@ -95,9 +96,13 @@ class Puzzle:
if not line: if not line:
header = False header = False
continue continue
key, val = line.split(':', 1) try:
key = key.lower() key, val = line.split(':', 1)
val = val.strip() key = key.lower()
val = val.strip()
except ValueError:
raise ValueError("Invalid header line: [%s]" % (line))
if key == 'author': if key == 'author':
self.authors.append(val) self.authors.append(val)
elif key == 'summary': elif key == 'summary':
@ -116,7 +121,8 @@ class Puzzle:
pass pass
self.files[name] = PuzzleFile(stream, name, not hidden) self.files[name] = PuzzleFile(stream, name, not hidden)
else: else:
raise ValueError("Unrecognized header field: {}".format(key)) raise ValueError(
"Unrecognized header field: {}".format(key))
else: else:
self.body.write(line) self.body.write(line)
@ -293,6 +299,7 @@ class Category:
with pushd(self.path): with pushd(self.path):
self.catmod.make(points, puzzle) self.catmod.make(points, puzzle)
else: else:
logging.info("category.puzzle: %d" % (points))
puzzle.read_directory(path) puzzle.read_directory(path)
return puzzle return puzzle