paceboard/scripts/utils/gen/runs.py

80 lines
2.9 KiB
Python
Raw Normal View History

2021-06-23 21:26:35 +01:00
### Generation helper for run pages
from .. import file as util_file
from .. import csv as util_csv
2021-06-27 19:47:25 +01:00
import shutil, os, datetime
2021-06-23 21:26:35 +01:00
def generate(templatedir, destinationdir, templateFilename):
2021-06-25 21:13:36 +01:00
"""Main generation function for run page generation helper.
2021-06-25 21:13:36 +01:00
templatedir -- the relative path of the template html file's directory\n
destinationpath -- the directory where run paths should be generated\n
templateFilename -- the filename of the run template (always run.html)\n
"""
2021-06-28 03:40:15 +01:00
# Read runs, categories, and config csv files
2021-06-25 21:13:36 +01:00
runIdName = "tk_run_id"
categoryIdName = "tk_category_dashname"
runs = util_csv.dictReaderMultiRow("../csv/runs.csv", runIdName)
categories = util_csv.dictReaderMultiRow("../csv/categories.csv", categoryIdName)
config = util_csv.dictReaderFirstRow("../csv/config.csv")
2021-06-25 21:13:36 +01:00
for run in runs:
2021-06-28 03:40:15 +01:00
# Get proper directory
2021-06-26 04:05:19 +01:00
thisRun = runs[run]
path = f"{destinationdir}/{thisRun[runIdName]}"
currentDir = os.getcwd()
2021-06-28 03:40:15 +01:00
# Copy template to appropriate directory
os.makedirs(path, exist_ok=True)
shutil.copy(
f"{currentDir}/{templatedir}/{templateFilename}",
f"{currentDir}/{path}/index.html",
)
2021-06-28 03:40:15 +01:00
# Replace run tk placeholders with values, then save tk_run_link and tk_run_duration for use in lk handlers
2021-06-25 21:13:36 +01:00
tk_run_link = ""
2021-06-26 04:05:19 +01:00
for key in thisRun:
util_file.replaceTextInFile(f"{path}/index.html", key, thisRun[key])
2021-06-25 21:13:36 +01:00
if key == "tk_run_link":
2021-06-26 04:05:19 +01:00
tk_run_link = thisRun[key]
elif key == "tk_run_duration":
tk_run_duration = thisRun[key]
2021-06-25 21:13:36 +01:00
2021-06-28 03:40:15 +01:00
# Replace category tk placeholders with values
2021-06-26 04:05:19 +01:00
for key in categories[thisRun["tk_run_category_dashname"]]:
util_file.replaceTextInFile(
2021-06-25 21:13:36 +01:00
f"{path}/index.html",
key,
2021-06-26 04:05:19 +01:00
categories[thisRun["tk_run_category_dashname"]][key],
)
2021-06-28 03:40:15 +01:00
# Replace config tk placeholders with values
for key in config.keys():
util_file.replaceTextInFile(f"{path}/index.html", key, config[key])
2021-06-25 21:13:36 +01:00
2021-06-28 03:40:15 +01:00
# lk_run_link handler
2021-06-25 21:13:36 +01:00
if tk_run_link == "":
util_file.replaceTextInFile(
f"{path}/index.html", "lk_run_link", "No recording available"
)
else:
util_file.replaceTextInFile(
f"{path}/index.html",
"lk_run_link",
f'<a class="runLink" href="{tk_run_link}">Run recording</a>',
)
2021-06-26 04:05:19 +01:00
2021-06-28 03:40:15 +01:00
# lk_run_duration handler
2021-06-26 04:05:19 +01:00
runDurationSplit = [float(value) for value in tk_run_duration.split(":")]
lk_run_duration = datetime.timedelta(
hours=runDurationSplit[0],
minutes=runDurationSplit[1],
seconds=runDurationSplit[2],
)
util_file.replaceTextInFile(
f"{path}/index.html", "lk_run_duration", str(lk_run_duration)
)