Skip to content

Commit

Permalink
set cache_dir on generate_pdf by default
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed May 24, 2017
1 parent 75515bf commit 93ee5c1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 44 deletions.
14 changes: 8 additions & 6 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
History
-------

v0.32.0 (2017-05-24)
....................
* set ``cache_dir`` for ``generate_pdf`` by default

v0.31.0 (2017-05-23)
....................
* move to python 3.6 +
* add ``async`` generation for roughly 10x speedup

v0.30.0
.......

- uprev wkhtmltopdf from **0.12.2 (beta)** to **0.12.4**.
- code cleanup
- (this is the same as ``v0.3``, I made a mistake when versioning)
* uprev wkhtmltopdf from **0.12.2 (beta)** to **0.12.4**.
* code cleanup
* this is the same as ``v0.3``, I made a mistake when versioning

v0.21.0
.......

- correct permissions on wkhtmltopdf binary.
* correct permissions on wkhtmltopdf binary.
4 changes: 0 additions & 4 deletions benchmark/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

THIS_DIR = Path(__file__).parent.resolve()
html = (THIS_DIR / 'invoice.html').read_text()
PDF_CACHE = THIS_DIR / 'pdf_cache'
if not PDF_CACHE.exists():
Path.mkdir(PDF_CACHE)
OUT_DIR = THIS_DIR / 'output'
if not OUT_DIR.exists():
Path.mkdir(OUT_DIR)
Expand All @@ -27,7 +24,6 @@ def go_sync():
zoom='1.25',
margin_left='8mm',
margin_right='8mm',
cache_dir=PDF_CACHE,
)
print(f'{i:03}: {len(pdf)}')
file = OUT_DIR / f'output_{i:03}.pdf'
Expand Down
2 changes: 1 addition & 1 deletion pydf/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from distutils.version import StrictVersion

VERSION = StrictVersion('0.31.0')
VERSION = StrictVersion('0.32.0')
53 changes: 26 additions & 27 deletions pydf/wkhtmltopdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

THIS_DIR = Path(__file__).parent.resolve()
WK_PATH = str(THIS_DIR / 'bin' / 'wkhtmltopdf')
DFT_CACHE_DIR = Path(tempfile.gettempdir()) / 'pydf_cache'


def _execute_wk(*args, input=None):
Expand All @@ -30,7 +31,7 @@ def _execute_wk(*args, input=None):
return subprocess.run(wk_args, input=input, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


def _convert_args(py_args):
def _convert_args(**py_args):
cmd_args = []
for name, value in py_args.items():
if value in {None, False}:
Expand Down Expand Up @@ -61,13 +62,12 @@ def _set_meta_data(pdf_content, **kwargs):


class AsyncPydf:
def __init__(self, *, max_processes=20, loop=None):
def __init__(self, *, max_processes=20, loop=None, cache_dir=DFT_CACHE_DIR):
self.semaphore = asyncio.Semaphore(value=max_processes, loop=loop)
self.loop = loop
cache_dir = Path(tempfile.gettempdir()) / 'pydf_cache'
if not cache_dir.exists():
Path.mkdir(cache_dir)
self.cache_dir = str(cache_dir)
self.cache_dir = cache_dir

async def generate_pdf(self,
html,
Expand All @@ -77,8 +77,7 @@ async def generate_pdf(self,
creator=None,
producer=None,
**cmd_args):
cmd_args.setdefault('cache_dir', self.cache_dir)
cmd_args = [WK_PATH] + _convert_args(cmd_args)
cmd_args = [WK_PATH] + _convert_args(cache_dir=self.cache_dir, **cmd_args)
async with self.semaphore:
p = await asyncio.create_subprocess_exec(
*cmd_args,
Expand Down Expand Up @@ -107,25 +106,25 @@ async def generate_pdf(self,


def generate_pdf(html, *,
title=None,
author=None,
subject=None,
creator=None,
producer=None,
title: str=None,
author: str=None,
subject: str=None,
creator: str=None,
producer: str=None,
# from here on arguments are passed via the commandline to wkhtmltopdf
cache_dir=None,
grayscale=False,
lowquality=False,
margin_bottom=None,
margin_left=None,
margin_right=None,
margin_top=None,
orientation=None,
page_height=None,
page_width=None,
page_size=None,
image_dpi=None,
image_quality=None,
cache_dir: Path=DFT_CACHE_DIR,
grayscale: bool=False,
lowquality: bool=False,
margin_bottom: str=None,
margin_left: str=None,
margin_right: str=None,
margin_top: str=None,
orientation: str=None,
page_height: str=None,
page_width: str=None,
page_size: str=None,
image_dpi: str=None,
image_quality: str=None,
**extra_kwargs):
"""
Generate a pdf from either a url or a html string.
Expand Down Expand Up @@ -158,8 +157,8 @@ def generate_pdf(html, *,
:param extra_kwargs: any exotic extra options for wkhtmltopdf
:return: string representing pdf
"""
if html.lstrip().startswith(('http', 'www')):
raise ValueError('pdf generation from urls is not supported')
if not cache_dir.exists():
Path.mkdir(cache_dir)

py_args = dict(
cache_dir=cache_dir,
Expand All @@ -177,7 +176,7 @@ def generate_pdf(html, *,
image_quality=image_quality,
)
py_args.update(extra_kwargs)
cmd_args = _convert_args(py_args)
cmd_args = _convert_args(**py_args)

p = _execute_wk(*cmd_args, input=html.encode())
pdf_content = p.stdout
Expand Down
6 changes: 0 additions & 6 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ def test_extra_kwargs():
assert pdf_content[:4] == b'%PDF'


def test_generate_url():
with pytest.raises(ValueError) as exc_info:
generate_pdf('www.google.com')
assert 'pdf generation from urls is not supported' in str(exc_info)


def test_bad_arguments():
with pytest.raises(RuntimeError) as exc_info:
generate_pdf('hello', foobar='broken')
Expand Down

0 comments on commit 93ee5c1

Please sign in to comment.