paceboard/scripts/utils/gen/categories.py

112 lines
3.8 KiB
Python
Raw Normal View History

2021-06-23 21:26:35 +01:00
### Generation helper for category pages
from .. import file as util_file
from .. import csv as util_csv
import shutil
import os
2021-06-26 04:05:19 +01:00
import datetime
2021-06-23 21:26:35 +01:00
def generate(templatedir, destinationdir, templateFilename):
"""Main generation function for category 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 category paths should be generated\n
templateFilename -- the filename of the category template (always category.html)\n
"""
2021-06-26 04:05:19 +01:00
runIdName = "tk_run_id"
2021-06-25 21:13:36 +01:00
categoryIdName = "tk_category_dashname"
2021-06-26 04:05:19 +01:00
runs = util_csv.dictReaderMultiRow("../csv/runs.csv", runIdName)
2021-06-25 21:13:36 +01:00
categories = util_csv.dictReaderMultiRow("../csv/categories.csv", categoryIdName)
config = util_csv.dictReaderFirstRow("../csv/config.csv")
for category in categories:
2021-06-26 04:05:19 +01:00
thisCategory = categories[category]
path = f"{destinationdir}/{thisCategory[categoryIdName]}"
currentDir = os.getcwd()
os.makedirs(path, exist_ok=True)
shutil.copy(
f"{currentDir}/{templatedir}/{templateFilename}",
f"{currentDir}/{path}/index.html",
)
2021-06-26 04:05:19 +01:00
for key in thisCategory:
util_file.replaceTextInFile(f"{path}/index.html", key, thisCategory[key])
for key in config.keys():
util_file.replaceTextInFile(f"{path}/index.html", key, config[key])
2021-06-26 04:05:19 +01:00
## lk_leaderboard handler ##
runsInCategory = {}
for run in runs:
thisRun = runs[run]
if (
thisRun["tk_run_category_dashname"]
== thisCategory["tk_category_dashname"]
):
runsInCategory[thisRun["tk_run_id"]] = thisRun
runDurationsInCategory = {}
for run in runsInCategory:
thisRun = runsInCategory[run]
runDurationSplit = [
float(value) for value in thisRun["tk_run_duration"].split(":")
]
runDurationsInCategory[thisRun["tk_run_id"]] = datetime.timedelta(
hours=runDurationSplit[0],
minutes=runDurationSplit[1],
seconds=runDurationSplit[2],
)
sortedRunsInCategory = {
runId: runDuration
for runId, runDuration in sorted(
runDurationsInCategory.items(), key=lambda item: item[1]
)
}
trimmedRunsInCategory = {}
runnersRepresentedInCategory = []
for run in sortedRunsInCategory:
thisRun = runs[run]
runner = thisRun["tk_run_runner"]
if runner not in runnersRepresentedInCategory:
trimmedRunsInCategory[thisRun["tk_run_id"]] = thisRun
runnersRepresentedInCategory.append(runner)
place = 1
lk_leaderboard = '<table class="categoryBoard centerHoriz">'
for run in trimmedRunsInCategory:
thisRun = trimmedRunsInCategory[run]
thisRunner = thisRun["tk_run_runner"]
thisRunId = thisRun["tk_run_id"]
thisLink = f"../../runs/{thisRunId}"
thisDuration = str(runDurationsInCategory[thisRunId])
thisDate = thisRun["tk_run_date"]
lk_leaderboard += f'<tr><th>{place}.</th><th>{thisRunner}</th><th><a href="{thisLink}">{thisDuration}</a></th><th>{thisDate}</th></tr>'
2021-06-27 05:30:26 +01:00
# Also handle replacing lk_run_place on run pages
util_file.replaceTextInFile(
f"{destinationdir}/../runs/{thisRunId}/index.html",
"lk_run_place",
str(place),
)
2021-06-26 04:05:19 +01:00
place += 1
2021-06-27 05:30:26 +01:00
2021-06-26 04:05:19 +01:00
lk_leaderboard += "</table>"
util_file.replaceTextInFile(
f"{path}/index.html", "lk_leaderboard", lk_leaderboard
)
## End of: lk_leaderboard handler ##