devel-server use new Puzzles obj. Needs cleanup.

This commit is contained in:
Neale Pickett 2016-10-18 05:02:05 +00:00
parent 274eabfdc0
commit caa8835f05
3 changed files with 48 additions and 20 deletions

View File

@ -1,4 +1,4 @@
#!/usr/local/bin/python3 #!/usr/bin/env python3
import glob import glob
import http.server import http.server
@ -15,6 +15,9 @@ except ImportError:
NOT_FOUND = 404 NOT_FOUND = 404
OK = 200 OK = 200
# XXX: This will eventually cause a problem. Do something more clever here.
seed = 1
def page(title, body): def page(title, body):
return """<!DOCTYPE html> return """<!DOCTYPE html>
@ -91,25 +94,22 @@ you are a fool.
elif len(parts) == 3: elif len(parts) == 3:
# List all point values in a category # List all point values in a category
body.append("# Puzzles in category `{}`".format(parts[2])) body.append("# Puzzles in category `{}`".format(parts[2]))
puzz = [] fpath = os.path.join("puzzles", parts[2])
for i in glob.glob(os.path.join("puzzles", parts[2], "*.moth")): cat = puzzles.Category(fpath, seed)
base = os.path.basename(i) for points in cat.pointvals:
root, _ = os.path.splitext(base) body.append("* [puzzles/{cat}/{points}](/puzzles/{cat}/{points}/)".format(cat=parts[2], points=points))
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))
elif len(parts) == 4: elif len(parts) == 4:
body.append("# {} puzzle {}".format(parts[2], parts[3])) body.append("# {} puzzle {}".format(parts[2], parts[3]))
with open("puzzles/{}/{}.moth".format(parts[2], parts[3]), encoding="utf-8") as f: fpath = os.path.join("puzzles", parts[2])
p = puzzles.Puzzle(f) cat = puzzles.Category(fpath, seed)
body.append("* Author: `{}`".format(p.fields.get("author"))) p = cat.puzzle(int(parts[3]))
body.append("* Summary: `{}`".format(p.fields.get("summary"))) body.append("* Author: `{}`".format(p['author']))
body.append("* Summary: `{}`".format(p['summary']))
body.append('') body.append('')
body.append("## Body") body.append("## Body")
body.append(p.body) body.append(p.body)
body.append("## Answers:") body.append("## Answers")
for a in p.answers: for a in p['answers']:
body.append("* `{}`".format(a)) body.append("* `{}`".format(a))
body.append("") body.append("")
else: else:

View File

@ -27,7 +27,7 @@ PuzzleFile = namedtuple('PuzzleFile', ['path', 'handle', 'name', 'visible'])
class Puzzle: class Puzzle:
KNOWN_KEYS = [ KNOWN_KEYS = [
'file', 'files',
'resource', 'resource',
'temp_file', 'temp_file',
'answer', 'answer',
@ -60,7 +60,7 @@ class Puzzle:
self.body = '' self.body = ''
if not os.path.exists(path): 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.isfile(path): elif os.path.isfile(path):
try: try:
# Expected format is path/<points_int>.moth # Expected format is path/<points_int>.moth
@ -81,8 +81,8 @@ class Puzzle:
files = os.listdir(path) files = os.listdir(path)
if 'config.moth' in files: if 'puzzle.moth' in files:
self._read_config(open(os.path.join(path, 'config.moth'))) self._read_config(open(os.path.join(path, 'puzzle.moth')))
if 'make.py' in files: if 'make.py' in files:
# Good Lord this is dangerous as fuck. # Good Lord this is dangerous as fuck.
@ -93,7 +93,7 @@ class Puzzle:
else: else:
raise ValueError("Unacceptable file type for puzzle at {}".format(path)) 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) self.rand = random.Random(self._seed)
# Set our 'files' as a dict, since we want register them uniquely by name. # Set our 'files' as a dict, since we want register them uniquely by name.
@ -246,3 +246,23 @@ if __name__ == '__main__':
for points in sorted(puzzles): for points in sorted(puzzles):
puzzle = puzzles[points] puzzle = puzzles[points]
print(puzzle.secrets()) 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)

8
setup.cfg Normal file
View File

@ -0,0 +1,8 @@
[flake8]
# flake8 is an automated code formatting pedant.
# Use it, please.
#
# python3 -m flake8 .
#
ignore = E501
exclude = .git