diff --git a/README.rst b/README.rst index 1cf9ca9..7ceac6d 100644 --- a/README.rst +++ b/README.rst @@ -28,7 +28,7 @@ External dependencies ===================== * You will need **pdftk** installed to generate the sheets in PDF format. -* You will need **pdflatex** installed to generate the PDF spell pages. +* You will need **pdflatex** installed to generate the PDF spell pages (optional). .. note:: @@ -37,6 +37,12 @@ External dependencies is not available in some RPM distributions, such as Fedora and CentOS. One alternative would be to build your PC sheets using docker. +.. note:: + + If the ``pdflatex`` command is available on your system, + spellcasters will include a spellbook with descriptions of each + spell known. If not, then this feature will be skipped. + Usage ===== diff --git a/VERSION b/VERSION index 267577d..f7abe27 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.1 +0.4.2 \ No newline at end of file diff --git a/dungeonsheets/exceptions.py b/dungeonsheets/exceptions.py index fee0c22..db6d580 100644 --- a/dungeonsheets/exceptions.py +++ b/dungeonsheets/exceptions.py @@ -1,3 +1,8 @@ class DiceError(ValueError): """Improper formatting for a dice string.""" - pass + +class LatexError(OSError): + """PDFLatex did not execute correctly.""" + +class LatexNotFoundError(LatexError): + """PDFLatex did not execute correctly.""" diff --git a/dungeonsheets/make_sheets.py b/dungeonsheets/make_sheets.py index d97aeb1..e93d98c 100644 --- a/dungeonsheets/make_sheets.py +++ b/dungeonsheets/make_sheets.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +import logging +log = logging.getLogger(__name__) import argparse import importlib.util import os @@ -8,9 +10,10 @@ import warnings from fdfgen import forge_fdf -from dungeonsheets import character +from dungeonsheets import character, exceptions from dungeonsheets.stats import mod_str + """Program to take character definitions and build a PDF of the character sheet.""" @@ -67,17 +70,29 @@ def create_spellbook_pdf(character, basename): tex_file = f'{basename}.tex' with open(tex_file, mode='w') as f: f.write(tex) + # Convenience function for removing temporary files + def remove_temp_files(basename_): + filenames = [f'{basename_}.tex', f'{basename_}.aux', + f'{basename_}.log'] + for filename in filenames: + if os.path.exists(filename): + os.remove(filename) # Compile the PDF pdf_file = f'{basename}.pdf' - output_dir = os.path.dirname(pdf_file) - result = subprocess.call(['pdflatex', '--output-directory', - output_dir, tex_file], - stdout=subprocess.DEVNULL) - # Remove temporary files - if result == 0: - os.remove(tex_file) - os.remove(f'{basename}.aux') - os.remove(f'{basename}.log') + output_dir = os.path.abspath(os.path.dirname(pdf_file)) + try: + result = subprocess.run(['pdflatex', '--output-directory', + output_dir, tex_file, '-halt-on-error'], + stdout=subprocess.DEVNULL, timeout=10) + except FileNotFoundError: + # Remove temporary files + remove_temp_files(basename) + raise exceptions.LatexNotFoundError() + else: + if result.returncode == 0: + remove_temp_files(basename) + else: + raise exceptions.LatexError(f'Processing of {basename}.tex failed.') def create_spells_pdf(character, basename, flatten=False): @@ -319,8 +334,13 @@ def make_sheet(character_file, flatten=False): sheets.append(spell_base + '.pdf') # Create spell book spellbook_base = os.path.splitext(character_file)[0] + '_spellbook' - create_spellbook_pdf(character=char, basename=spellbook_base) - sheets.append(spellbook_base + '.pdf') + try: + create_spellbook_pdf(character=char, basename=spellbook_base) + except exceptions.LatexNotFoundError as e: + log.warning('``pdflatex`` not available. Skipping spellbook ' + f'for {char.name}') + else: + sheets.append(spellbook_base + '.pdf') # Combine sheets into final pdf final_pdf = os.path.splitext(character_file)[0] + '.pdf' popenargs = ('pdftk', *sheets, 'cat', 'output', final_pdf) diff --git a/examples/rogue.pdf b/examples/rogue.pdf index 84e9039..3477fee 100644 Binary files a/examples/rogue.pdf and b/examples/rogue.pdf differ diff --git a/examples/warlock.pdf b/examples/warlock.pdf index bf6595d..319d40b 100644 Binary files a/examples/warlock.pdf and b/examples/warlock.pdf differ diff --git a/examples/wizard.pdf b/examples/wizard.pdf index 8310cf6..9c49884 100644 Binary files a/examples/wizard.pdf and b/examples/wizard.pdf differ