Make category-wide puzzles work

This commit is contained in:
Neale Pickett 2016-10-25 03:09:04 +00:00
parent 17fe6cc779
commit 5c5cb36530
2 changed files with 17 additions and 22 deletions

View File

@ -29,15 +29,16 @@ def page(title, body):
return """<!DOCTYPE html> return """<!DOCTYPE html>
<html> <html>
<head> <head>
<title>{}</title> <title>{title}</title>
<link rel="stylesheet" href="/files/src/www/res/style.css"> <link rel="stylesheet" href="/files/src/www/res/style.css">
</head> </head>
<body> <body>
<h1>{title}</h1>
<div id="preview" class="terminal"> <div id="preview" class="terminal">
{} {body}
</div> </div>
</body> </body>
</html>""".format(title, body) </html>""".format(title=title, body=body)
def mdpage(body): def mdpage(body):
@ -132,7 +133,7 @@ you are a fool.
if not cat: if not cat:
title = "Puzzle Categories" title = "Puzzle Categories"
body.write("<ul>") body.write("<ul>")
for i in glob.glob(os.path.join("puzzles", "*", "")): for i in sorted(glob.glob(os.path.join("puzzles", "*", ""))):
body.write('<li><a href="{}">{}</a></li>'.format(i, i)) body.write('<li><a href="{}">{}</a></li>'.format(i, i))
body.write("</ul>") body.write("</ul>")
elif not puzzle: elif not puzzle:
@ -155,6 +156,7 @@ you are a fool.
body.write("</ul>") body.write("</ul>")
body.write("<h2>Answers</h2>") body.write("<h2>Answers</h2>")
body.write("<ul>") body.write("<ul>")
assert puzzle.answers, 'No answers defined'
for a in puzzle.answers: for a in puzzle.answers:
body.write("<li><code>{}</code></li>".format(html.escape(a))) body.write("<li><code>{}</code></li>".format(html.escape(a)))
body.write("</ul>") body.write("</ul>")

View File

@ -177,24 +177,16 @@ class Category:
self.pointvals = [] self.pointvals = []
self.catmod = None self.catmod = None
try: if os.path.exists(os.path.join(path, 'category.py')):
catmod = SourceFileLoader( self.catmod = importlib.machinery.SourceFileLoader(
'catmod', 'catmod',
os.path.join(path, 'category.py')).load_module() os.path.join(path, 'category.py')).load_module()
assert all([ self.pointvals = self.catmod.points[:]
hasattr(catmod, 'make'), else:
hasattr(catmod, 'points'), for fpath in glob.glob(os.path.join(path, "[0-9]*")):
type(catmod.points) is list, pn = os.path.basename(fpath)
]) points = int(pn)
self.catmod = catmod self.pointvals.append(points)
self.pointvals.extend(catmod.points)
except:
pass
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() self.pointvals.sort()
@ -202,8 +194,9 @@ class Category:
puzzle = Puzzle(self.seed, points) puzzle = Puzzle(self.seed, points)
path = os.path.join(self.path, str(points)) path = os.path.join(self.path, str(points))
if self.catmod: if self.catmod:
self.catmod.make(p, points) self.catmod.make(points, puzzle)
puzzle.read_directory(path) else:
puzzle.read_directory(path)
return puzzle return puzzle
def puzzles(self): def puzzles(self):