diff --git a/patacrep/build.py b/patacrep/build.py index 5682da1d..492f30fc 100644 --- a/patacrep/build.py +++ b/patacrep/build.py @@ -154,6 +154,9 @@ class SongbookBuilder(object): """Run function if it has not been run yet. If it as, return the previous return value. + + Warning: several function calls with different arguments return the + same value. """ if function not in self._called_functions: self._called_functions[function] = function(*args, **kwargs) @@ -263,13 +266,35 @@ 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 _get_interpolation(self): + """Return the interpolation values for a custom command.""" + 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"] + }) + return interpolation + + def build_custom(self, command): """Run a shell command""" - LOGGER.info("Running '{}'…".format(command)) - exit_code = call(command, shell=True) + interpolation = self._run_once(self._get_interpolation) + 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, )