some updates to package-puzzles

This commit is contained in:
slackish 2016-10-18 15:42:42 -06:00
parent 1ba99db98f
commit 36c93973cc
1 changed files with 65 additions and 115 deletions

View File

@ -12,15 +12,15 @@ import markdown
import random import random
import zipfile import zipfile
messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' import puzzles
def djb2hash(buf):
h = 5381
for c in buf:
h = ((h * 33) + c) & 0xffffffff
return h
def write_kv_pairs(ziphandle, filename, kv): def write_kv_pairs(ziphandle, filename, kv):
""" Write out a sorted map to file
:param ziphandle: a zipfile object
:param filename: The filename to write within the zipfile object
:param kv: the map to write out
:return:
"""
filehandle = io.StringIO() filehandle = io.StringIO()
for key in sorted(kv.keys()): for key in sorted(kv.keys()):
if type(kv[key]) == type([]): if type(kv[key]) == type([]):
@ -31,122 +31,72 @@ def write_kv_pairs(ziphandle, filename, kv):
filehandle.seek(0) filehandle.seek(0)
ziphandle.writestr(filename, filehandle.read()) ziphandle.writestr(filename, filehandle.read())
class Puzzle: if __name__ == '__main__':
def __init__(self, stream): parser = argparse.ArgumentParser(description='Build a puzzle category')
self.message = bytes(random.choice(messageChars) for i in range(20)) parser.add_argument('seed', help='contest seed')
self.fields = {} parser.add_argument('puzzledir', nargs='+', help='Directory of puzzle source')
self.answers = [] parser.add_argument('outdir', help='Output directory')
self.hashes = [] args = parser.parse_args()
body = []
header = True
for line in stream:
if header:
line = line.strip()
if not line.strip():
header = False
continue
key, val = line.split(':', 1)
key = key.lower()
val = val.strip()
self._add_field(key, val)
else:
body.append(line)
self.body = ''.join(body)
def _add_field(self, key, val): for puzzledir in args.puzzledir:
if key == 'answer': puzzles = {}
h = djb2hash(val.encode('utf8')) secrets = {}
self.answers.append(val)
self.hashes.append(h)
categoryname = os.path.basename(puzzledir.strip(os.sep))
# build puzzle seed
puzzle_seed = hashlib.new('sha1')
puzzle_seed.update(categoryname.encode('utf-8'))
puzzle_seed.update(args.seed.encode('utf-8'))
puzzle_seed = puzzle_seed.hexdigest()
# create zipfile
zipfilename = os.path.join(args.outdir, "%s.zip" % categoryname)
if os.path.exists(zipfilename):
# assume blank state for now
pass
# # open and gather some state
# zf = zipfile.ZipFile(zipfilename, 'a')
else: else:
self.fields[key] = val # create and read in state
zf = zipfile.ZipFile(zipfilename, 'w')
def publish(self): # read in puzzle details (will be pflarr in future)
obj = { for puzzlePath in glob.glob(os.path.join(puzzledir, "*.moth")):
'author': self.fields['author'], filename = os.path.basename(puzzlePath)
'hashes': self.hashes, points, ext = os.path.splitext(filename)
'body': markdown.markdown(self.body), points = int(points)
} puzzle = puzzles.Puzzle(open(puzzlePath))
return obj puzzles[points] = puzzle
def secrets(self): # build mapping, answers, and summary
obj = { mapping = {}
'answers': self.answers, answers = {}
'summary': self.fields['summary'], summary = {}
} for points in sorted(puzzles):
return obj puzzle = puzzles[points]
hashmap = hashlib.sha1(args.seed.encode('utf-8'))
hashmap.update(categoryname.encode('utf-8'))
parser = argparse.ArgumentParser(description='Build a puzzle category') hashmap.update(str(points).encode('utf-8'))
parser.add_argument('seed', help='contest seed') mapping[points] = hashmap.hexdigest()
parser.add_argument('puzzledir', nargs='+', help='Directory of puzzle source') answers[points] = puzzle.answers
parser.add_argument('outdir', help='Output directory') summary[points] = puzzle.fields['summary']
args = parser.parse_args()
for puzzledir in args.puzzledir:
puzzles = {}
secrets = {}
categoryname = os.path.basename(puzzledir.strip(os.sep))
# build puzzle seed
puzzle_seed = hashlib.new('sha1')
puzzle_seed.update(categoryname.encode('utf-8'))
puzzle_seed.update(args.seed.encode('utf-8'))
puzzle_seed = puzzle_seed.hexdigest()
# create zipfile
zipfilename = os.path.join(args.outdir, "%s.zip" % categoryname)
if os.path.exists(zipfilename):
# assume blank state for now
pass
# # open and gather some state
# zf = zipfile.ZipFile(zipfilename, 'a')
else:
# create and read in state
zf = zipfile.ZipFile(zipfilename, 'w')
# read in puzzle details (will be pflarr in future)
for puzzlePath in glob.glob(os.path.join(puzzledir, "*.moth")):
filename = os.path.basename(puzzlePath)
points, ext = os.path.splitext(filename)
points = int(points)
puzzle = Puzzle(open(puzzlePath))
puzzles[points] = puzzle
# build mapping, answers, and summary
mapping = {}
answers = {}
summary = {}
for points in sorted(puzzles):
puzzle = puzzles[points]
print(args.seed)
print(categoryname)
print(points)
hashmap = hashlib.sha1(args.seed.encode('utf-8'))
hashmap.update(categoryname.encode('utf-8'))
hashmap.update(str(points).encode('utf-8'))
mapping[points] = hashmap.hexdigest()
answers[points] = puzzle.answers
summary[points] = puzzle.fields['summary']
# write mapping, answers, and summary
write_kv_pairs(zf, os.path.join(categoryname, 'map.txt'), mapping)
write_kv_pairs(zf, os.path.join(categoryname, 'answers.txt'), answers)
write_kv_pairs(zf, os.path.join(categoryname, 'summary.txt'), summary)
# write out puzzles
for points in sorted(puzzles):
puzzle = puzzles[points]
puzzledir = os.path.join(categoryname, "content", mapping[points])
# test write
write_kv_pairs(zf, os.path.join(puzzledir, 'summary.txt'), summary)
# build json
# write mapping, answers, and summary
write_kv_pairs(zf, os.path.join(categoryname, 'map.txt'), mapping)
write_kv_pairs(zf, os.path.join(categoryname, 'answers.txt'), answers)
write_kv_pairs(zf, os.path.join(categoryname, 'summary.txt'), summary)
# write out puzzles
for points in sorted(puzzles):
puzzle = puzzles[points]
puzzledir = os.path.join(categoryname, 'content', mapping[points])
# build/write json
ziphandle.writestr(filename, filehandle.read())
zf.writestr(os.path.join(puzzledir, 'puzzle.json'), \
json.dumps(puzzle.publish()))
# write associated files
#vim:py #vim:py