mirror of
https://github.com/Threnklyn/dungeon-sheets.git
synced 2026-05-19 04:33:26 +02:00
Made pdflatex an optional dependency and a little more reliable.
This commit is contained in:
+7
-1
@@ -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
|
||||
=====
|
||||
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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)
|
||||
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
|
||||
if result == 0:
|
||||
os.remove(tex_file)
|
||||
os.remove(f'{basename}.aux')
|
||||
os.remove(f'{basename}.log')
|
||||
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,7 +334,12 @@ def make_sheet(character_file, flatten=False):
|
||||
sheets.append(spell_base + '.pdf')
|
||||
# Create spell book
|
||||
spellbook_base = os.path.splitext(character_file)[0] + '_spellbook'
|
||||
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'
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user