Browse Source

[songbook] Added step interpolation

pull/113/head
Louis 9 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:
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.

48
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."""

4
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 "<BASENAME>.aux", "<BASENAME>.log", and so on.
""".format(steps=','.join(DEFAULT_STEPS))),
default=None,
)

Loading…
Cancel
Save