Browse Source

[songbook] Added step interpolation

pull/113/head
Louis 10 years ago
parent
commit
352aff24b0
  1. 27
      patacrep/build.py
  2. 48
      patacrep/errors.py
  3. 4
      patacrep/songbook.py

27
patacrep/build.py

@ -263,13 +263,30 @@ class SongbookBuilder(object):
with codecs.open(sxd_file[:-3] + "sbx", "w", "utf-8") as index_file: with codecs.open(sxd_file[:-3] + "sbx", "w", "utf-8") as index_file:
index_file.write(idx.entries_to_str()) index_file.write(idx.entries_to_str())
@staticmethod def build_custom(self, command):
def build_custom(command):
"""Run a shell command""" """Run a shell command"""
LOGGER.info("Running '{}'".format(command)) interpolation = {
exit_code = call(command, shell=True) "basename": self.basename,
}
for index in ['title', 'auth']:
interpolation.update({
'{}_{}'.format(index, extension): '{}_{}.{}'.format(self.basename, index, extension)
for extension in ['sbx', 'sxd']
})
interpolation.update({
extension: '{}.{}'.format(self.basename, extension)
for extension in ["aux", "log", "out", "pdf", "sxc", "tex"]
})
try:
formatted_command = command.format(**interpolation)
except KeyError as error:
raise errors.StepError((
'Custom step: Unknown key "{{{error}}}" in command "{command}".'
).format(error=error.args[0], command=command))
LOGGER.info("Running '{}'".format(formatted_command))
exit_code = call(formatted_command, shell=True)
if exit_code: if exit_code:
raise errors.StepCommandError(command, exit_code) raise errors.StepCommandError(formatted_command, exit_code)
def clean(self): def clean(self):
"""Clean (some) temporary files used during compilation. """Clean (some) temporary files used during compilation.

48
patacrep/errors.py

@ -31,30 +31,36 @@ class TemplateError(SongbookError):
else: else:
return self.message return self.message
class LatexCompilationError(SongbookError): class StepError(SongbookError):
"""Error during LaTeX compilation.""" """Error during execution of one compilation step."""
def __init__(self, basename): def __init__(self, message):
super(LatexCompilationError, self).__init__() super(StepError, self).__init__()
self.basename = basename self.message = message
def __str__(self): def __str__(self):
return ( return self.message
"""Error while pdfLaTeX compilation of "{basename}.tex" """
"""(see {basename}.log for more information).""" class LatexCompilationError(StepError):
).format(basename=self.basename) """Error during LaTeX compilation."""
class StepCommandError(SongbookError): def __init__(self, basename):
super(LatexCompilationError, self).__init__(
(
"""Error while pdfLaTeX compilation of "{basename}.tex" """
"""(see {basename}.log for more information)."""
).format(basename=basename)
)
class StepCommandError(StepError):
"""Error during custom command compilation.""" """Error during custom command compilation."""
def __init__(self, command, code): def __init__(self, command, code):
super(StepCommandError, self).__init__() super(StepCommandError, self).__init__((
self.command = command """Error while running custom command "{command}": got return"""
self.code = code " code {code}."
).format(command=command, code=code))
def __str__(self):
return ("""Error while running custom command "{command}": got return"""
" code {code}.").format(command=self.command, code=self.code)
class CleaningError(SongbookError): class CleaningError(SongbookError):
"""Error during cleaning of LaTeX auxiliary files.""" """Error during cleaning of LaTeX auxiliary files."""
@ -70,15 +76,13 @@ class CleaningError(SongbookError):
exception=str(self.exception) exception=str(self.exception)
) )
class UnknownStep(SongbookError): class UnknownStep(StepError):
"""Unknown compilation step.""" """Unknown compilation step."""
def __init__(self, step): def __init__(self, step):
super(UnknownStep, self).__init__() super(UnknownStep, self).__init__(
self.step = step """Compilation step "{step}" unknown.""".format(step=step)
)
def __str__(self):
return """Compilation step "{step}" unknown.""".format(step=self.step)
class ParsingError(SongbookError): class ParsingError(SongbookError):
"""Parsing error.""" """Parsing error."""

4
patacrep/songbook.py

@ -79,6 +79,10 @@ def argument_parser(args):
in a shell). Several steps (excepted the custom shell in a shell). Several steps (excepted the custom shell
command) can be combinend in one --steps argument, as a command) can be combinend in one --steps argument, as a
comma separated string. comma separated string.
Substring {{basename}} is replaced by the basename of the song
book, and substrings {{aux}}, {{log}}, {{out}}, {{pdf}}, {{sxc}}, {{tex}}
are replaced by "<BASENAME>.aux", "<BASENAME>.log", and so on.
""".format(steps=','.join(DEFAULT_STEPS))), """.format(steps=','.join(DEFAULT_STEPS))),
default=None, default=None,
) )

Loading…
Cancel
Save