|
@ -10,26 +10,25 @@ import logging |
|
|
import os.path |
|
|
import os.path |
|
|
import re |
|
|
import re |
|
|
import subprocess |
|
|
import subprocess |
|
|
import sys |
|
|
|
|
|
|
|
|
|
|
|
from songbook import __SHAREDIR__ |
|
|
from songbook_core import __SHAREDIR__ |
|
|
from songbook import errors |
|
|
from songbook_core import errors |
|
|
from songbook.files import recursiveFind |
|
|
from songbook_core.files import recursive_find |
|
|
from songbook.index import processSXD |
|
|
from songbook_core.index import process_sxd |
|
|
from songbook.songs import Song, SongsList |
|
|
from songbook_core.songs import Song, SongsList |
|
|
|
|
|
|
|
|
EOL = "\n" |
|
|
EOL = "\n" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parseTemplate(template): |
|
|
def parse_template(template): |
|
|
"""Return the list of parameters defined in the template.""" |
|
|
"""Return the list of parameters defined in the template.""" |
|
|
embeddedJsonPattern = re.compile(r"^%%:") |
|
|
embedded_json_pattern = re.compile(r"^%%:") |
|
|
with open(template) as template_file: |
|
|
with open(template) as template_file: |
|
|
code = [ |
|
|
code = [ |
|
|
line[3:-1] |
|
|
line[3:-1] |
|
|
for line |
|
|
for line |
|
|
in template_file |
|
|
in template_file |
|
|
if embeddedJsonPattern.match(line) |
|
|
if embedded_json_pattern.match(line) |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
data = json.loads(''.join(code)) |
|
|
data = json.loads(''.join(code)) |
|
@ -39,15 +38,16 @@ def parseTemplate(template): |
|
|
return parameters |
|
|
return parameters |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def toValue(parameter, data): |
|
|
# pylint: disable=too-many-return-statements |
|
|
|
|
|
def to_value(parameter, data): |
|
|
if "type" not in parameter: |
|
|
if "type" not in parameter: |
|
|
return data |
|
|
return data |
|
|
elif parameter["type"] == "stringlist": |
|
|
elif parameter["type"] == "stringlist": |
|
|
if "join" in parameter: |
|
|
if "join" in parameter: |
|
|
joinText = parameter["join"] |
|
|
join_text = parameter["join"] |
|
|
else: |
|
|
else: |
|
|
joinText = '' |
|
|
join_text = '' |
|
|
return joinText.join(data) |
|
|
return join_text.join(data) |
|
|
elif parameter["type"] == "color": |
|
|
elif parameter["type"] == "color": |
|
|
return data[1:] |
|
|
return data[1:] |
|
|
elif parameter["type"] == "font": |
|
|
elif parameter["type"] == "font": |
|
@ -58,24 +58,24 @@ def toValue(parameter, data): |
|
|
return data |
|
|
return data |
|
|
elif parameter["type"] == "flag": |
|
|
elif parameter["type"] == "flag": |
|
|
if "join" in parameter: |
|
|
if "join" in parameter: |
|
|
joinText = parameter["join"] |
|
|
join_text = parameter["join"] |
|
|
else: |
|
|
else: |
|
|
joinText = '' |
|
|
join_text = '' |
|
|
return joinText.join(data) |
|
|
return join_text.join(data) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def formatDeclaration(name, parameter): |
|
|
def format_declaration(name, parameter): |
|
|
value = "" |
|
|
value = "" |
|
|
if "default" in parameter: |
|
|
if "default" in parameter: |
|
|
value = parameter["default"] |
|
|
value = parameter["default"] |
|
|
return ( |
|
|
return ( |
|
|
r'\def\set@{name}#1{{\def\get{name}{{#1}}}}'.format(name=name) |
|
|
r'\def\set@{name}#1{{\def\get{name}{{#1}}}}'.format(name=name) |
|
|
+ EOL |
|
|
+ EOL |
|
|
+ formatDefinition(name, toValue(parameter, value)) |
|
|
+ format_definition(name, to_value(parameter, value)) |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def formatDefinition(name, value): |
|
|
def format_definition(name, value): |
|
|
return r'\set@{name}{{{value}}}'.format(name=name, value=value) + EOL |
|
|
return r'\set@{name}{{{value}}}'.format(name=name, value=value) + EOL |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -103,9 +103,9 @@ def clean(basename): |
|
|
raise errors.CleaningError(basename + ext, exception) |
|
|
raise errors.CleaningError(basename + ext, exception) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def makeTexFile(sb, output): |
|
|
def make_tex_file(songbook, output): |
|
|
"""Create the LaTeX file corresponding to the .sb file given in argument.""" |
|
|
"""Create the LaTeX file corresponding to the .sb file given in argument.""" |
|
|
datadir = sb['datadir'] |
|
|
datadir = songbook['datadir'] |
|
|
name = output[:-4] |
|
|
name = output[:-4] |
|
|
template_dir = os.path.join(datadir, 'templates') |
|
|
template_dir = os.path.join(datadir, 'templates') |
|
|
songs = [] |
|
|
songs = [] |
|
@ -117,33 +117,33 @@ def makeTexFile(sb, output): |
|
|
authwords = {"after": ["by"], "ignore": ["unknown"], "sep": ["and"]} |
|
|
authwords = {"after": ["by"], "ignore": ["unknown"], "sep": ["and"]} |
|
|
|
|
|
|
|
|
# parse the songbook data |
|
|
# parse the songbook data |
|
|
if "template" in sb: |
|
|
if "template" in songbook: |
|
|
template = sb["template"] |
|
|
template = songbook["template"] |
|
|
del sb["template"] |
|
|
del songbook["template"] |
|
|
else: |
|
|
else: |
|
|
template = os.path.join(__SHAREDIR__, "templates", "default.tmpl") |
|
|
template = os.path.join(__SHAREDIR__, "templates", "default.tmpl") |
|
|
if "songs" in sb: |
|
|
if "songs" in songbook: |
|
|
songs = sb["songs"] |
|
|
songs = songbook["songs"] |
|
|
del sb["songs"] |
|
|
del songbook["songs"] |
|
|
if "titleprefixwords" in sb: |
|
|
if "titleprefixwords" in songbook: |
|
|
prefixes = sb["titleprefixwords"] |
|
|
prefixes = songbook["titleprefixwords"] |
|
|
for prefix in sb["titleprefixwords"]: |
|
|
for prefix in songbook["titleprefixwords"]: |
|
|
prefixes_tex += r"\titleprefixword{%s}" % prefix + EOL |
|
|
prefixes_tex += r"\titleprefixword{%s}" % prefix + EOL |
|
|
sb["titleprefixwords"] = prefixes_tex |
|
|
songbook["titleprefixwords"] = prefixes_tex |
|
|
if "authwords" in sb: |
|
|
if "authwords" in songbook: |
|
|
# Populating default value |
|
|
# Populating default value |
|
|
for key in ["after", "sep", "ignore"]: |
|
|
for key in ["after", "sep", "ignore"]: |
|
|
if key not in sb["authwords"]: |
|
|
if key not in songbook["authwords"]: |
|
|
sb["authwords"][key] = authwords[key] |
|
|
songbook["authwords"][key] = authwords[key] |
|
|
# Processing authwords values |
|
|
# Processing authwords values |
|
|
authwords = sb["authwords"] |
|
|
authwords = songbook["authwords"] |
|
|
for key in ["after", "sep", "ignore"]: |
|
|
for key in ["after", "sep", "ignore"]: |
|
|
for word in authwords[key]: |
|
|
for word in authwords[key]: |
|
|
if key == "after": |
|
|
if key == "after": |
|
|
authwords_tex += r"\auth%sword{%s}" % ("by", word) + EOL |
|
|
authwords_tex += r"\auth%sword{%s}" % ("by", word) + EOL |
|
|
else: |
|
|
else: |
|
|
authwords_tex += r"\auth%sword{%s}" % (key, word) + EOL |
|
|
authwords_tex += r"\auth%sword{%s}" % (key, word) + EOL |
|
|
sb["authwords"] = authwords_tex |
|
|
songbook["authwords"] = authwords_tex |
|
|
if "after" in authwords: |
|
|
if "after" in authwords: |
|
|
authwords["after"] = [re.compile(r"^.*%s\b(.*)" % after) |
|
|
authwords["after"] = [re.compile(r"^.*%s\b(.*)" % after) |
|
|
for after in authwords["after"]] |
|
|
for after in authwords["after"]] |
|
@ -152,52 +152,55 @@ def makeTexFile(sb, output): |
|
|
authwords["sep"] = [re.compile(r"^(.*)%s (.*)$" % sep) |
|
|
authwords["sep"] = [re.compile(r"^(.*)%s (.*)$" % sep) |
|
|
for sep in authwords["sep"]] |
|
|
for sep in authwords["sep"]] |
|
|
|
|
|
|
|
|
if "lang" not in sb: |
|
|
if "lang" not in songbook: |
|
|
sb["lang"] = "french" |
|
|
songbook["lang"] = "french" |
|
|
if "sort" in sb: |
|
|
if "sort" in songbook: |
|
|
sort = sb["sort"] |
|
|
sort = songbook["sort"] |
|
|
del sb["sort"] |
|
|
del songbook["sort"] |
|
|
else: |
|
|
else: |
|
|
sort = [u"by", u"album", u"@title"] |
|
|
sort = [u"by", u"album", u"@title"] |
|
|
Song.sort = sort |
|
|
Song.sort = sort |
|
|
Song.prefixes = prefixes |
|
|
Song.prefixes = prefixes |
|
|
Song.authwords = authwords |
|
|
Song.authwords = authwords |
|
|
|
|
|
|
|
|
parameters = parseTemplate(os.path.join(template_dir, template)) |
|
|
parameters = parse_template(os.path.join(template_dir, template)) |
|
|
|
|
|
|
|
|
# compute songslist |
|
|
# compute songslist |
|
|
if songs == "all": |
|
|
if songs == "all": |
|
|
songs = [ |
|
|
songs = [ |
|
|
os.path.relpath(filename, os.path.join(datadir, 'songs')) |
|
|
os.path.relpath(filename, os.path.join(datadir, 'songs')) |
|
|
for filename |
|
|
for filename |
|
|
in recursiveFind(os.path.join(datadir, 'songs'), '*.sg') |
|
|
in recursive_find(os.path.join(datadir, 'songs'), '*.sg') |
|
|
] |
|
|
] |
|
|
songslist = SongsList(datadir, sb["lang"]) |
|
|
songslist = SongsList(datadir, songbook["lang"]) |
|
|
songslist.append_list(songs) |
|
|
songslist.append_list(songs) |
|
|
|
|
|
|
|
|
sb["languages"] = ",".join(songslist.languages()) |
|
|
songbook["languages"] = ",".join(songslist.languages()) |
|
|
|
|
|
|
|
|
# output relevant fields |
|
|
# output relevant fields |
|
|
out = codecs.open(output, 'w', 'utf-8') |
|
|
out = codecs.open(output, 'w', 'utf-8') |
|
|
out.write('%% This file has been automatically generated, do not edit!\n') |
|
|
out.write('%% This file has been automatically generated, do not edit!\n') |
|
|
out.write(r'\makeatletter' + EOL) |
|
|
out.write(r'\makeatletter' + EOL) |
|
|
# output automatic parameters |
|
|
# output automatic parameters |
|
|
out.write(formatDeclaration("name", {"default": name})) |
|
|
out.write(format_declaration("name", {"default": name})) |
|
|
out.write(formatDeclaration("songslist", {"type": "stringlist"})) |
|
|
out.write(format_declaration("songslist", {"type": "stringlist"})) |
|
|
# output template parameter command |
|
|
# output template parameter command |
|
|
for name, parameter in parameters.iteritems(): |
|
|
for name, parameter in parameters.iteritems(): |
|
|
out.write(formatDeclaration(name, parameter)) |
|
|
out.write(format_declaration(name, parameter)) |
|
|
# output template parameter values |
|
|
# output template parameter values |
|
|
for name, value in sb.iteritems(): |
|
|
for name, value in songbook.iteritems(): |
|
|
if name in parameters: |
|
|
if name in parameters: |
|
|
out.write(formatDefinition(name, toValue(parameters[name], value))) |
|
|
out.write(format_definition( |
|
|
|
|
|
name, |
|
|
|
|
|
to_value(parameters[name], value), |
|
|
|
|
|
)) |
|
|
|
|
|
|
|
|
if len(songs) > 0: |
|
|
if len(songs) > 0: |
|
|
out.write(formatDefinition('songslist', songslist.latex())) |
|
|
out.write(format_definition('songslist', songslist.latex())) |
|
|
out.write(r'\makeatother' + EOL) |
|
|
out.write(r'\makeatother' + EOL) |
|
|
|
|
|
|
|
|
# output template |
|
|
# output template |
|
|
commentPattern = re.compile(r"^\s*%") |
|
|
comment_pattern = re.compile(r"^\s*%") |
|
|
with codecs.open( |
|
|
with codecs.open( |
|
|
os.path.join(template_dir, template), 'r', 'utf-8' |
|
|
os.path.join(template_dir, template), 'r', 'utf-8' |
|
|
) as template_file: |
|
|
) as template_file: |
|
@ -205,7 +208,7 @@ def makeTexFile(sb, output): |
|
|
line |
|
|
line |
|
|
for line |
|
|
for line |
|
|
in template_file |
|
|
in template_file |
|
|
if not commentPattern.match(line) |
|
|
if not comment_pattern.match(line) |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
for index, line in enumerate(content): |
|
|
for index, line in enumerate(content): |
|
@ -225,24 +228,35 @@ def makeTexFile(sb, output): |
|
|
out.write(u''.join(content)) |
|
|
out.write(u''.join(content)) |
|
|
out.close() |
|
|
out.close() |
|
|
|
|
|
|
|
|
def buildsongbook(sb, basename, interactive = False, logger = logging.getLogger()): |
|
|
def buildsongbook( |
|
|
|
|
|
songbook, |
|
|
|
|
|
basename, |
|
|
|
|
|
interactive=False, |
|
|
|
|
|
logger=logging.getLogger() |
|
|
|
|
|
): |
|
|
"""Build a songbook |
|
|
"""Build a songbook |
|
|
|
|
|
|
|
|
Arguments: |
|
|
Arguments: |
|
|
- sb: Python representation of the .sb songbook configuration file. |
|
|
- songbook: Python representation of the .sb songbook configuration file. |
|
|
- basename: basename of the songbook to be built. |
|
|
- basename: basename of the songbook to be built. |
|
|
- interactive: in False, do not expect anything from stdin. |
|
|
- interactive: in False, do not expect anything from stdin. |
|
|
""" |
|
|
""" |
|
|
|
|
|
|
|
|
texFile = basename + ".tex" |
|
|
tex_file = basename + ".tex" |
|
|
|
|
|
|
|
|
# Make TeX file |
|
|
# Make TeX file |
|
|
makeTexFile(sb, texFile) |
|
|
make_tex_file(songbook, tex_file) |
|
|
|
|
|
|
|
|
if not 'TEXINPUTS' in os.environ.keys(): |
|
|
if not 'TEXINPUTS' in os.environ.keys(): |
|
|
os.environ['TEXINPUTS'] = '' |
|
|
os.environ['TEXINPUTS'] = '' |
|
|
os.environ['TEXINPUTS'] += os.pathsep + os.path.join(__SHAREDIR__, 'latex') |
|
|
os.environ['TEXINPUTS'] += os.pathsep + os.path.join( |
|
|
os.environ['TEXINPUTS'] += os.pathsep + os.path.join(sb['datadir'], 'latex') |
|
|
__SHAREDIR__, |
|
|
|
|
|
'latex', |
|
|
|
|
|
) |
|
|
|
|
|
os.environ['TEXINPUTS'] += os.pathsep + os.path.join( |
|
|
|
|
|
songbook['datadir'], |
|
|
|
|
|
'latex', |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
# pdflatex options |
|
|
# pdflatex options |
|
|
pdflatex_options = [] |
|
|
pdflatex_options = [] |
|
@ -251,20 +265,20 @@ def buildsongbook(sb, basename, interactive = False, logger = logging.getLogger( |
|
|
pdflatex_options.append("-halt-on-error") |
|
|
pdflatex_options.append("-halt-on-error") |
|
|
|
|
|
|
|
|
# First pdflatex pass |
|
|
# First pdflatex pass |
|
|
if subprocess.call(["pdflatex"] + pdflatex_options + [texFile]): |
|
|
if subprocess.call(["pdflatex"] + pdflatex_options + [tex_file]): |
|
|
raise errors.LatexCompilationError(basename) |
|
|
raise errors.LatexCompilationError(basename) |
|
|
|
|
|
|
|
|
# Make index |
|
|
# Make index |
|
|
sxdFiles = glob.glob("%s_*.sxd" % basename) |
|
|
sxd_files = glob.glob("%s_*.sxd" % basename) |
|
|
for sxdFile in sxdFiles: |
|
|
for sxd_file in sxd_files: |
|
|
logger.info("processing " + sxdFile) |
|
|
logger.info("processing " + sxd_file) |
|
|
idx = processSXD(sxdFile) |
|
|
idx = process_sxd(sxd_file) |
|
|
indexFile = open(sxdFile[:-3] + "sbx", "w") |
|
|
index_file = open(sxd_file[:-3] + "sbx", "w") |
|
|
indexFile.write(idx.entriesToStr().encode('utf8')) |
|
|
index_file.write(idx.entries_to_str().encode('utf8')) |
|
|
indexFile.close() |
|
|
index_file.close() |
|
|
|
|
|
|
|
|
# Second pdflatex pass |
|
|
# Second pdflatex pass |
|
|
if subprocess.call(["pdflatex"] + pdflatex_options + [texFile]): |
|
|
if subprocess.call(["pdflatex"] + pdflatex_options + [tex_file]): |
|
|
raise errors.LatexCompilationError(basename) |
|
|
raise errors.LatexCompilationError(basename) |
|
|
|
|
|
|
|
|
# Cleaning |
|
|
# Cleaning |