Allowing the program to be configured to target some directory to be served
by the web server, and also allowing the program to generate files for
multiple hunts, (putting each hunt into its own directory).
"""
import boto3
from boto3.dynamodb.conditions import Key
"""
import boto3
from boto3.dynamodb.conditions import Key
+WEBROOT = "/srv/halibut.cworth.org/www"
+
website = "https://halibut.cworth.org/"
#change this if we're using AWS or some other subdomain instead
website = "https://halibut.cworth.org/"
#change this if we're using AWS or some other subdomain instead
+def hunt_file(hunt, name):
+ """Return a path file 'name' within the given hunt.
+
+ This will be withing WEBROOT and in a hunt-specific path."""
+
+ return "{}/{}/{}".format(WEBROOT, hunt['channel_id'], name)
+
def filename_from_name(name):
"""Returns a string derived from name, but with all spaces and slashes
replaced with underscores, (for making a clean filename)"""
def filename_from_name(name):
"""Returns a string derived from name, but with all spaces and slashes
replaced with underscores, (for making a clean filename)"""
rounds = list(rounds)
rounds.sort()
rounds = list(rounds)
rounds.sort()
+ return hunt, puzzles, rounds
def round_stat(rnd, puzzles):
#Counts puzzles, solved, list of puzzles for a given round
def round_stat(rnd, puzzles):
#Counts puzzles, solved, list of puzzles for a given round
-def overview(puzzles, rounds):
+def overview(hunt, puzzles, rounds):
#big board, main page. saves as index.html
start = ['<!DOCTYPE html>\n',
'<html>\n',
#big board, main page. saves as index.html
start = ['<!DOCTYPE html>\n',
'<html>\n',
columns.append(' </div>\n')
end = ['</body>\n', '</html>\n']
html = start + expanding + columns + end
columns.append(' </div>\n')
end = ['</body>\n', '</html>\n']
html = start + expanding + columns + end
+ file = hunt_file(hunt, "index.html")
f = open(file, "w")
for line in html:
f.write(line)
f.close()
return None
f = open(file, "w")
for line in html:
f.write(line)
f.close()
return None
-def round_overview(rnd, puzzles):
+def round_overview(hunt, rnd, puzzles):
#inputs: round name, puzzles
#round overview page
#saves as (round name)_round.html, in case meta/round share names.
#inputs: round name, puzzles
#round overview page
#saves as (round name)_round.html, in case meta/round share names.
' </body>\n',
'</html>\n']
html = start + puzzle_list + end
' </body>\n',
'</html>\n']
html = start + puzzle_list + end
- file = "{}_round.html".format(filename_from_name(rnd))
+ file = hunt_file(hunt, "{}_round.html".format(filename_from_name(rnd)))
f = open(file, "w")
for line in html:
f.write(line)
f.close()
return None
f = open(file, "w")
for line in html:
f.write(line)
f.close()
return None
-def puzzle_overview(puzzle):
+def puzzle_overview(hunt, puzzle):
#overview page for individual puzzles. saves as (name of puzzle).html,
#with underscores rather than spaces
name = puzzle['name']
#overview page for individual puzzles. saves as (name of puzzle).html,
#with underscores rather than spaces
name = puzzle['name']
'\n',
'</body>\n',
'</html>\n']
'\n',
'</body>\n',
'</html>\n']
- file = "{}.html".format(filename_from_name(name))
+ file = hunt_file(hunt, "{}.html".format(filename_from_name(name)))
f = open(file, "w")
for line in html:
f.write(line)
return None
f = open(file, "w")
for line in html:
f.write(line)
return None
-def puzzle_lists(puzzles, filt):
+def puzzle_lists(hunt, puzzles, filt):
#filt is one of "All", "Solved", "Unsolved" and spits out the appropriate list of puzzles
#generates pages for all puzzles, solved puzzles, unsolved puzzles
#saves as all/solved/unsolved.html, has a sidebar to link to each other and to the big board
#filt is one of "All", "Solved", "Unsolved" and spits out the appropriate list of puzzles
#generates pages for all puzzles, solved puzzles, unsolved puzzles
#saves as all/solved/unsolved.html, has a sidebar to link to each other and to the big board
' </body>\n',
'</html>\n']
if filt == "All":
' </body>\n',
'</html>\n']
if filt == "All":
+ file1 = hunt_file(hunt, 'all.html')
f = open(file1, "w")
for line in start + unsolved_code + solved_code + end:
f.write(line)
f.close()
elif filt == "Solved":
f = open(file1, "w")
for line in start + unsolved_code + solved_code + end:
f.write(line)
f.close()
elif filt == "Solved":
+ file2 = hunt_file(hunt, 'solved.html')
f = open(file2, 'w')
for line in start + solved_code + end:
f.write(line)
f.close()
elif filt == "Unsolved":
f = open(file2, 'w')
for line in start + solved_code + end:
f.write(line)
f.close()
elif filt == "Unsolved":
- file3 = 'unsolved.html'
+ file3 = hunt_file(hunt, 'unsolved.html')
f = open(file3, 'w')
for line in start + unsolved_code + end:
f.write(line)
f = open(file3, 'w')
for line in start + unsolved_code + end:
f.write(line)
# Initialize AWS resources to talk to database
db = boto3.resource('dynamodb')
table = db.Table("turbot")
# Initialize AWS resources to talk to database
db = boto3.resource('dynamodb')
table = db.Table("turbot")
-puzzles, rounds = hunt_info(table, "mh2021")
+hunt, puzzles, rounds = hunt_info(table, "mh2021")
+
+# Create a directory for the hunt in the WEBROOT
+root = hunt_file(hunt, "")
+try:
+ os.mkdir(root)
+except FileExistsError:
+ # We're happy as a clam if the directory already exists
+ pass
-overview(puzzles, rounds)
+overview(hunt, puzzles, rounds)
- round_overview(rnd, puzzles)
+ round_overview(hunt, rnd, puzzles)
- puzzle_overview(puzzle)
-puzzle_lists(puzzles, "All")
-puzzle_lists(puzzles, "Solved")
-puzzle_lists(puzzles, "Unsolved")
+ puzzle_overview(hunt, puzzle)
+puzzle_lists(hunt, puzzles, "All")
+puzzle_lists(hunt, puzzles, "Solved")
+puzzle_lists(hunt, puzzles, "Unsolved")