Skip to content

Commit

Permalink
also extract WeasyPrint version
Browse files Browse the repository at this point in the history
This might become handy later when we need to use WeasyPrint's PDF/A mode
which requires additional CLI flags.
  • Loading branch information
FelixSchwarz committed May 13, 2024
1 parent 2cff7d3 commit 620c706
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ install_requires =
docopt-ng
jinja2
lxml
packaging


22 changes: 21 additions & 1 deletion src/schwarz/pyinvoice/pdf_generator.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@

from contextlib import contextmanager
from pathlib import Path
import re
import subprocess
import sys
from tempfile import NamedTemporaryFile

from babel.support import Format, Translations
import jinja2
from packaging.version import Version

from .templating import templated_text


__all__ = ['generate_pdf', 'is_weasyprint_available']

_weasyprint_version = None

def is_weasyprint_available():
try:
subprocess.run(['weasyprint', '--version'], capture_output=True, shell=False)
process = subprocess.run(['weasyprint', '--version'], capture_output=True, shell=False)
except FileNotFoundError:
return False
global _weasyprint_version
_weasyprint_version = _extract_weasyprint_version(process.stdout)
return True


def _extract_weasyprint_version(weasyprint_stdout):
version_pattern = br'WeasyPrint version (\d+\.\d+)$'
match = re.search(version_pattern, weasyprint_stdout.strip())
if not match:
sys.stderr.write('Could not determine WeasyPrint version\n')
sys.stderr.write('Output of "weasyprint --version": %r\n' % weasyprint_stdout)
sys.exit(10)
version_str = match.group(1).decode('ascii')
# LATER: check for minimum version (pdf-identifier)
return Version(version_str)


def build_formatter(locale, currency):
_format = Format(locale=locale)
_format.amount = lambda v: _format.currency(v, currency=currency)
Expand Down

0 comments on commit 620c706

Please sign in to comment.