paceboard/scripts/utils/gen/categories.py

119 lines
4.4 KiB
Python
Raw Permalink 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
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):
"""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-28 03:40:15 +01:00
# Read runs, categories, and config csv files
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-28 03:40:15 +01:00
# Get proper directory
2021-06-26 04:05:19 +01:00
thisCategory = categories[category]
path = f"{destinationdir}/{thisCategory[categoryIdName]}"
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 category tk placeholders with values
2021-06-26 04:05:19 +01:00
for key in thisCategory:
util_file.replaceTextInFile(f"{path}/index.html", key, thisCategory[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-26 04:05:19 +01:00
## lk_leaderboard handler ##
2021-06-28 03:40:15 +01:00
# Find runsInCategory
2021-06-26 04:05:19 +01:00
runsInCategory = {}
for run in runs:
thisRun = runs[run]
if (
thisRun["tk_run_category_dashname"]
== thisCategory["tk_category_dashname"]
):
runsInCategory[thisRun["tk_run_id"]] = thisRun
2021-06-28 03:40:15 +01:00
# Find runDurationsInCategory by parsing runsInCategory values
2021-06-26 04:05:19 +01:00
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],
)
2021-06-28 03:40:15 +01:00
# Find sortedRunsInCategory by sorting durations from runDurationsInCategory
2021-06-26 04:05:19 +01:00
sortedRunsInCategory = {
runId: runDuration
for runId, runDuration in sorted(
runDurationsInCategory.items(), key=lambda item: item[1]
)
}
2021-06-28 03:40:15 +01:00
# Find trimmedRunsInCategory by only including one run per runner from sortedRunsInCategory
2021-06-28 23:40:04 +01:00
trimmedRunsInCategory = []
2021-06-26 04:05:19 +01:00
runnersRepresentedInCategory = []
for run in sortedRunsInCategory:
thisRun = runs[run]
runner = thisRun["tk_run_runner"]
if runner not in runnersRepresentedInCategory:
2021-06-28 23:40:04 +01:00
trimmedRunsInCategory.append(thisRun)
2021-06-26 04:05:19 +01:00
runnersRepresentedInCategory.append(runner)
2021-06-28 03:40:15 +01:00
# Replace lk_leaderboard with category leaderboard table
2021-06-26 04:05:19 +01:00
place = 1
lk_leaderboard = '<table class="categoryBoard centerHoriz">'
for run in trimmedRunsInCategory:
2021-06-28 03:40:15 +01:00
# Define values for table
2021-06-28 23:40:04 +01:00
runner = run["tk_run_runner"]
runId = run["tk_run_id"]
runLink = f"../../runs/{runId}"
runDuration = str(runDurationsInCategory[runId])
runDate = run["tk_run_date"]
2021-06-26 04:05:19 +01:00
2021-06-28 03:40:15 +01:00
# Concatenate a row to the table
2021-06-29 01:30:14 +01:00
lk_leaderboard += f'<tr><td>{place}.</td><td>{runner}</td><td><a href="{runLink}">{runDuration}</a></td><td>{runDate}</td></tr>'
2021-06-27 05:30:26 +01:00
# Also handle replacing lk_run_place on run pages
util_file.replaceTextInFile(
2021-06-28 23:40:04 +01:00
f"{destinationdir}/../runs/{runId}/index.html",
2021-06-27 05:30:26 +01:00
"lk_run_place",
str(place),
)
2021-06-26 04:05:19 +01:00
place += 1
lk_leaderboard += "</table>"
2021-06-28 03:40:15 +01:00
# Replace lk_leaderboard placeholder with table string
2021-06-26 04:05:19 +01:00
util_file.replaceTextInFile(
f"{path}/index.html", "lk_leaderboard", lk_leaderboard
)
## End of: lk_leaderboard handler ##