2018-10-12 23:00:20 +02:00
|
|
|
import os
|
2020-09-21 20:14:10 +02:00
|
|
|
from shutil import copy
|
|
|
|
from pathlib import Path
|
2018-10-12 23:00:20 +02:00
|
|
|
import subprocess
|
|
|
|
|
2020-09-21 20:14:10 +02:00
|
|
|
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
|
|
|
|
2020-09-21 20:14:10 +02:00
|
|
|
from .songs import load_songs
|
|
|
|
from .karaoke_templates import update_karaoke_songs
|
2018-10-12 23:00:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Builder:
|
2019-09-12 20:02:58 +02:00
|
|
|
def __init__(self, root_folder, libreto):
|
2018-10-12 23:00:20 +02:00
|
|
|
self.root_folder = root_folder
|
2018-10-14 20:52:05 +02:00
|
|
|
self.libreto = libreto
|
2020-09-21 20:14:10 +02:00
|
|
|
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
|
2019-09-12 20:02:58 +02:00
|
|
|
self.current_path = self.root_folder
|
2018-10-12 23:00:20 +02:00
|
|
|
|
|
|
|
def url(self, path):
|
2019-09-12 20:02:58 +02:00
|
|
|
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)
|
2019-09-12 20:02:58 +02:00
|
|
|
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):
|
2020-09-21 20:14:10 +02:00
|
|
|
songs, pending_songs = load_songs(self.root_folder)
|
2018-10-12 23:00:20 +02:00
|
|
|
global_context = {
|
2018-10-14 20:52:05 +02:00
|
|
|
'songs': songs,
|
2019-09-12 20:02:58 +02:00
|
|
|
'root_folder': self.root_folder,
|
2018-10-12 23:00:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 20:52:05 +02:00
|
|
|
for song in songs:
|
2019-09-12 20:02:58 +02:00
|
|
|
self.current_path = song.path
|
2018-10-14 20:52:05 +02:00
|
|
|
song.render(self, global_context)
|
|
|
|
|
|
|
|
self.render('index.html', self.root_folder, global_context)
|
|
|
|
|
|
|
|
home = self.root_folder / 'home'
|
2019-09-12 20:02:58 +02:00
|
|
|
self.current_path = home
|
2018-10-14 20:52:05 +02:00
|
|
|
if not home.exists():
|
|
|
|
home.mkdir()
|
|
|
|
self.render('home.html', home, global_context)
|
2018-10-12 23:00:20 +02:00
|
|
|
|
2018-10-14 20:52:05 +02:00
|
|
|
playlist = self.root_folder / 'playlist'
|
2019-09-12 20:02:58 +02:00
|
|
|
self.current_path = playlist
|
2018-10-14 20:52:05 +02:00
|
|
|
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)
|
|
|
|
|
2020-09-21 20:14:10 +02:00
|
|
|
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():
|
2018-10-14 20:52:05 +02:00
|
|
|
static.mkdir()
|
2018-10-12 23:00:20 +02:00
|
|
|
|
|
|
|
subprocess.check_call([
|
|
|
|
'rsync',
|
|
|
|
'-ra',
|
|
|
|
str(self.static_dir),
|
|
|
|
str(self.root_folder.absolute()),
|
|
|
|
])
|
2018-10-14 20:52:05 +02:00
|
|
|
|
|
|
|
libreto = self.root_folder / 'static/libreto/libreto.pdf'
|
|
|
|
copy(str(self.libreto.absolute()), str(libreto.absolute()))
|