This repository has been archived on 2024-08-27. You can view files and clone it, but cannot push or open issues or pull requests.
negromate_origins/web/negromateweb/builder.py

99 lines
2.9 KiB
Python
Raw Normal View History

2018-10-12 23:00:20 +02:00
import os
from shutil import copy
from pathlib import Path
2018-10-12 23:00:20 +02:00
import subprocess
from jinja2 import Environment, PackageLoader, select_autoescape
2019-12-07 12:24:28 +01:00
from jinja2.utils import Markup
2018-10-12 23:00:20 +02:00
from .songs import load_songs
from .karaoke_templates import update_karaoke_songs
2018-10-12 23:00:20 +02:00
class Builder:
def __init__(self, root_folder, libreto):
2018-10-12 23:00:20 +02:00
self.root_folder = root_folder
self.libreto = libreto
self.src_dir = Path(__file__).parent
self.static_dir = self.src_dir / 'static'
2018-10-12 23:00:20 +02:00
self.env = Environment(
loader=PackageLoader('negromateweb', 'templates'),
autoescape=select_autoescape(['html']),
)
self.env.filters['url'] = self.url
2019-12-07 12:24:28 +01:00
self.env.filters['display_boolean'] = self.display_boolean
self.current_path = self.root_folder
2018-10-12 23:00:20 +02:00
def url(self, path):
return os.path.relpath(path, self.current_path)
2018-10-12 23:00:20 +02:00
2019-12-07 12:24:28 +01:00
def display_boolean(self, value):
if value:
return Markup('✓')
else:
return Markup('✗')
2018-10-12 23:00:20 +02:00
def render(self, template, target, context):
html_file = target / 'index.html'
page_template = self.env.get_template(template)
root_path = os.path.relpath(self.root_folder, target)
context['root_path'] = root_path
2018-10-12 23:00:20 +02:00
with html_file.open('w') as page:
page.write(page_template.render(context))
def build(self):
songs, pending_songs = load_songs(self.root_folder)
2018-10-12 23:00:20 +02:00
global_context = {
'songs': songs,
'root_folder': self.root_folder,
2018-10-12 23:00:20 +02:00
}
for song in songs:
self.current_path = song.path
song.render(self, global_context)
self.render('index.html', self.root_folder, global_context)
home = self.root_folder / 'home'
self.current_path = home
if not home.exists():
home.mkdir()
self.render('home.html', home, global_context)
2018-10-12 23:00:20 +02:00
playlist = self.root_folder / 'playlist'
self.current_path = playlist
if not playlist.exists():
playlist.mkdir()
self.render('playlist.html', playlist, global_context)
2018-10-12 23:00:20 +02:00
2019-12-07 12:24:28 +01:00
todo = self.root_folder / 'todo'
self.current_path = todo
if not todo.exists():
todo.mkdir()
todo_context = {
'pending_songs': pending_songs,
}
todo_context.update(global_context)
self.render('todo.html', todo, todo_context)
template_file = self.src_dir / 'templates' / 'karaoke.ass'
update_karaoke_songs(songs, template_file)
2018-10-12 23:00:20 +02:00
static = self.root_folder / 'static'
if not static.exists():
static.mkdir()
2018-10-12 23:00:20 +02:00
subprocess.check_call([
'rsync',
'-ra',
str(self.static_dir),
str(self.root_folder.absolute()),
])
libreto = self.root_folder / 'static/libreto/libreto.pdf'
copy(str(self.libreto.absolute()), str(libreto.absolute()))