import os from shutil import copy from pathlib import Path import subprocess from jinja2 import Environment, PackageLoader, select_autoescape from jinja2.utils import Markup from .songs import load_songs from .karaoke_templates import update_karaoke_songs class Builder: def __init__(self, root_folder, libreto): self.root_folder = root_folder self.libreto = libreto self.src_dir = Path(__file__).parent self.static_dir = self.src_dir / 'static' self.env = Environment( loader=PackageLoader('negromateweb', 'templates'), autoescape=select_autoescape(['html']), ) self.env.filters['url'] = self.url self.env.filters['display_boolean'] = self.display_boolean self.current_path = self.root_folder def url(self, path): return os.path.relpath(path, self.current_path) def display_boolean(self, value): if value: return Markup('✓') else: return Markup('✗') 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 with html_file.open('w') as page: page.write(page_template.render(context)) def build(self): songs, pending_songs = load_songs(self.root_folder) global_context = { 'songs': songs, 'root_folder': self.root_folder, } 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) playlist = self.root_folder / 'playlist' self.current_path = playlist if not playlist.exists(): playlist.mkdir() self.render('playlist.html', playlist, global_context) 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) static = self.root_folder / 'static' if not static.exists(): static.mkdir() 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()))