2016-10-16 20:32:00 -06:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import argparse
|
2016-10-17 19:58:51 -06:00
|
|
|
from collections import defaultdict, namedtuple
|
2016-10-16 20:32:00 -06:00
|
|
|
import glob
|
2016-10-17 19:58:51 -06:00
|
|
|
import hashlib
|
|
|
|
from importlib.machinery import SourceFileLoader
|
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-17 19:58:51 -06:00
|
|
|
import tempfile
|
2016-10-16 20:32:00 -06:00
|
|
|
|
|
|
|
messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
|
2016-10-16 20:32:00 -06:00
|
|
|
def djb2hash(buf):
|
|
|
|
h = 5381
|
|
|
|
for c in buf:
|
|
|
|
h = ((h * 33) + c) & 0xffffffff
|
|
|
|
return h
|
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
# We use a named tuple rather than a full class, because any random name generation has
|
|
|
|
# to be done with Puzzle's random number generator, and it's cleaner to not pass that around.
|
|
|
|
PuzzleFile = namedtuple('PuzzleFile', ['path', 'handle', 'name', 'visible'])
|
|
|
|
|
2016-10-17 20:28:24 -06:00
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
class Puzzle:
|
|
|
|
|
|
|
|
KNOWN_KEYS = [
|
2016-10-17 23:02:05 -06:00
|
|
|
'files',
|
2016-10-17 19:58:51 -06:00
|
|
|
'resource',
|
|
|
|
'temp_file',
|
|
|
|
'answer',
|
|
|
|
'points',
|
|
|
|
'author',
|
|
|
|
'summary'
|
|
|
|
]
|
|
|
|
REQUIRED_KEYS = [
|
|
|
|
'author',
|
|
|
|
'answer',
|
|
|
|
'points'
|
|
|
|
]
|
|
|
|
SINGULAR_KEYS = [
|
|
|
|
'points'
|
|
|
|
]
|
|
|
|
|
|
|
|
# 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'))]
|
|
|
|
|
|
|
|
def __init__(self, path, category_seed):
|
2016-10-17 13:24:54 -06:00
|
|
|
super().__init__()
|
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
self._dict = defaultdict(lambda: [])
|
|
|
|
if os.path.isdir(path):
|
|
|
|
self._puzzle_dir = path
|
|
|
|
else:
|
|
|
|
self._puzzle_dir = None
|
2016-10-16 20:32:00 -06:00
|
|
|
self.message = bytes(random.choice(messageChars) for i in range(20))
|
2016-10-17 13:24:54 -06:00
|
|
|
self.body = ''
|
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
if not os.path.exists(path):
|
2016-10-17 23:02:05 -06:00
|
|
|
raise ValueError("No puzzle at path: {}".format(path))
|
2016-10-17 19:58:51 -06:00
|
|
|
elif os.path.isfile(path):
|
|
|
|
try:
|
|
|
|
# Expected format is path/<points_int>.moth
|
|
|
|
self['points'] = int(os.path.split(path)[-1].split('.')[0])
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
raise ValueError("Invalid puzzle config. "
|
|
|
|
"Expected something like <point_value>.moth")
|
|
|
|
|
|
|
|
stream = open(path)
|
|
|
|
self._read_config(stream)
|
|
|
|
elif os.path.isdir(path):
|
|
|
|
try:
|
|
|
|
# Expected format is path/<points_int>.moth
|
|
|
|
self['points'] = int(os.path.split(path)[-1])
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
raise ValueError("Invalid puzzle config. Expected an integer point value for a "
|
|
|
|
"directory name.")
|
|
|
|
|
|
|
|
files = os.listdir(path)
|
|
|
|
|
2016-10-17 23:02:05 -06:00
|
|
|
if 'puzzle.moth' in files:
|
|
|
|
self._read_config(open(os.path.join(path, 'puzzle.moth')))
|
2016-10-17 19:58:51 -06:00
|
|
|
|
|
|
|
if 'make.py' in files:
|
|
|
|
# Good Lord this is dangerous as fuck.
|
|
|
|
loader = SourceFileLoader('puzzle_mod', os.path.join(path, 'make.py'))
|
|
|
|
puzzle_mod = loader.load_module()
|
|
|
|
if hasattr(puzzle_mod, 'make'):
|
|
|
|
puzzle_mod.make(self)
|
|
|
|
else:
|
|
|
|
raise ValueError("Unacceptable file type for puzzle at {}".format(path))
|
|
|
|
|
2016-10-17 23:02:05 -06:00
|
|
|
self._seed = category_seed * self['points']
|
2016-10-17 19:58:51 -06:00
|
|
|
self.rand = random.Random(self._seed)
|
|
|
|
|
|
|
|
# Set our 'files' as a dict, since we want register them uniquely by name.
|
|
|
|
self['files'] = dict()
|
|
|
|
|
|
|
|
def _read_config(self, stream):
|
|
|
|
"""Read a configuration file (ISO 2822)"""
|
2016-10-16 20:32:00 -06:00
|
|
|
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()
|
2016-10-17 19:58:51 -06:00
|
|
|
self[key] = val
|
2016-10-16 20:32:00 -06:00
|
|
|
else:
|
|
|
|
body.append(line)
|
2016-10-17 19:58:51 -06:00
|
|
|
self.body = ''.join(body)
|
|
|
|
|
|
|
|
def random_hash(self):
|
|
|
|
"""Create a random hash from our number generator suitable for use as a filename."""
|
|
|
|
return hashlib.sha1(str(self.rand.random()).encode('ascii')).digest()
|
|
|
|
|
|
|
|
def _puzzle_file(self, path, name, visible=True):
|
|
|
|
"""Make a puzzle file instance for the given file.
|
|
|
|
:param path: The path to the file
|
|
|
|
:param name: The name of the file. If set to None, the published file will have
|
|
|
|
a random hash as a name and have visible set to False.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Make sure it actually exists.
|
|
|
|
if not os.path.exists(path):
|
|
|
|
raise ValueError("Included file {} does not exist.")
|
|
|
|
|
|
|
|
file = open(path, 'rb')
|
|
|
|
|
|
|
|
return PuzzleFile(path=path, handle=file, name=name, visible=visible)
|
|
|
|
|
|
|
|
def make_file(self, name=None, mode='rw+b'):
|
|
|
|
"""Get a file object for adding dynamically generated data to the puzzle.
|
|
|
|
:param name: The name of the file for links within the puzzle. If this is None,
|
|
|
|
the file will be hidden with a random hash as the name.
|
|
|
|
:return: A file object for writing
|
|
|
|
"""
|
|
|
|
|
|
|
|
file = tempfile.TemporaryFile(mode=mode, delete=False)
|
|
|
|
|
|
|
|
self._dict['files'].append(self._puzzle_file(file.name, name))
|
|
|
|
|
|
|
|
return file
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
|
|
|
|
if key in ('file', 'resource', 'hidden') and self._puzzle_dir is None:
|
|
|
|
raise KeyError("Cannot set a puzzle file for single file puzzles.")
|
2016-10-16 20:32:00 -06:00
|
|
|
|
|
|
|
if key == 'answer':
|
2016-10-17 19:58:51 -06:00
|
|
|
# Handle adding answers to the puzzle
|
|
|
|
self._dict['hashes'].append(djb2hash(value.encode('utf8')))
|
|
|
|
self._dict['answers'].append(value)
|
|
|
|
elif key == 'file':
|
|
|
|
# Handle adding files to the puzzle
|
|
|
|
path = os.path.join(self._puzzle_dir, 'files', value)
|
|
|
|
self._dict['files'][value] = self._puzzle_file(path, value)
|
|
|
|
elif key == 'resource':
|
|
|
|
# Handle adding category files to the puzzle
|
|
|
|
path = os.path.join(self._puzzle_dir, '../res', value)
|
|
|
|
self._dict['files'].append(self._puzzle_file(path, value))
|
|
|
|
elif key == 'hidden':
|
|
|
|
# Handle adding secret, 'hidden' files to the puzzle.
|
|
|
|
path = os.path.join(self._puzzle_dir, 'files', value)
|
|
|
|
name = self.random_hash()
|
|
|
|
self._dict['files'].append(self._puzzle_file(path, name, visible=False))
|
|
|
|
elif key in self.SINGULAR_KEYS:
|
|
|
|
# These keys can only have one value
|
|
|
|
self._dict[key] = value
|
|
|
|
elif key in self.KNOWN_KEYS:
|
|
|
|
self._dict[key].append(value)
|
|
|
|
else:
|
|
|
|
raise KeyError("Invalid Attribute: {}".format(key))
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self._dict[item]
|
|
|
|
|
|
|
|
def make_answer(self, word_count, sep=b' '):
|
|
|
|
"""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.
|
|
|
|
:returns: The answer bytes
|
|
|
|
"""
|
|
|
|
|
|
|
|
if type(sep) == str:
|
|
|
|
sep = sep.encode('ascii')
|
|
|
|
|
|
|
|
answer = sep.join(self.rand.sample(self.ANSWER_WORDS, word_count))
|
|
|
|
self['answer'] = answer
|
|
|
|
|
|
|
|
return answer
|
|
|
|
|
2016-10-16 20:32:00 -06:00
|
|
|
def htmlify(self):
|
|
|
|
return mistune.markdown(self.body)
|
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
def publish(self, dest):
|
|
|
|
"""Deploy the puzzle to the given directory, and return the info needed for describing
|
|
|
|
the puzzle and accepting answers in MOTH."""
|
|
|
|
|
|
|
|
if not os.path.exists(dest):
|
|
|
|
raise ValueError("Puzzle destination directory does not exist.")
|
|
|
|
|
|
|
|
# Delete the original directory
|
|
|
|
|
|
|
|
# Save puzzle html file
|
|
|
|
|
|
|
|
# Copy over all the files.
|
|
|
|
|
2016-10-16 20:32:00 -06:00
|
|
|
obj = {
|
2016-10-17 13:24:54 -06:00
|
|
|
'author': self['author'],
|
2016-10-17 19:58:51 -06:00
|
|
|
'hashes': self['hashes'],
|
2016-10-16 20:32:00 -06:00
|
|
|
'body': self.htmlify(),
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def secrets(self):
|
|
|
|
obj = {
|
2016-10-17 19:58:51 -06:00
|
|
|
'answers': self['answers'],
|
2016-10-17 13:24:54 -06:00
|
|
|
'summary': self['summary'],
|
2016-10-16 20:32:00 -06:00
|
|
|
}
|
|
|
|
return obj
|
2016-10-17 20:28:24 -06:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-10-16 20:32:00 -06:00
|
|
|
parser = argparse.ArgumentParser(description='Build a puzzle category')
|
|
|
|
parser.add_argument('puzzledir', nargs='+', help='Directory of puzzle source')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
for puzzledir in args.puzzledir:
|
|
|
|
puzzles = {}
|
|
|
|
secrets = {}
|
|
|
|
for puzzlePath in glob.glob(os.path.join(puzzledir, "*.moth")):
|
|
|
|
filename = os.path.basename(puzzlePath)
|
|
|
|
points, ext = os.path.splitext(filename)
|
|
|
|
points = int(points)
|
2016-10-17 19:58:51 -06:00
|
|
|
puzzle = Puzzle(puzzlePath, "test")
|
2016-10-16 20:32:00 -06:00
|
|
|
puzzles[points] = puzzle
|
|
|
|
|
|
|
|
for points in sorted(puzzles):
|
|
|
|
puzzle = puzzles[points]
|
|
|
|
print(puzzle.secrets())
|
2016-10-17 23:02:05 -06:00
|
|
|
|
|
|
|
|
|
|
|
class Category:
|
|
|
|
def __init__(self, path, seed):
|
|
|
|
self.path = path
|
|
|
|
self.seed = seed
|
|
|
|
self.pointvals = []
|
|
|
|
for fpath in glob.glob(os.path.join(path, "[0-9]*")):
|
|
|
|
pn = os.path.basename(fpath)
|
|
|
|
points = int(pn)
|
|
|
|
self.pointvals.append(points)
|
|
|
|
self.pointvals.sort()
|
|
|
|
|
|
|
|
def puzzle(self, points):
|
|
|
|
path = os.path.join(self.path, str(points))
|
|
|
|
return Puzzle(path, self.seed)
|
|
|
|
|
|
|
|
def puzzles(self):
|
|
|
|
for points in self.pointvals:
|
|
|
|
yield self.puzzle(points)
|