From 8efb5b6772282e11e212edf67c8befe22ea8c06f Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Sat, 9 Jul 2022 15:18:29 +0200 Subject: Add plumbing to update next concert on the frontpage --- Makefile | 5 ++++- admin/build-indexes.sh | 15 +++++++++++++++ admin/update-index.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ build-concerts.py | 12 +----------- helpers.py | 11 +++++++++++ 5 files changed, 82 insertions(+), 12 deletions(-) create mode 100755 admin/build-indexes.sh create mode 100755 admin/update-index.py diff --git a/Makefile b/Makefile index ca01374..05616cd 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ scripts_folders = $(call dirnames,$(scripts)) .PHONY: all clean site # Maintenance: -.PHONY: feeds upload +.PHONY: feeds indexes upload #################### Recipes. @@ -49,6 +49,9 @@ all: site feeds: ./admin/feeds/build-feeds.sh $(feeds_src) +indexes: + ./admin/build-indexes.sh . $(languages) + upload: site ./upload.sh $(OUTDIR) diff --git a/admin/build-indexes.sh b/admin/build-indexes.sh new file mode 100755 index 0000000..9dcbda7 --- /dev/null +++ b/admin/build-indexes.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +set -eu + +HERE=$(dirname "$0") +ROOT=${HERE}/.. + +for directory +do + concert="${directory}"/concerts.in + index="${directory}"/index.html + + PYTHONPATH="${ROOT}" \ + "${HERE}"/update-index.py "${concert}" "${index}" +done diff --git a/admin/update-index.py b/admin/update-index.py new file mode 100755 index 0000000..5946433 --- /dev/null +++ b/admin/update-index.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +from datetime import datetime +from sys import argv + +from helpers import ( + guess_language, + read_concerts, + split_concerts, + tmplocale, +) + + +CALENDAR_LAYOUT = { + 'en': '%B
%d', + 'fr': '%d
%B', +} + +INDEX_TEMPLATE = '''\ +
+ +

+ {CALENDAR} +

+
+
+''' + + +def main(concerts_src, index_dst): + today = datetime.fromordinal( + datetime.today().date().toordinal() + ) + past_concerts, next_concerts = split_concerts( + read_concerts(concerts_src), today + ) + + concert = next_concerts[0] if next_concerts else past_concerts[-1] + + lang = guess_language(concerts_src) + template = INDEX_TEMPLATE.format(CALENDAR=CALENDAR_LAYOUT[lang]) + + with tmplocale(lang): + index = concert.time.strftime(template) + + with open(index_dst, 'w') as index_file: + index_file.write(index) + + +if __name__ == '__main__': + main(*argv[1:]) diff --git a/build-concerts.py b/build-concerts.py index 6e99673..ed37864 100755 --- a/build-concerts.py +++ b/build-concerts.py @@ -9,6 +9,7 @@ from helpers import ( guess_language, read_concerts, relative_path, + split_concerts, tmplocale, touchup_plaintext, ) @@ -19,17 +20,6 @@ from helpers import ( # - canceled => warning -def split_concerts(concerts, threshold): - cutoff = len(concerts) - - for i, c in enumerate(concerts): - if c.time > threshold: - cutoff = i - break - - return reversed(concerts[:cutoff]), concerts[cutoff:] - - LOCALIZED_TEXT = { 'en': { 'past': 'Past concerts', diff --git a/helpers.py b/helpers.py index e3ce938..e5c089f 100644 --- a/helpers.py +++ b/helpers.py @@ -135,6 +135,17 @@ def read_concerts(filename): return tuple(sorted(concerts, key=attrgetter('time'))) +def split_concerts(concerts, threshold): + cutoff = len(concerts) + + for i, c in enumerate(concerts): + if c.time > threshold: + cutoff = i + break + + return reversed(concerts[:cutoff]), concerts[cutoff:] + + _TOUCHUPS = ( (re.compile('([0-9])(st|nd|rd|th|er|ère|nde|ème)'), r'\1\2'), (re.compile('<(https?://[^ ]+)>'), r'\1'), -- cgit v1.2.3