mirror of https://github.com/dirtbags/moth.git
Reworked some basics of how the Puzzle class works. Shouldn't look to different from the outside.
This commit is contained in:
parent
9840dd694e
commit
5072b23838
|
@ -2,8 +2,9 @@
|
||||||
*#
|
*#
|
||||||
*.pyc
|
*.pyc
|
||||||
*.o
|
*.o
|
||||||
|
.idea
|
||||||
./bin/
|
./bin/
|
||||||
build/
|
build/
|
||||||
cache/
|
cache/
|
||||||
target/
|
target/
|
||||||
puzzles
|
puzzles
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import glob
|
import glob
|
||||||
import http
|
|
||||||
import http.server
|
import http.server
|
||||||
import mistune
|
import mistune
|
||||||
import os
|
import os
|
||||||
|
@ -9,7 +8,7 @@ import pathlib
|
||||||
import puzzles
|
import puzzles
|
||||||
import socketserver
|
import socketserver
|
||||||
|
|
||||||
if hasattr(http, 'HTTPStatus'):
|
if hasattr(http.server, 'HTTPStatus'):
|
||||||
HTTPStatus = http.HTTPStatus
|
HTTPStatus = http.HTTPStatus
|
||||||
else:
|
else:
|
||||||
class HTTPStatus:
|
class HTTPStatus:
|
||||||
|
|
48
puzzles.py
48
puzzles.py
|
@ -1,12 +1,13 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import hmac
|
|
||||||
import base64
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import base64
|
||||||
import glob
|
import glob
|
||||||
|
import hmac
|
||||||
import json
|
import json
|
||||||
import os
|
|
||||||
import mistune
|
import mistune
|
||||||
|
import multidict
|
||||||
|
import os
|
||||||
import random
|
import random
|
||||||
|
|
||||||
messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
|
@ -17,12 +18,19 @@ def djb2hash(buf):
|
||||||
h = ((h * 33) + c) & 0xffffffff
|
h = ((h * 33) + c) & 0xffffffff
|
||||||
return h
|
return h
|
||||||
|
|
||||||
class Puzzle:
|
class Puzzle(multidict.MultiDict):
|
||||||
def __init__(self, stream):
|
|
||||||
|
def __init__(self, seed):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
self.message = bytes(random.choice(messageChars) for i in range(20))
|
self.message = bytes(random.choice(messageChars) for i in range(20))
|
||||||
self.fields = {}
|
self.body = ''
|
||||||
self.answers = []
|
|
||||||
self.hashes = []
|
self.rand = random.Random(seed)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_stream(cls, stream):
|
||||||
|
pzl = cls(None)
|
||||||
|
|
||||||
body = []
|
body = []
|
||||||
header = True
|
header = True
|
||||||
|
@ -35,34 +43,32 @@ class Puzzle:
|
||||||
key, val = line.split(':', 1)
|
key, val = line.split(':', 1)
|
||||||
key = key.lower()
|
key = key.lower()
|
||||||
val = val.strip()
|
val = val.strip()
|
||||||
self._add_field(key, val)
|
pzl.add(key, val)
|
||||||
else:
|
else:
|
||||||
body.append(line)
|
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':
|
if key == 'answer':
|
||||||
h = djb2hash(val.encode('utf8'))
|
super().add(hash, djb2hash(value.encode('utf8')))
|
||||||
self.answers.append(val)
|
|
||||||
self.hashes.append(h)
|
|
||||||
else:
|
|
||||||
self.fields[key] = val
|
|
||||||
|
|
||||||
def htmlify(self):
|
def htmlify(self):
|
||||||
return mistune.markdown(self.body)
|
return mistune.markdown(self.body)
|
||||||
|
|
||||||
def publish(self):
|
def publish(self):
|
||||||
obj = {
|
obj = {
|
||||||
'author': self.fields['author'],
|
'author': self['author'],
|
||||||
'hashes': self.hashes,
|
'hashes': self.getall('hash'),
|
||||||
'body': self.htmlify(),
|
'body': self.htmlify(),
|
||||||
}
|
}
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def secrets(self):
|
def secrets(self):
|
||||||
obj = {
|
obj = {
|
||||||
'answers': self.answers,
|
'answers': self.getall('answer'),
|
||||||
'summary': self.fields['summary'],
|
'summary': self['summary'],
|
||||||
}
|
}
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
@ -78,7 +84,7 @@ if __name__ == '__main__':
|
||||||
filename = os.path.basename(puzzlePath)
|
filename = os.path.basename(puzzlePath)
|
||||||
points, ext = os.path.splitext(filename)
|
points, ext = os.path.splitext(filename)
|
||||||
points = int(points)
|
points = int(points)
|
||||||
puzzle = Puzzle(open(puzzlePath))
|
puzzle = Puzzle.from_stream(open(puzzlePath))
|
||||||
puzzles[points] = puzzle
|
puzzles[points] = puzzle
|
||||||
|
|
||||||
for points in sorted(puzzles):
|
for points in sorted(puzzles):
|
||||||
|
|
Loading…
Reference in New Issue