further debugging; now appears to work properly

This commit is contained in:
Neale Pickett 2016-10-23 20:55:35 +00:00
parent e4b30617a6
commit 99a3502617
2 changed files with 28 additions and 18 deletions

View File

@ -113,15 +113,22 @@ you are a fool.
path = self.path.rstrip('/')
parts = path.split("/")
title = None
fpath = None
points = None
cat = None
puzzle = None
try:
fpath = os.path.join("puzzles", parts[2])
cat = moth.Category(path, seed)
puzzle = cat.puzzle(int(parts[3]))
points = int(parts[3])
except:
pass
if fpath:
cat = moth.Category(fpath, seed)
if points:
puzzle = cat.puzzle(int(parts[3]))
if not cat:
title = "Puzzle Categories"
body.write("<ul>")
@ -133,26 +140,26 @@ you are a fool.
title = "Puzzles in category `{}`".format(parts[2])
body.write("<ul>")
for points in cat.pointvals:
body.write('<li><a href="/puzzles/{cat}{points}">puzzles/{cat}{points}</a></li>'.format(cat=parts[2], points=points))
body.write('<li><a href="/puzzles/{cat}/{points}">puzzles/{cat}/{points}</a></li>'.format(cat=parts[2], points=points))
body.write("</ul>")
if len(parts) == 4:
elif len(parts) == 4:
# Serve up a puzzle
title = "{} puzzle {}".format(parts[2], parts[3])
body.write("<h2>Author</h2><p>{}</p>".format(puzzle.author))
body.write("<h2>Summary</h2><p>{}</p>".format(puzzle.summary))
body.write("<h2>Body</h2>")
body.write(puzzle.html_body())
body.write("<h2>Files</h2>")
body.write("<ul>")
for name in puzzle.files:
body.write('<li><a href="/{cat}/{points}/{filename}">{filename}</a></li>'
.format(cat=parts[2], points=puzzle.points, filename=name))
body.write("</ul>")
body.write("<h2>Answers</h2>")
body.write("<ul>")
for a in puzzle.answers:
body.write("<li><code>{}</code></li>".format(html.escape(a)))
body.write("</ul>")
body.write("<h2>Files</h2>")
body.write("<ul>")
for f in puzzle.files:
body.write('<li><a href="/{cat}/{points}/{filename}">{filename}</a></li>'
.format(cat=parts[2], points=puzzle.points, filename=f.name))
body.write("</ul>")
body.write("<h2>Author</h2><p>{}</p>".format(puzzle.author))
body.write("<h2>Summary</h2><p>{}</p>".format(puzzle.summary))
body.write("<h2>Debug Log</h2>")
body.write('<ul class="log">')
for l in puzzle.logs:
@ -195,7 +202,7 @@ you are a fool.
except OSError:
self.send_error(HTTPStatus.NOT_FOUND, "File not found")
return
if fspath.endswith(".md"):
if path.endswith(".md"):
ctype = "text/html; charset=utf-8"
content = mdpage(payload.decode('utf-8'))
payload = content.encode('utf-8')

View File

@ -9,6 +9,7 @@ import importlib.machinery
import mistune
import os
import random
import string
import tempfile
messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
@ -124,7 +125,7 @@ class Puzzle:
def random_hash(self):
"""Create a file basename (no extension) with our number generator."""
return ''.join(self.random.choice(string.ascii_lowercase) for i in range(8))
return ''.join(self.rand.choice(string.ascii_lowercase) for i in range(8))
def make_temp_file(self, name=None, visible=True):
"""Get a file object for adding dynamically generated data to the puzzle. When you're
@ -136,12 +137,14 @@ class Puzzle:
:return: A file object for writing
"""
stream = tempfile.TemporaryFile()
self.add_stream(stream, name, visible)
return stream
def add_stream(self, stream, name=None, visible=True):
if name is None:
name = self.random_hash()
stream = tempfile.TemporaryFile()
self.files[name] = PuzzleFile(stream, name, visible)
return stream
def make_answer(self, word_count, sep=' '):
"""Generate and return a new answer. It's automatically added to the puzzle answer list.
@ -150,7 +153,7 @@ class Puzzle:
:returns: The answer string
"""
answer = sep.join(self.rand.sample(self.ANSWER_WORDS, word_count))
answer = sep.join(self.rand.sample(ANSWER_WORDS, word_count))
self.answers.append(answer)
return answer