]> git.cworth.org Git - turbot-web/blobdiff - html_generator.py
Widened the expanded view boxes on the main board. Changed the answers display to...
[turbot-web] / html_generator.py
index 43d33702f81105f996ad6161174b0a1e47b216f0..01cecdea7e521c48fb844db5c52e40865d27f58b 100644 (file)
@@ -18,6 +18,7 @@ import boto3
 from boto3.dynamodb.conditions import Key
 import os
 import re
+import sys
 
 WEBROOT = "/srv/halibut.cworth.org/www"
 
@@ -197,7 +198,7 @@ def overview(hunt, puzzles, rounds):
             if puzzle['status'] == 'solved':
                 expanding += ['      <tr class=\'solved\';>\n',
                 '        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + ".html", puzzle['name']+meta)),
-                '        <td>{}</td>\n'.format(puzzle['solution']),
+                '        <td>{}</td>\n'.format(", ".join(puzzle['solution']).upper()),
                 '      </tr>\n']
             else:
                 expanding += ['      <tr class=\'unsolved\';>\n',
@@ -211,10 +212,11 @@ def overview(hunt, puzzles, rounds):
     end = ['</body>\n', '</html>\n']
     html = start + expanding + columns + end
     file = hunt_file(hunt, "index.html")
-    f = open(file, "w")
+    f = open(file + ".tmp", "w")
     for line in html:
         f.write(line)
     f.close()
+    os.rename(file + ".tmp", file)
     return None
 
 def round_overview(hunt, rnd, puzzles):
@@ -264,9 +266,9 @@ def round_overview(hunt, rnd, puzzles):
              '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
              '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
              '                        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
-             '                        <td>{}</td>\n'.format(puzzle['solution']),
+             '                        <td>{}</td>\n'.format(", ".join(puzzle['solution']).upper()),
             # '                        <td></td>\n',
-             '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
+             '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
              '                    </tr>\n']
         else:
             puzzle_list += [ '                    <tr>\n',
@@ -276,7 +278,7 @@ def round_overview(hunt, rnd, puzzles):
              '                        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
              '                        <td></td>\n',
             # '                        <td></td>\n',
-             '                        <td>{}</td>\n'.format(" ".join(puzzle.get('tags',[]))),
+             '                        <td>{}</td>\n'.format(", ".join(puzzle.get('tags',[]))),
              '                    </tr>\n']
     end = ['                </tbody>\n',
     '            </table>\n',
@@ -285,10 +287,11 @@ def round_overview(hunt, rnd, puzzles):
     '</html>\n']
     html = start + puzzle_list + end
     file = hunt_file(hunt, "{}_round.html".format(filename_from_name(rnd)))
-    f = open(file, "w")
+    f = open(file + ".tmp", "w")
     for line in html:
         f.write(line)
     f.close()
+    os.rename(file + ".tmp", file)
     return None
 
 def puzzle_overview(hunt, puzzle):
@@ -305,7 +308,7 @@ def puzzle_overview(hunt, puzzle):
     else:
         round_url = ''
     if puzzle['status'] == 'solved':
-        solution = puzzle['solution']
+        solution = ", ".join(puzzle['solution']).upper()
         status = 'solved'
     else:
         solution = ""
@@ -334,7 +337,7 @@ def puzzle_overview(hunt, puzzle):
      '        <table class="center">\n',
      '            <tr>\n',
      '                <td>Round(s): {}</td>\n'.format(" ".join(round_url)), #round page on our site
-     '                <td>Tags: {}</td>\n'.format(" ".join(puzzle.get('tags',[]))), #add tags
+     '                <td>Tags: {}</td>\n'.format(", ".join(puzzle.get('tags',[]))), #add tags
      '            </tr>\n',
      '            <tr>\n',
      '                <td>Answer: {}</td>\n'.format(solution),
@@ -346,9 +349,11 @@ def puzzle_overview(hunt, puzzle):
      '</body>\n',
      '</html>\n']
     file = hunt_file(hunt, "{}.html".format(filename_from_name(name)))
-    f = open(file, "w")
+    f = open(file + ".tmp", "w")
     for line in html:
         f.write(line)
+    f.close()
+    os.rename(file + ".tmp", file)
     return None
 
 def puzzle_lists(hunt, puzzles, filt):
@@ -395,7 +400,7 @@ def puzzle_lists(hunt, puzzles, filt):
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
         if 'rounds' in puzzle:
-            round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
+            round_url = link(website + filename_from_name(puzzle['rounds'][0]) + "_round.html", puzzle['rounds'][0])
         else:
             round_url = ''
         #assuming one round per puzzle for now
@@ -405,9 +410,9 @@ def puzzle_lists(hunt, puzzles, filt):
          '                        <td>{}</td>\n'.format(elink(puzzle.get('url',''), 'Puzzle')),
          '                        <td>{}</td>\n'.format(elink(puzzle['sheet_url'], 'Sheet')),
          '                        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
-         '                        <td>{}</td>\n'.format(puzzle['solution']),
+         '                        <td>{}</td>\n'.format(", ".join(puzzle['solution']).upper()),
          '                        <td>{}</td>\n'.format(round_url),
-         '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
+         '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
          '                    </tr>\n']
     for puzzle in unsolved_puzzles:
         if puzzle['type'] == 'meta':
@@ -416,7 +421,7 @@ def puzzle_lists(hunt, puzzles, filt):
             meta = ''
         slack_url = channel_url(puzzle['channel_id'])
         if 'rounds' in puzzle:
-            round_url = link(website + filename_from_name(rnd) + "_round.html", puzzle['rounds'][0])
+            round_url = link(website + filename_from_name(puzzle['rounds'][0]) + "_round.html", puzzle['rounds'][0])
         else:
             round_url = ''
         #assuming one round per puzzle for now
@@ -428,7 +433,7 @@ def puzzle_lists(hunt, puzzles, filt):
          '                        <td>{}</td>\n'.format(link(website + filename_from_name(puzzle['name']) + '.html', 'Overview')),
          '                        <td></td>\n',
          '                        <td>{}</td>\n'.format(round_url),
-         '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
+         '                        <td>{}</td>\n'.format("".join(puzzle.get('tags',[]))),
          '                    </tr>\n']
     end = ['                </tbody>\n',
     '            </table>\n',
@@ -437,42 +442,61 @@ def puzzle_lists(hunt, puzzles, filt):
     '</html>\n']
     if filt == "All":
         file1 = hunt_file(hunt, 'all.html')
-        f = open(file1, "w")
+        f = open(file1 + ".tmp", "w")
         for line in start + unsolved_code + solved_code + end:
             f.write(line)
         f.close()
+        os.rename(file1 + ".tmp", file1)
     elif filt == "Solved":
         file2 = hunt_file(hunt, 'solved.html')
-        f = open(file2, 'w')
+        f = open(file2 + ".tmp", 'w')
         for line in start + solved_code + end:
             f.write(line)
         f.close()
+        os.rename(file2 + ".tmp", file2)
     elif filt == "Unsolved":
         file3 = hunt_file(hunt, 'unsolved.html')
-        f = open(file3, 'w')
+        f = open(file3 + ".tmp", 'w')
         for line in start + unsolved_code + end:
             f.write(line)
         f.close()
+        os.rename(file3 + ".tmp", file3)
     return None
 
-# Initialize AWS resources to talk to database
+def generate_for_hunt_id(table, hunt_id):
+    hunt, puzzles, rounds = hunt_info(table, hunt_id)
+
+    # 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(hunt, puzzles, rounds)
+    for rnd in rounds:
+        round_overview(hunt, rnd, puzzles)
+        for puzzle in puzzles:
+            puzzle_overview(hunt, puzzle)
+    puzzle_lists(hunt, puzzles, "All")
+    puzzle_lists(hunt, puzzles, "Solved")
+    puzzle_lists(hunt, puzzles, "Unsolved")
+            
+
+# Initialize AWS resources to talk to the database
 db = boto3.resource('dynamodb')
 table = db.Table("turbot")
-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(hunt, puzzles, rounds)
-for rnd in rounds:
-    round_overview(hunt, rnd, puzzles)
-for puzzle in puzzles:
-    puzzle_overview(hunt, puzzle)
-puzzle_lists(hunt, puzzles, "All")
-puzzle_lists(hunt, puzzles, "Solved")
-puzzle_lists(hunt, puzzles, "Unsolved")
+
+def usage():
+    print("Usage: {} hunt_id [...]")
+    print("")
+    print("Generates pages (under {}) ".format(WEBROOT))
+    print("for the specified hunt_id(s).")
+
+if len(sys.argv) < 2:
+    usage()
+    sys.exit(1)
+
+for hunt_id in sys.argv[1:]:
+    generate_for_hunt_id(table, hunt_id)