From f06de852e846631b90e6c7c195913da1abce1f68 Mon Sep 17 00:00:00 2001 From: Paul Ferrell Date: Mon, 17 Oct 2016 13:24:54 -0600 Subject: [PATCH] Reworked some basics of how the Puzzle class works. Shouldn't look to different from the outside. --- .gitignore | 3 ++- devel-server.py | 3 +-- puzzles.py | 48 +++++++++++++++++++++++++++--------------------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index c3c1634..a22d658 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,9 @@ *# *.pyc *.o +.idea ./bin/ build/ cache/ target/ -puzzles \ No newline at end of file +puzzles diff --git a/devel-server.py b/devel-server.py index f4ae8d4..732a87b 100755 --- a/devel-server.py +++ b/devel-server.py @@ -1,7 +1,6 @@ #!/usr/bin/python3 import glob -import http import http.server import mistune import os @@ -9,7 +8,7 @@ import pathlib import puzzles import socketserver -if hasattr(http, 'HTTPStatus'): +if hasattr(http.server, 'HTTPStatus'): HTTPStatus = http.HTTPStatus else: class HTTPStatus: diff --git a/puzzles.py b/puzzles.py index e2c79f5..e844a00 100644 --- a/puzzles.py +++ b/puzzles.py @@ -1,12 +1,13 @@ #!/usr/bin/python3 -import hmac -import base64 import argparse +import base64 import glob +import hmac import json -import os import mistune +import multidict +import os import random messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' @@ -17,12 +18,19 @@ def djb2hash(buf): h = ((h * 33) + c) & 0xffffffff return h -class Puzzle: - def __init__(self, stream): +class Puzzle(multidict.MultiDict): + + def __init__(self, seed): + super().__init__() + self.message = bytes(random.choice(messageChars) for i in range(20)) - self.fields = {} - self.answers = [] - self.hashes = [] + self.body = '' + + self.rand = random.Random(seed) + + @classmethod + def from_stream(cls, stream): + pzl = cls(None) body = [] header = True @@ -35,34 +43,32 @@ class Puzzle: key, val = line.split(':', 1) key = key.lower() val = val.strip() - self._add_field(key, val) + pzl.add(key, val) else: body.append(line) - self.body = ''.join(body) + pzl.body = ''.join(body) + return pzl - def _add_field(self, key, val): + def add(self, key, value): + super().add(key, value) if key == 'answer': - h = djb2hash(val.encode('utf8')) - self.answers.append(val) - self.hashes.append(h) - else: - self.fields[key] = val + super().add(hash, djb2hash(value.encode('utf8'))) def htmlify(self): return mistune.markdown(self.body) def publish(self): obj = { - 'author': self.fields['author'], - 'hashes': self.hashes, + 'author': self['author'], + 'hashes': self.getall('hash'), 'body': self.htmlify(), } return obj def secrets(self): obj = { - 'answers': self.answers, - 'summary': self.fields['summary'], + 'answers': self.getall('answer'), + 'summary': self['summary'], } return obj @@ -78,7 +84,7 @@ if __name__ == '__main__': filename = os.path.basename(puzzlePath) points, ext = os.path.splitext(filename) points = int(points) - puzzle = Puzzle(open(puzzlePath)) + puzzle = Puzzle.from_stream(open(puzzlePath)) puzzles[points] = puzzle for points in sorted(puzzles):