diff --git a/devel-server.py b/devel-server.py index b1d0a3b..9ae36ff 100755 --- a/devel-server.py +++ b/devel-server.py @@ -1,4 +1,4 @@ -#!/usr/local/bin/python3 +#!/usr/bin/env python3 import glob import http.server @@ -15,6 +15,9 @@ except ImportError: NOT_FOUND = 404 OK = 200 +# XXX: This will eventually cause a problem. Do something more clever here. +seed = 1 + def page(title, body): return """ @@ -91,25 +94,22 @@ you are a fool. elif len(parts) == 3: # List all point values in a category body.append("# Puzzles in category `{}`".format(parts[2])) - puzz = [] - for i in glob.glob(os.path.join("puzzles", parts[2], "*.moth")): - base = os.path.basename(i) - root, _ = os.path.splitext(base) - points = int(root) - puzz.append(points) - for puzzle in sorted(puzz): - body.append("* [puzzles/{cat}/{points}](/puzzles/{cat}/{points}/)".format(cat=parts[2], points=puzzle)) + fpath = os.path.join("puzzles", parts[2]) + cat = puzzles.Category(fpath, seed) + for points in cat.pointvals: + body.append("* [puzzles/{cat}/{points}](/puzzles/{cat}/{points}/)".format(cat=parts[2], points=points)) elif len(parts) == 4: body.append("# {} puzzle {}".format(parts[2], parts[3])) - with open("puzzles/{}/{}.moth".format(parts[2], parts[3]), encoding="utf-8") as f: - p = puzzles.Puzzle(f) - body.append("* Author: `{}`".format(p.fields.get("author"))) - body.append("* Summary: `{}`".format(p.fields.get("summary"))) + fpath = os.path.join("puzzles", parts[2]) + cat = puzzles.Category(fpath, seed) + p = cat.puzzle(int(parts[3])) + body.append("* Author: `{}`".format(p['author'])) + body.append("* Summary: `{}`".format(p['summary'])) body.append('') body.append("## Body") body.append(p.body) - body.append("## Answers:") - for a in p.answers: + body.append("## Answers") + for a in p['answers']: body.append("* `{}`".format(a)) body.append("") else: diff --git a/puzzles.py b/puzzles.py index ab4471e..634049e 100644 --- a/puzzles.py +++ b/puzzles.py @@ -64,7 +64,7 @@ class Puzzle: self.body = '' if not os.path.exists(path): - raise ValueError("No puzzle at path: {]".format(path)) + raise ValueError("No puzzle at path: {}".format(path)) elif os.path.isdir(path): # Expected format is path/.moth pathname = os.path.split(path)[-1] @@ -86,7 +86,7 @@ class Puzzle: else: raise ValueError("Unacceptable file type for puzzle at {}".format(path)) - self._seed = hashlib.sha1(category_seed + bytes(self['points'])).digest() + self._seed = category_seed * self['points'] self.rand = random.Random(self._seed) # Set our 'files' as a dict, since we want register them uniquely by name. @@ -269,3 +269,23 @@ if __name__ == '__main__': for points in sorted(puzzles): puzzle = puzzles[points] print(puzzle.secrets()) + + +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) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..3bf77b8 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,8 @@ +[flake8] +# flake8 is an automated code formatting pedant. +# Use it, please. +# +# python3 -m flake8 . +# +ignore = E501 +exclude = .git \ No newline at end of file