Made pdflatex an optional dependency and a little more reliable.

This commit is contained in:
Mark Wolfman
2018-10-14 15:21:07 -05:00
parent b09fb2430b
commit 0648a08d0c
7 changed files with 46 additions and 15 deletions
+7 -1
View File
@@ -28,7 +28,7 @@ External dependencies
===================== =====================
* You will need **pdftk** installed to generate the sheets in PDF format. * 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:: .. note::
@@ -37,6 +37,12 @@ External dependencies
is not available in some RPM distributions, such as Fedora and CentOS. is not available in some RPM distributions, such as Fedora and CentOS.
One alternative would be to build your PC sheets using docker. 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 Usage
===== =====
+1 -1
View File
@@ -1 +1 @@
0.4.1 0.4.2
+6 -1
View File
@@ -1,3 +1,8 @@
class DiceError(ValueError): class DiceError(ValueError):
"""Improper formatting for a dice string.""" """Improper formatting for a dice string."""
pass
class LatexError(OSError):
"""PDFLatex did not execute correctly."""
class LatexNotFoundError(LatexError):
"""PDFLatex did not execute correctly."""
+29 -9
View File
@@ -1,5 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import logging
log = logging.getLogger(__name__)
import argparse import argparse
import importlib.util import importlib.util
import os import os
@@ -8,9 +10,10 @@ import warnings
from fdfgen import forge_fdf from fdfgen import forge_fdf
from dungeonsheets import character from dungeonsheets import character, exceptions
from dungeonsheets.stats import mod_str from dungeonsheets.stats import mod_str
"""Program to take character definitions and build a PDF of the """Program to take character definitions and build a PDF of the
character sheet.""" character sheet."""
@@ -67,17 +70,29 @@ def create_spellbook_pdf(character, basename):
tex_file = f'{basename}.tex' tex_file = f'{basename}.tex'
with open(tex_file, mode='w') as f: with open(tex_file, mode='w') as f:
f.write(tex) 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 # Compile the PDF
pdf_file = f'{basename}.pdf' pdf_file = f'{basename}.pdf'
output_dir = os.path.dirname(pdf_file) output_dir = os.path.abspath(os.path.dirname(pdf_file))
result = subprocess.call(['pdflatex', '--output-directory', try:
output_dir, tex_file], result = subprocess.run(['pdflatex', '--output-directory',
stdout=subprocess.DEVNULL) output_dir, tex_file, '-halt-on-error'],
stdout=subprocess.DEVNULL, timeout=10)
except FileNotFoundError:
# Remove temporary files # Remove temporary files
if result == 0: remove_temp_files(basename)
os.remove(tex_file) raise exceptions.LatexNotFoundError()
os.remove(f'{basename}.aux') else:
os.remove(f'{basename}.log') 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): def create_spells_pdf(character, basename, flatten=False):
@@ -319,7 +334,12 @@ def make_sheet(character_file, flatten=False):
sheets.append(spell_base + '.pdf') sheets.append(spell_base + '.pdf')
# Create spell book # Create spell book
spellbook_base = os.path.splitext(character_file)[0] + '_spellbook' spellbook_base = os.path.splitext(character_file)[0] + '_spellbook'
try:
create_spellbook_pdf(character=char, basename=spellbook_base) 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') sheets.append(spellbook_base + '.pdf')
# Combine sheets into final pdf # Combine sheets into final pdf
final_pdf = os.path.splitext(character_file)[0] + '.pdf' final_pdf = os.path.splitext(character_file)[0] + '.pdf'
Binary file not shown.
Binary file not shown.
Binary file not shown.