mirror of https://github.com/dirtbags/moth.git
Adding support for Python libraries inside of puzzles and categories
This commit is contained in:
parent
d5b8d5f8f3
commit
1bb777e512
|
@ -210,7 +210,7 @@ sessionStorage.setItem("id", "devel-server")
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(body.encode('utf-8'))
|
self.wfile.write(body.encode('utf-8'))
|
||||||
endpoints.append((r"/", handle_index))
|
endpoints.append((r"/", handle_index))
|
||||||
endpoints.append((r"/{ignored}", handle_index))
|
#endpoints.append((r"/{ignored}", handle_index))
|
||||||
|
|
||||||
|
|
||||||
def handle_theme_file(self):
|
def handle_theme_file(self):
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import copy
|
||||||
import glob
|
import glob
|
||||||
import hashlib
|
import hashlib
|
||||||
import html
|
import html
|
||||||
|
@ -11,6 +12,7 @@ import mistune
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
|
@ -26,9 +28,24 @@ def djb2hash(str):
|
||||||
def pushd(newdir):
|
def pushd(newdir):
|
||||||
curdir = os.getcwd()
|
curdir = os.getcwd()
|
||||||
os.chdir(newdir)
|
os.chdir(newdir)
|
||||||
|
# Force a copy of the old path, instead of just a reference
|
||||||
|
old_path = list(sys.path)
|
||||||
|
old_modules = copy.copy(sys.modules)
|
||||||
|
sys.path.append(newdir)
|
||||||
|
print("New path: %s" % (sys.path,))
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
|
# Restore the old path
|
||||||
|
to_remove = []
|
||||||
|
for module in sys.modules:
|
||||||
|
if module not in old_modules:
|
||||||
|
to_remove.append(module)
|
||||||
|
|
||||||
|
for module in to_remove:
|
||||||
|
del(sys.modules[module])
|
||||||
|
|
||||||
|
sys.path = old_path
|
||||||
os.chdir(curdir)
|
os.chdir(curdir)
|
||||||
|
|
||||||
|
|
||||||
|
@ -316,7 +333,8 @@ class Category:
|
||||||
with pushd(self.path):
|
with pushd(self.path):
|
||||||
self.catmod.make(points, puzzle)
|
self.catmod.make(points, puzzle)
|
||||||
else:
|
else:
|
||||||
puzzle.read_directory(path)
|
with pushd(self.path):
|
||||||
|
puzzle.read_directory(path)
|
||||||
return puzzle
|
return puzzle
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
import io
|
||||||
|
import categorylib # Category-level libraries can be imported here
|
||||||
|
|
||||||
|
def make(puzzle):
|
||||||
|
import puzzlelib # puzzle-level libraries can only be imported inside of the make function
|
||||||
|
puzzle.authors = ['donaldson']
|
||||||
|
puzzle.summary = 'more crazy stuff you can do with puzzle generation using Python libraries'
|
||||||
|
|
||||||
|
puzzle.body.write("## Crazy Things You Can Do With Puzzle Generation (part II)\n")
|
||||||
|
puzzle.body.write("\n")
|
||||||
|
puzzle.body.write("The source to this puzzle has some more advanced examples of stuff you can do in Python.\n")
|
||||||
|
puzzle.body.write("\n")
|
||||||
|
puzzle.body.write("1 == %s\n\n" % puzzlelib.getone(),)
|
||||||
|
puzzle.body.write("2 == %s\n\n" % categorylib.gettwo(),)
|
||||||
|
|
||||||
|
puzzle.answers.append('tea')
|
||||||
|
answer = puzzle.make_answer() # Generates a random answer, appending it to puzzle.answers too
|
||||||
|
puzzle.log("Answers: {}".format(puzzle.answers))
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
"""This is an example of a puzzle-level library.
|
||||||
|
|
||||||
|
This library can be imported by child puzzles using `import puzzlelib`
|
||||||
|
"""
|
||||||
|
|
||||||
|
def getone():
|
||||||
|
return 1
|
|
@ -0,0 +1,7 @@
|
||||||
|
"""This is an example of a category-level library.
|
||||||
|
|
||||||
|
This library can be imported by child puzzles using `import categorylib`
|
||||||
|
"""
|
||||||
|
|
||||||
|
def gettwo():
|
||||||
|
return 2
|
Loading…
Reference in New Issue