update-index.py (1088B)
1 #!/usr/bin/env python3 2 3 from datetime import datetime 4 from sys import argv 5 6 from helpers import ( 7 guess_language, 8 read_concerts, 9 split_concerts, 10 tmplocale, 11 ) 12 13 14 CALENDAR_LAYOUT = { 15 'en': '<span id="month">%B</span><br><span id="day">%d</span>', 16 'fr': '<span id="day">%d</span><br><span id="month">%B</span>', 17 } 18 19 INDEX_TEMPLATE = '''\ 20 <main> 21 <a id="next-concert" href="concerts.html#concert-%F"> 22 <p> 23 {CALENDAR} 24 </p> 25 </a> 26 </main> 27 ''' 28 29 30 def main(concerts_src, index_dst): 31 today = datetime.fromordinal( 32 datetime.today().date().toordinal() 33 ) 34 past_concerts, next_concerts = split_concerts( 35 read_concerts(concerts_src), today 36 ) 37 38 concert = next_concerts[0] if next_concerts else past_concerts[-1] 39 40 lang = guess_language(concerts_src) 41 template = INDEX_TEMPLATE.format(CALENDAR=CALENDAR_LAYOUT[lang]) 42 43 with tmplocale(lang): 44 index = concert.time.strftime(template) 45 46 with open(index_dst, 'w') as index_file: 47 index_file.write(index) 48 49 50 if __name__ == '__main__': 51 main(*argv[1:])