From 0d36b5a21bf7d04dd274ab51c42f9fa7a8bcd2bc Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 25 Feb 2014 18:41:40 +0100 Subject: [PATCH] Raise exceptions when failing. Closes #5 --- songbook.py | 9 ++++++++- songbook/build.py | 17 +++++++++-------- songbook/errors.py | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 songbook/errors.py diff --git a/songbook.py b/songbook.py index 90b332ce..765815cb 100755 --- a/songbook.py +++ b/songbook.py @@ -13,6 +13,7 @@ import sys from songbook.build import buildsongbook from songbook import __VERSION__ +from songbook import errors def argument_parser(args): @@ -65,7 +66,13 @@ def main(): ) else: songbook['datadir'] = os.path.dirname(songbook_path) - buildsongbook(songbook, basename) + try: + buildsongbook(songbook, basename) + except errors.SongbookError as error: + print(error) + sys.exit(1) + + sys.exit(0) if __name__ == '__main__': main() diff --git a/songbook/build.py b/songbook/build.py index 52a14bf7..16d8aba4 100755 --- a/songbook/build.py +++ b/songbook/build.py @@ -11,10 +11,11 @@ import os.path import re import sys +from songbook import __SHAREDIR__ +from songbook import errors from songbook.files import recursiveFind from songbook.index import processSXD from songbook.songs import Song, SongsList -from songbook import __SHAREDIR__ EOL = "\n" @@ -95,9 +96,10 @@ def clean(basename): ] for ext in generated_extensions: - os.unlink(basename + ext) - - return True + try: + os.unlink(basename + ext) + except Exception as exception: + raise errors.CleaningError(basename + ext, exception) def makeTexFile(sb, output): @@ -243,7 +245,7 @@ def buildsongbook(sb, basename): # First pdflatex pass if call(["pdflatex", "--shell-escape", texFile]): - sys.exit(1) + raise errors.LatexCompilationError(basename) # Make index sxdFiles = glob.glob("%s_*.sxd" % basename) @@ -256,8 +258,7 @@ def buildsongbook(sb, basename): # Second pdflatex pass if call(["pdflatex", "--shell-escape", texFile]): - sys.exit(1) + raise errors.LatexCompilationError(basename) # Cleaning - if not clean(basename): - sys.exit(1) + clean(basename) diff --git a/songbook/errors.py b/songbook/errors.py new file mode 100644 index 00000000..41a17f60 --- /dev/null +++ b/songbook/errors.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""Songbook exceptions and errors.""" + +class SongbookError(Exception): + pass + +class LatexCompilationError(SongbookError): + """Error during LaTeX compilation.""" + + def __init__(self, basename): + self.basename = basename + + def __str__(self): + return """Error while pdfLaTeX compilation of "{basename}.tex" (see {basename}.log for more information).""".format(basename = self.basename) + +class CleaningError(SongbookError): + """Error during cleaning of LaTeX auxiliary files.""" + + def __init__(self, filename, exception): + self.filename = filename + self.exception = exception + + def __str__(self): + return """Error while removing "{filename}": {exception}.""".format(filename = self.filename, exception = str(self.exception))