Browse Source

Raise exceptions when failing. Closes #5

pull/20/head
Louis 11 years ago
parent
commit
0d36b5a21b
  1. 7
      songbook.py
  2. 15
      songbook/build.py
  3. 26
      songbook/errors.py

7
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)
try:
buildsongbook(songbook, basename)
except errors.SongbookError as error:
print(error)
sys.exit(1)
sys.exit(0)
if __name__ == '__main__':
main()

15
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:
try:
os.unlink(basename + ext)
return True
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)

26
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))
Loading…
Cancel
Save