2016-10-16 20:32:00 -06:00
|
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
|
|
import argparse
|
2016-10-22 10:35:55 -06:00
|
|
|
|
import contextlib
|
2019-08-15 16:30:48 -06:00
|
|
|
|
import copy
|
2016-10-16 20:32:00 -06:00
|
|
|
|
import glob
|
2016-10-17 19:58:51 -06:00
|
|
|
|
import hashlib
|
2018-09-27 15:07:03 -06:00
|
|
|
|
import html
|
2016-10-22 10:35:55 -06:00
|
|
|
|
import io
|
|
|
|
|
import importlib.machinery
|
2019-10-30 10:05:07 -06:00
|
|
|
|
import logging
|
2016-10-16 20:32:00 -06:00
|
|
|
|
import mistune
|
2016-10-17 13:24:54 -06:00
|
|
|
|
import os
|
2016-10-16 20:32:00 -06:00
|
|
|
|
import random
|
2016-10-23 14:55:35 -06:00
|
|
|
|
import string
|
2019-08-15 16:30:48 -06:00
|
|
|
|
import sys
|
2016-10-17 19:58:51 -06:00
|
|
|
|
import tempfile
|
2019-03-24 20:34:01 -06:00
|
|
|
|
import shlex
|
2019-07-05 17:53:46 -06:00
|
|
|
|
import yaml
|
2016-10-16 20:32:00 -06:00
|
|
|
|
|
|
|
|
|
messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
|
|
2019-10-30 10:05:07 -06:00
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
2019-02-24 11:53:22 -07:00
|
|
|
|
def djb2hash(str):
|
2016-10-16 20:32:00 -06:00
|
|
|
|
h = 5381
|
2019-02-24 11:53:22 -07:00
|
|
|
|
for c in str.encode("utf-8"):
|
2016-10-16 20:32:00 -06:00
|
|
|
|
h = ((h * 33) + c) & 0xffffffff
|
|
|
|
|
return h
|
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
@contextlib.contextmanager
|
|
|
|
|
def pushd(newdir):
|
|
|
|
|
curdir = os.getcwd()
|
2019-10-30 10:05:07 -06:00
|
|
|
|
LOGGER.debug("Attempting to chdir from %s to %s" % (curdir, newdir))
|
2016-10-22 10:35:55 -06:00
|
|
|
|
os.chdir(newdir)
|
2019-08-15 16:34:10 -06:00
|
|
|
|
|
2019-08-15 16:30:48 -06:00
|
|
|
|
# Force a copy of the old path, instead of just a reference
|
|
|
|
|
old_path = list(sys.path)
|
|
|
|
|
old_modules = copy.copy(sys.modules)
|
|
|
|
|
sys.path.append(newdir)
|
2019-08-15 16:34:10 -06:00
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
2019-08-15 16:30:48 -06:00
|
|
|
|
# Restore the old path
|
|
|
|
|
to_remove = []
|
|
|
|
|
for module in sys.modules:
|
|
|
|
|
if module not in old_modules:
|
|
|
|
|
to_remove.append(module)
|
|
|
|
|
|
|
|
|
|
for module in to_remove:
|
|
|
|
|
del(sys.modules[module])
|
|
|
|
|
|
|
|
|
|
sys.path = old_path
|
2019-10-30 10:05:07 -06:00
|
|
|
|
LOGGER.debug("Changing directory back from %s to %s" % (newdir, curdir))
|
2016-10-22 10:35:55 -06:00
|
|
|
|
os.chdir(curdir)
|
|
|
|
|
|
2017-02-02 11:34:57 -07:00
|
|
|
|
|
|
|
|
|
def loadmod(name, path):
|
|
|
|
|
abspath = os.path.abspath(path)
|
|
|
|
|
loader = importlib.machinery.SourceFileLoader(name, abspath)
|
|
|
|
|
return loader.load_module()
|
|
|
|
|
|
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
# Get a big list of clean words for our answer file.
|
|
|
|
|
ANSWER_WORDS = [w.strip() for w in open(os.path.join(os.path.dirname(__file__),
|
|
|
|
|
'answer_words.txt'))]
|
|
|
|
|
|
|
|
|
|
class PuzzleFile:
|
|
|
|
|
"""A file associated with a puzzle.
|
|
|
|
|
|
2016-10-18 13:11:53 -06:00
|
|
|
|
path: The path to the original input file. May be None (when this is created from a file handle
|
|
|
|
|
and there is no original input.
|
|
|
|
|
handle: A File-like object set to read the file from. You should be able to read straight
|
|
|
|
|
from it without having to seek to the beginning of the file.
|
|
|
|
|
name: The name of the output file.
|
|
|
|
|
visible: A boolean indicating whether this file should visible to the user. If False,
|
|
|
|
|
the file is still expected to be accessible, but it's path must be known
|
|
|
|
|
(or figured out) to retrieve it."""
|
2016-10-17 19:58:51 -06:00
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
def __init__(self, stream, name, visible=True):
|
|
|
|
|
self.stream = stream
|
|
|
|
|
self.name = name
|
|
|
|
|
self.visible = visible
|
2016-10-17 21:26:56 -06:00
|
|
|
|
|
2019-08-02 16:24:45 -06:00
|
|
|
|
class PuzzleSuccess(dict):
|
|
|
|
|
"""Puzzle success objectives
|
|
|
|
|
|
|
|
|
|
:param acceptable: Learning outcome from acceptable knowledge of the subject matter
|
|
|
|
|
:param mastery: Learning outcome from mastery of the subject matter
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
valid_fields = ["acceptable", "mastery"]
|
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
super(PuzzleSuccess, self).__init__()
|
|
|
|
|
for key in self.valid_fields:
|
|
|
|
|
self[key] = None
|
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
|
if key in self.valid_fields:
|
|
|
|
|
self[key] = value
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
|
if attr in self.valid_fields:
|
|
|
|
|
return self[attr]
|
|
|
|
|
raise AttributeError("'%s' object has no attribute '%s'" % (type(self).__name__, attr))
|
|
|
|
|
|
|
|
|
|
def __setattr__(self, attr, value):
|
|
|
|
|
if attr in self.valid_fields:
|
|
|
|
|
self[attr] = value
|
|
|
|
|
else:
|
2019-08-02 17:26:43 -06:00
|
|
|
|
raise AttributeError("'%s' object has no attribute '%s'" % (type(self).__name__, attr))
|
2019-08-02 16:24:45 -06:00
|
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
class Puzzle:
|
|
|
|
|
def __init__(self, category_seed, points):
|
2016-10-18 13:29:41 -06:00
|
|
|
|
"""A MOTH Puzzle.
|
2016-10-22 10:35:55 -06:00
|
|
|
|
|
2016-10-18 11:24:46 -06:00
|
|
|
|
:param category_seed: A byte string to use as a seed for random numbers for this puzzle.
|
|
|
|
|
It is combined with the puzzle points.
|
2016-10-22 10:35:55 -06:00
|
|
|
|
:param points: The point value of the puzzle.
|
2016-10-17 21:26:56 -06:00
|
|
|
|
"""
|
|
|
|
|
|
2016-10-17 13:24:54 -06:00
|
|
|
|
super().__init__()
|
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
self.points = points
|
|
|
|
|
self.summary = None
|
2017-01-30 12:13:02 -07:00
|
|
|
|
self.authors = []
|
2016-10-22 10:35:55 -06:00
|
|
|
|
self.answers = []
|
2017-10-23 10:01:11 -06:00
|
|
|
|
self.scripts = []
|
2019-02-24 17:02:28 -07:00
|
|
|
|
self.pattern = None
|
2018-10-02 19:21:54 -06:00
|
|
|
|
self.hint = None
|
2016-10-22 10:35:55 -06:00
|
|
|
|
self.files = {}
|
|
|
|
|
self.body = io.StringIO()
|
2019-07-09 11:41:51 -06:00
|
|
|
|
|
|
|
|
|
# NIST NICE objective content
|
|
|
|
|
self.objective = None # Text describing the expected learning outcome from solving this puzzle, *why* are you solving this puzzle
|
2019-08-02 16:24:45 -06:00
|
|
|
|
self.success = PuzzleSuccess() # Text describing criteria for different levels of success, e.g. {"Acceptable": "Did OK", "Mastery": "Did even better"}
|
2019-07-09 10:36:01 -06:00
|
|
|
|
self.solution = None # Text describing how to solve the puzzle
|
2019-08-02 12:02:14 -06:00
|
|
|
|
self.ksas = [] # A list of references to related NICE KSAs (e.g. K0058, . . .)
|
2019-07-09 11:41:51 -06:00
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
self.logs = []
|
|
|
|
|
self.randseed = category_seed * self.points
|
|
|
|
|
self.rand = random.Random(self.randseed)
|
2016-10-17 21:26:56 -06:00
|
|
|
|
|
2017-01-31 17:03:42 -07:00
|
|
|
|
def log(self, *vals):
|
2016-10-19 15:02:38 -06:00
|
|
|
|
"""Add a new log message to this puzzle."""
|
2017-01-31 17:03:42 -07:00
|
|
|
|
msg = ' '.join(str(v) for v in vals)
|
|
|
|
|
self.logs.append(msg)
|
2016-10-19 15:02:38 -06:00
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
def read_stream(self, stream):
|
2016-10-16 20:32:00 -06:00
|
|
|
|
header = True
|
2019-07-05 17:53:46 -06:00
|
|
|
|
line = ""
|
|
|
|
|
if stream.read(3) == "---":
|
|
|
|
|
header = "yaml"
|
|
|
|
|
else:
|
|
|
|
|
header = "moth"
|
|
|
|
|
|
|
|
|
|
stream.seek(0)
|
|
|
|
|
|
|
|
|
|
if header == "yaml":
|
2019-10-30 10:05:07 -06:00
|
|
|
|
LOGGER.info("Puzzle is YAML-formatted")
|
2019-07-05 17:53:46 -06:00
|
|
|
|
self.read_yaml_header(stream)
|
|
|
|
|
elif header == "moth":
|
2019-10-30 10:05:07 -06:00
|
|
|
|
LOGGER.info("Puzzle is MOTH-formatted")
|
2019-07-05 17:53:46 -06:00
|
|
|
|
self.read_moth_header(stream)
|
|
|
|
|
|
2016-10-16 20:32:00 -06:00
|
|
|
|
for line in stream:
|
2019-07-05 17:53:46 -06:00
|
|
|
|
self.body.write(line)
|
|
|
|
|
|
|
|
|
|
def read_yaml_header(self, stream):
|
|
|
|
|
contents = ""
|
|
|
|
|
header = False
|
2016-10-16 20:32:00 -06:00
|
|
|
|
for line in stream:
|
2019-07-05 17:53:46 -06:00
|
|
|
|
if line.strip() == "---" and header: # Handle last line
|
|
|
|
|
break
|
|
|
|
|
elif line.strip() == "---": # Handle first line
|
|
|
|
|
header = True
|
|
|
|
|
continue
|
2016-10-16 20:32:00 -06:00
|
|
|
|
else:
|
2019-07-05 17:53:46 -06:00
|
|
|
|
contents += line
|
|
|
|
|
|
|
|
|
|
config = yaml.safe_load(contents)
|
|
|
|
|
for key, value in config.items():
|
|
|
|
|
key = key.lower()
|
|
|
|
|
self.handle_header_key(key, value)
|
|
|
|
|
|
|
|
|
|
def read_moth_header(self, stream):
|
|
|
|
|
for line in stream:
|
|
|
|
|
line = line.strip()
|
|
|
|
|
if not line:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
key, val = line.split(':', 1)
|
|
|
|
|
key = key.lower()
|
|
|
|
|
val = val.strip()
|
|
|
|
|
self.handle_header_key(key, val)
|
|
|
|
|
|
|
|
|
|
def handle_header_key(self, key, val):
|
2019-10-30 10:05:07 -06:00
|
|
|
|
LOGGER.debug("Handling key: %s, value: %s", key, val)
|
2019-07-05 17:53:46 -06:00
|
|
|
|
if key == 'author':
|
|
|
|
|
self.authors.append(val)
|
|
|
|
|
elif key == 'summary':
|
|
|
|
|
self.summary = val
|
|
|
|
|
elif key == 'answer':
|
2019-07-09 11:59:57 -06:00
|
|
|
|
if not isinstance(val, str):
|
|
|
|
|
raise ValueError("Answers must be strings, got %s, instead" % (type(val),))
|
2019-07-05 17:53:46 -06:00
|
|
|
|
self.answers.append(val)
|
|
|
|
|
elif key == "answers":
|
|
|
|
|
for answer in val:
|
2019-07-09 11:59:57 -06:00
|
|
|
|
if not isinstance(answer, str):
|
|
|
|
|
raise ValueError("Answers must be strings, got %s, instead" % (type(answer),))
|
2019-07-05 17:53:46 -06:00
|
|
|
|
self.answers.append(answer)
|
|
|
|
|
elif key == 'pattern':
|
|
|
|
|
self.pattern = val
|
|
|
|
|
elif key == 'hint':
|
|
|
|
|
self.hint = val
|
|
|
|
|
elif key == 'name':
|
|
|
|
|
pass
|
|
|
|
|
elif key == 'file':
|
|
|
|
|
parts = shlex.split(val)
|
|
|
|
|
name = parts[0]
|
|
|
|
|
hidden = False
|
2019-10-30 10:05:07 -06:00
|
|
|
|
LOGGER.debug("Attempting to open %s", os.path.abspath(name))
|
2019-07-05 17:53:46 -06:00
|
|
|
|
stream = open(name, 'rb')
|
|
|
|
|
try:
|
|
|
|
|
name = parts[1]
|
|
|
|
|
hidden = (parts[2].lower() == "hidden")
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
self.files[name] = PuzzleFile(stream, name, not hidden)
|
|
|
|
|
elif key == 'script':
|
|
|
|
|
stream = open(val, 'rb')
|
|
|
|
|
# Make sure this shows up in the header block of the HTML output.
|
|
|
|
|
self.files[val] = PuzzleFile(stream, val, visible=False)
|
|
|
|
|
self.scripts.append(val)
|
2019-08-13 15:01:07 -06:00
|
|
|
|
elif key == "objective":
|
|
|
|
|
self.objective = val
|
|
|
|
|
elif key == "success":
|
2019-08-14 16:55:09 -06:00
|
|
|
|
# Force success dictionary keys to be lower-case
|
|
|
|
|
self.success = dict((x.lower(), y) for x,y in val.items())
|
2019-08-13 15:01:07 -06:00
|
|
|
|
elif key == "success.acceptable":
|
|
|
|
|
self.success.acceptable = val
|
|
|
|
|
elif key == "success.mastery":
|
|
|
|
|
self.success.mastery = val
|
|
|
|
|
elif key == "solution":
|
|
|
|
|
self.solution = val
|
|
|
|
|
elif key == "ksa":
|
|
|
|
|
self.ksas.append(val)
|
2019-07-05 17:53:46 -06:00
|
|
|
|
else:
|
|
|
|
|
raise ValueError("Unrecognized header field: {}".format(key))
|
|
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
def read_directory(self, path):
|
|
|
|
|
try:
|
2017-02-02 11:34:57 -07:00
|
|
|
|
puzzle_mod = loadmod("puzzle", os.path.join(path, "puzzle.py"))
|
2016-10-22 10:35:55 -06:00
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
puzzle_mod = None
|
|
|
|
|
|
2016-12-01 16:20:04 -07:00
|
|
|
|
with pushd(path):
|
|
|
|
|
if puzzle_mod:
|
2016-10-22 10:35:55 -06:00
|
|
|
|
puzzle_mod.make(self)
|
2016-12-01 16:20:04 -07:00
|
|
|
|
else:
|
|
|
|
|
with open('puzzle.moth') as f:
|
|
|
|
|
self.read_stream(f)
|
2016-10-17 19:58:51 -06:00
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
def random_hash(self):
|
|
|
|
|
"""Create a file basename (no extension) with our number generator."""
|
2016-10-23 14:55:35 -06:00
|
|
|
|
return ''.join(self.rand.choice(string.ascii_lowercase) for i in range(8))
|
2016-10-17 19:58:51 -06:00
|
|
|
|
|
2016-10-18 20:11:20 -06:00
|
|
|
|
def make_temp_file(self, name=None, visible=True):
|
2016-10-17 21:26:56 -06:00
|
|
|
|
"""Get a file object for adding dynamically generated data to the puzzle. When you're
|
|
|
|
|
done with this file, flush it, but don't close it.
|
2016-10-22 10:35:55 -06:00
|
|
|
|
|
2016-10-17 21:26:56 -06:00
|
|
|
|
:param name: The name of the file for links within the puzzle. If this is None, a name
|
|
|
|
|
will be generated for you.
|
|
|
|
|
:param visible: Whether or not the file will be visible to the user.
|
2016-10-17 19:58:51 -06:00
|
|
|
|
:return: A file object for writing
|
|
|
|
|
"""
|
|
|
|
|
|
2016-10-23 14:55:35 -06:00
|
|
|
|
stream = tempfile.TemporaryFile()
|
|
|
|
|
self.add_stream(stream, name, visible)
|
|
|
|
|
return stream
|
|
|
|
|
|
|
|
|
|
def add_stream(self, stream, name=None, visible=True):
|
2016-10-17 21:26:56 -06:00
|
|
|
|
if name is None:
|
|
|
|
|
name = self.random_hash()
|
2016-10-22 10:35:55 -06:00
|
|
|
|
self.files[name] = PuzzleFile(stream, name, visible)
|
2016-10-17 19:58:51 -06:00
|
|
|
|
|
2016-12-01 16:20:04 -07:00
|
|
|
|
def add_file(self, filename, visible=True):
|
|
|
|
|
fd = open(filename, 'rb')
|
|
|
|
|
name = os.path.basename(filename)
|
|
|
|
|
self.add_stream(fd, name=name, visible=visible)
|
|
|
|
|
|
2016-11-28 15:17:12 -07:00
|
|
|
|
def randword(self):
|
|
|
|
|
"""Return a randomly-chosen word"""
|
|
|
|
|
|
|
|
|
|
return self.rand.choice(ANSWER_WORDS)
|
|
|
|
|
|
2016-12-01 16:20:04 -07:00
|
|
|
|
def make_answer(self, word_count=4, sep=' '):
|
2016-10-17 19:58:51 -06:00
|
|
|
|
"""Generate and return a new answer. It's automatically added to the puzzle answer list.
|
|
|
|
|
:param int word_count: The number of words to include in the answer.
|
|
|
|
|
:param str|bytes sep: The word separator.
|
2016-10-18 09:51:33 -06:00
|
|
|
|
:returns: The answer string
|
2016-10-17 19:58:51 -06:00
|
|
|
|
"""
|
|
|
|
|
|
2016-11-28 15:17:12 -07:00
|
|
|
|
words = [self.randword() for i in range(word_count)]
|
|
|
|
|
answer = sep.join(words)
|
2016-10-22 10:35:55 -06:00
|
|
|
|
self.answers.append(answer)
|
2016-10-17 19:58:51 -06:00
|
|
|
|
return answer
|
|
|
|
|
|
2017-01-20 17:07:36 -07:00
|
|
|
|
hexdump_stdch = stdch = (
|
|
|
|
|
'················'
|
|
|
|
|
'················'
|
|
|
|
|
' !"#$%&\'()*+,-./'
|
|
|
|
|
'0123456789:;<=>?'
|
|
|
|
|
'@ABCDEFGHIJKLMNO'
|
|
|
|
|
'PQRSTUVWXYZ[\]^_'
|
|
|
|
|
'`abcdefghijklmno'
|
|
|
|
|
'pqrstuvwxyz{|}~·'
|
|
|
|
|
'················'
|
|
|
|
|
'················'
|
|
|
|
|
'················'
|
|
|
|
|
'················'
|
|
|
|
|
'················'
|
|
|
|
|
'················'
|
|
|
|
|
'················'
|
|
|
|
|
'················'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def hexdump(self, buf, charset=hexdump_stdch, gap=('<EFBFBD>', '⌷')):
|
|
|
|
|
hexes, chars = [], []
|
|
|
|
|
out = []
|
|
|
|
|
|
|
|
|
|
for b in buf:
|
|
|
|
|
if len(chars) == 16:
|
|
|
|
|
out.append((hexes, chars))
|
|
|
|
|
hexes, chars = [], []
|
|
|
|
|
|
|
|
|
|
if b is None:
|
|
|
|
|
h, c = gap
|
|
|
|
|
else:
|
|
|
|
|
h = '{:02x}'.format(b)
|
|
|
|
|
c = charset[b]
|
|
|
|
|
chars.append(c)
|
|
|
|
|
hexes.append(h)
|
|
|
|
|
|
|
|
|
|
out.append((hexes, chars))
|
|
|
|
|
|
|
|
|
|
offset = 0
|
|
|
|
|
elided = False
|
|
|
|
|
lastchars = None
|
|
|
|
|
self.body.write('<pre>')
|
|
|
|
|
for hexes, chars in out:
|
|
|
|
|
if chars == lastchars:
|
2019-07-10 16:33:45 -06:00
|
|
|
|
offset += len(chars)
|
2017-01-20 17:07:36 -07:00
|
|
|
|
if not elided:
|
|
|
|
|
self.body.write('*\n')
|
|
|
|
|
elided = True
|
|
|
|
|
continue
|
|
|
|
|
lastchars = chars[:]
|
|
|
|
|
elided = False
|
|
|
|
|
|
|
|
|
|
pad = 16 - len(chars)
|
|
|
|
|
hexes += [' '] * pad
|
|
|
|
|
|
|
|
|
|
self.body.write('{:08x} '.format(offset))
|
|
|
|
|
self.body.write(' '.join(hexes[:8]))
|
|
|
|
|
self.body.write(' ')
|
|
|
|
|
self.body.write(' '.join(hexes[8:]))
|
|
|
|
|
self.body.write(' |')
|
2018-09-27 15:07:03 -06:00
|
|
|
|
self.body.write(html.escape(''.join(chars)))
|
2017-01-20 17:07:36 -07:00
|
|
|
|
self.body.write('|\n')
|
|
|
|
|
offset += len(chars)
|
|
|
|
|
self.body.write('{:08x}\n'.format(offset))
|
|
|
|
|
self.body.write('</pre>')
|
|
|
|
|
|
2017-01-30 12:13:02 -07:00
|
|
|
|
def get_authors(self):
|
|
|
|
|
return self.authors or [self.author]
|
|
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
|
def get_body(self):
|
|
|
|
|
return self.body.getvalue()
|
|
|
|
|
|
|
|
|
|
def html_body(self):
|
2016-10-17 21:26:56 -06:00
|
|
|
|
"""Format and return the markdown for the puzzle body."""
|
2016-11-28 15:17:12 -07:00
|
|
|
|
return mistune.markdown(self.get_body(), escape=False)
|
2019-03-24 20:34:01 -06:00
|
|
|
|
|
2018-10-02 19:21:54 -06:00
|
|
|
|
def package(self, answers=False):
|
|
|
|
|
"""Return a dict packaging of the puzzle."""
|
2019-03-24 20:34:01 -06:00
|
|
|
|
|
2018-10-02 19:21:54 -06:00
|
|
|
|
files = [fn for fn,f in self.files.items() if f.visible]
|
|
|
|
|
return {
|
2019-08-15 16:32:28 -06:00
|
|
|
|
'authors': self.get_authors(),
|
2018-10-02 19:21:54 -06:00
|
|
|
|
'hashes': self.hashes(),
|
|
|
|
|
'files': files,
|
|
|
|
|
'scripts': self.scripts,
|
2019-02-24 17:02:28 -07:00
|
|
|
|
'pattern': self.pattern,
|
2018-10-02 19:21:54 -06:00
|
|
|
|
'body': self.html_body(),
|
2019-07-09 10:36:01 -06:00
|
|
|
|
'objective': self.objective,
|
|
|
|
|
'success': self.success,
|
|
|
|
|
'solution': self.solution,
|
2019-08-02 12:02:14 -06:00
|
|
|
|
'ksas': self.ksas,
|
2018-10-02 19:21:54 -06:00
|
|
|
|
}
|
2016-10-22 10:35:55 -06:00
|
|
|
|
|
|
|
|
|
def hashes(self):
|
|
|
|
|
"Return a list of answer hashes"
|
|
|
|
|
|
2019-02-24 11:53:22 -07:00
|
|
|
|
return [djb2hash(a) for a in self.answers]
|
2016-10-16 20:32:00 -06:00
|
|
|
|
|
2016-10-17 23:02:05 -06:00
|
|
|
|
|
|
|
|
|
class Category:
|
|
|
|
|
def __init__(self, path, seed):
|
|
|
|
|
self.path = path
|
|
|
|
|
self.seed = seed
|
2016-10-20 17:07:34 -06:00
|
|
|
|
self.catmod = None
|
|
|
|
|
|
2017-02-02 11:34:57 -07:00
|
|
|
|
try:
|
|
|
|
|
self.catmod = loadmod('category', os.path.join(path, 'category.py'))
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
self.catmod = None
|
|
|
|
|
|
|
|
|
|
def pointvals(self):
|
|
|
|
|
if self.catmod:
|
|
|
|
|
with pushd(self.path):
|
|
|
|
|
pointvals = self.catmod.pointvals()
|
2016-10-24 21:09:04 -06:00
|
|
|
|
else:
|
2017-02-02 11:34:57 -07:00
|
|
|
|
pointvals = []
|
|
|
|
|
for fpath in glob.glob(os.path.join(self.path, "[0-9]*")):
|
2016-10-24 21:09:04 -06:00
|
|
|
|
pn = os.path.basename(fpath)
|
|
|
|
|
points = int(pn)
|
2017-02-02 11:34:57 -07:00
|
|
|
|
pointvals.append(points)
|
|
|
|
|
return sorted(pointvals)
|
2016-10-17 23:02:05 -06:00
|
|
|
|
|
|
|
|
|
def puzzle(self, points):
|
2016-10-22 10:35:55 -06:00
|
|
|
|
puzzle = Puzzle(self.seed, points)
|
|
|
|
|
path = os.path.join(self.path, str(points))
|
|
|
|
|
if self.catmod:
|
2016-10-24 21:49:32 -06:00
|
|
|
|
with pushd(self.path):
|
|
|
|
|
self.catmod.make(points, puzzle)
|
2016-10-24 21:09:04 -06:00
|
|
|
|
else:
|
2019-08-15 16:30:48 -06:00
|
|
|
|
with pushd(self.path):
|
|
|
|
|
puzzle.read_directory(path)
|
2016-10-22 10:35:55 -06:00
|
|
|
|
return puzzle
|
2016-10-17 23:02:05 -06:00
|
|
|
|
|
2017-01-05 16:50:41 -07:00
|
|
|
|
def __iter__(self):
|
2017-02-02 11:34:57 -07:00
|
|
|
|
for points in self.pointvals():
|
2016-10-17 23:02:05 -06:00
|
|
|
|
yield self.puzzle(points)
|