From f2d4648c75d7070dc9663d1acfe1a787805d7883 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 12 Jan 2022 01:06:27 -0800 Subject: [PATCH] Generate all files in a hunt-specific directory under a target WEBROOT 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). --- html_generator.py | 54 +++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/html_generator.py b/html_generator.py index 0950d6f..43d3370 100644 --- a/html_generator.py +++ b/html_generator.py @@ -16,11 +16,21 @@ Requires sorttable.js, which should be included """ import boto3 from boto3.dynamodb.conditions import Key +import os import re +WEBROOT = "/srv/halibut.cworth.org/www" + 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)""" @@ -91,7 +101,7 @@ def hunt_info(table, hunt_id): rounds = list(rounds) rounds.sort() - return puzzles, rounds + return hunt, puzzles, rounds def round_stat(rnd, puzzles): #Counts puzzles, solved, list of puzzles for a given round @@ -123,7 +133,7 @@ def round_stat(rnd, puzzles): -def overview(puzzles, rounds): +def overview(hunt, puzzles, rounds): #big board, main page. saves as index.html start = ['\n', '\n', @@ -200,14 +210,14 @@ def overview(puzzles, rounds): columns.append(' \n') end = ['\n', '\n'] html = start + expanding + columns + end - file = "index.html" + file = hunt_file(hunt, "index.html") 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. @@ -274,14 +284,14 @@ def round_overview(rnd, puzzles): ' \n', '\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 -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'] @@ -335,13 +345,13 @@ def puzzle_overview(puzzle): '\n', '\n', '\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 -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 @@ -426,19 +436,19 @@ def puzzle_lists(puzzles, filt): ' \n', '\n'] if filt == "All": - file1 = 'all.html' + 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": - file2 = 'solved.html' + 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": - file3 = 'unsolved.html' + file3 = hunt_file(hunt, 'unsolved.html') f = open(file3, 'w') for line in start + unsolved_code + end: f.write(line) @@ -448,13 +458,21 @@ def puzzle_lists(puzzles, filt): # 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) for rnd in rounds: - round_overview(rnd, puzzles) + round_overview(hunt, rnd, puzzles) for puzzle in 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") -- 2.43.0