From 352aff24b0340e0cdbe9303dfe9d8f801a5c1c79 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 9 Oct 2015 22:51:09 +0200 Subject: [PATCH] [songbook] Added step interpolation --- patacrep/build.py | 27 ++++++++++++++++++++----- patacrep/errors.py | 48 ++++++++++++++++++++++++-------------------- patacrep/songbook.py | 4 ++++ 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/patacrep/build.py b/patacrep/build.py index 5682da1d..aa27b7cf 100644 --- a/patacrep/build.py +++ b/patacrep/build.py @@ -263,13 +263,30 @@ class SongbookBuilder(object): with codecs.open(sxd_file[:-3] + "sbx", "w", "utf-8") as index_file: index_file.write(idx.entries_to_str()) - @staticmethod - def build_custom(command): + def build_custom(self, command): """Run a shell command""" - LOGGER.info("Running '{}'…".format(command)) - exit_code = call(command, shell=True) + interpolation = { + "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: - raise errors.StepCommandError(command, exit_code) + raise errors.StepCommandError(formatted_command, exit_code) def clean(self): """Clean (some) temporary files used during compilation. diff --git a/patacrep/errors.py b/patacrep/errors.py index 7ecd7389..5ec9fd9b 100644 --- a/patacrep/errors.py +++ b/patacrep/errors.py @@ -31,30 +31,36 @@ class TemplateError(SongbookError): else: return self.message -class LatexCompilationError(SongbookError): - """Error during LaTeX compilation.""" +class StepError(SongbookError): + """Error during execution of one compilation step.""" - def __init__(self, basename): - super(LatexCompilationError, self).__init__() - self.basename = basename + def __init__(self, message): + super(StepError, self).__init__() + self.message = message def __str__(self): - return ( - """Error while pdfLaTeX compilation of "{basename}.tex" """ - """(see {basename}.log for more information).""" - ).format(basename=self.basename) + return self.message + +class LatexCompilationError(StepError): + """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.""" def __init__(self, command, code): - super(StepCommandError, self).__init__() - self.command = command - self.code = code + super(StepCommandError, self).__init__(( + """Error while running custom command "{command}": got return""" + " 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): """Error during cleaning of LaTeX auxiliary files.""" @@ -70,15 +76,13 @@ class CleaningError(SongbookError): exception=str(self.exception) ) -class UnknownStep(SongbookError): +class UnknownStep(StepError): """Unknown compilation step.""" def __init__(self, step): - super(UnknownStep, self).__init__() - self.step = step - - def __str__(self): - return """Compilation step "{step}" unknown.""".format(step=self.step) + super(UnknownStep, self).__init__( + """Compilation step "{step}" unknown.""".format(step=step) + ) class ParsingError(SongbookError): """Parsing error.""" diff --git a/patacrep/songbook.py b/patacrep/songbook.py index 56876ff0..71340cae 100644 --- a/patacrep/songbook.py +++ b/patacrep/songbook.py @@ -79,6 +79,10 @@ def argument_parser(args): in a shell). Several steps (excepted the custom shell command) can be combinend in one --steps argument, as a 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 ".aux", ".log", and so on. """.format(steps=','.join(DEFAULT_STEPS))), default=None, )