Get generated puzzles working

This commit is contained in:
Neale Pickett 2016-10-18 09:51:33 -06:00
parent 12c64ad48a
commit 2a22f3a684
1 changed files with 26 additions and 28 deletions

View File

@ -55,6 +55,9 @@ class Puzzle:
super().__init__() super().__init__()
if not os.path.isdir(path):
raise ValueError("No such directory: {}".format(path))
self._dict = defaultdict(lambda: []) self._dict = defaultdict(lambda: [])
if os.path.isdir(path): if os.path.isdir(path):
self._puzzle_dir = path self._puzzle_dir = path
@ -63,17 +66,20 @@ class Puzzle:
self.message = bytes(random.choice(messageChars) for i in range(20)) self.message = bytes(random.choice(messageChars) for i in range(20))
self.body = '' self.body = ''
if not os.path.exists(path): # A list of temporary files we've created that will need to be deleted.
raise ValueError("No puzzle at path: {}".format(path)) self._temp_files = []
elif os.path.isdir(path):
# Expected format is path/<points_int>.moth # Expected format is path/<points_int>.moth
pathname = os.path.split(path)[-1] pathname = os.path.split(path)[-1]
try: try:
self.points = int(pathname) self.points = int(pathname)
except ValueError: except ValueError:
pass raise ValueError("Directory name must be a point value: {}".format(path))
files = os.listdir(path) files = os.listdir(path)
self._seed = category_seed * self.points
self.rand = random.Random(self._seed)
if 'puzzle.moth' in files: if 'puzzle.moth' in files:
self._read_config(open(os.path.join(path, 'puzzle.moth'))) self._read_config(open(os.path.join(path, 'puzzle.moth')))
@ -82,15 +88,10 @@ class Puzzle:
loader = SourceFileLoader('puzzle_mod', os.path.join(path, 'puzzle.py')) loader = SourceFileLoader('puzzle_mod', os.path.join(path, 'puzzle.py'))
puzzle_mod = loader.load_module() puzzle_mod = loader.load_module()
if hasattr(puzzle_mod, 'make'): if hasattr(puzzle_mod, 'make'):
self.body = '# `puzzle.body` was not set by the `make` function'
puzzle_mod.make(self) puzzle_mod.make(self)
else: else:
raise ValueError("Unacceptable file type for puzzle at {}".format(path)) self.body = '# `puzzle.py` does not define a `make` function'
self._seed = category_seed * self.points
self.rand = random.Random(self._seed)
# A list of temporary files we've created that will need to be deleted.
self._temp_files = []
def cleanup(self): def cleanup(self):
"""Cleanup any outstanding temporary files.""" """Cleanup any outstanding temporary files."""
@ -214,16 +215,13 @@ class Puzzle:
def __getitem__(self, item): def __getitem__(self, item):
return self._dict[item.lower()] return self._dict[item.lower()]
def make_answer(self, word_count, sep=b' '): def make_answer(self, word_count, sep=' '):
"""Generate and return a new answer. It's automatically added to the puzzle answer list. """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 int word_count: The number of words to include in the answer.
:param str|bytes sep: The word separator. :param str|bytes sep: The word separator.
:returns: The answer bytes :returns: The answer string
""" """
if type(sep) == str:
sep = sep.encode('ascii')
answer = sep.join(self.rand.sample(self.ANSWER_WORDS, word_count)) answer = sep.join(self.rand.sample(self.ANSWER_WORDS, word_count))
self['answer'] = answer self['answer'] = answer