Skip to content

Commit 93ee5c1

Browse files
committed
set cache_dir on generate_pdf by default
1 parent 75515bf commit 93ee5c1

File tree

5 files changed

+35
-44
lines changed

5 files changed

+35
-44
lines changed

HISTORY.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
History
44
-------
55

6+
v0.32.0 (2017-05-24)
7+
....................
8+
* set ``cache_dir`` for ``generate_pdf`` by default
9+
610
v0.31.0 (2017-05-23)
711
....................
812
* move to python 3.6 +
913
* add ``async`` generation for roughly 10x speedup
1014

1115
v0.30.0
1216
.......
13-
14-
- uprev wkhtmltopdf from **0.12.2 (beta)** to **0.12.4**.
15-
- code cleanup
16-
- (this is the same as ``v0.3``, I made a mistake when versioning)
17+
* uprev wkhtmltopdf from **0.12.2 (beta)** to **0.12.4**.
18+
* code cleanup
19+
* this is the same as ``v0.3``, I made a mistake when versioning
1720

1821
v0.21.0
1922
.......
20-
21-
- correct permissions on wkhtmltopdf binary.
23+
* correct permissions on wkhtmltopdf binary.

benchmark/run.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
THIS_DIR = Path(__file__).parent.resolve()
99
html = (THIS_DIR / 'invoice.html').read_text()
10-
PDF_CACHE = THIS_DIR / 'pdf_cache'
11-
if not PDF_CACHE.exists():
12-
Path.mkdir(PDF_CACHE)
1310
OUT_DIR = THIS_DIR / 'output'
1411
if not OUT_DIR.exists():
1512
Path.mkdir(OUT_DIR)
@@ -27,7 +24,6 @@ def go_sync():
2724
zoom='1.25',
2825
margin_left='8mm',
2926
margin_right='8mm',
30-
cache_dir=PDF_CACHE,
3127
)
3228
print(f'{i:03}: {len(pdf)}')
3329
file = OUT_DIR / f'output_{i:03}.pdf'

pydf/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from distutils.version import StrictVersion
22

3-
VERSION = StrictVersion('0.31.0')
3+
VERSION = StrictVersion('0.32.0')

pydf/wkhtmltopdf.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
THIS_DIR = Path(__file__).parent.resolve()
1919
WK_PATH = str(THIS_DIR / 'bin' / 'wkhtmltopdf')
20+
DFT_CACHE_DIR = Path(tempfile.gettempdir()) / 'pydf_cache'
2021

2122

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

3233

33-
def _convert_args(py_args):
34+
def _convert_args(**py_args):
3435
cmd_args = []
3536
for name, value in py_args.items():
3637
if value in {None, False}:
@@ -61,13 +62,12 @@ def _set_meta_data(pdf_content, **kwargs):
6162

6263

6364
class AsyncPydf:
64-
def __init__(self, *, max_processes=20, loop=None):
65+
def __init__(self, *, max_processes=20, loop=None, cache_dir=DFT_CACHE_DIR):
6566
self.semaphore = asyncio.Semaphore(value=max_processes, loop=loop)
6667
self.loop = loop
67-
cache_dir = Path(tempfile.gettempdir()) / 'pydf_cache'
6868
if not cache_dir.exists():
6969
Path.mkdir(cache_dir)
70-
self.cache_dir = str(cache_dir)
70+
self.cache_dir = cache_dir
7171

7272
async def generate_pdf(self,
7373
html,
@@ -77,8 +77,7 @@ async def generate_pdf(self,
7777
creator=None,
7878
producer=None,
7979
**cmd_args):
80-
cmd_args.setdefault('cache_dir', self.cache_dir)
81-
cmd_args = [WK_PATH] + _convert_args(cmd_args)
80+
cmd_args = [WK_PATH] + _convert_args(cache_dir=self.cache_dir, **cmd_args)
8281
async with self.semaphore:
8382
p = await asyncio.create_subprocess_exec(
8483
*cmd_args,
@@ -107,25 +106,25 @@ async def generate_pdf(self,
107106

108107

109108
def generate_pdf(html, *,
110-
title=None,
111-
author=None,
112-
subject=None,
113-
creator=None,
114-
producer=None,
109+
title: str=None,
110+
author: str=None,
111+
subject: str=None,
112+
creator: str=None,
113+
producer: str=None,
115114
# from here on arguments are passed via the commandline to wkhtmltopdf
116-
cache_dir=None,
117-
grayscale=False,
118-
lowquality=False,
119-
margin_bottom=None,
120-
margin_left=None,
121-
margin_right=None,
122-
margin_top=None,
123-
orientation=None,
124-
page_height=None,
125-
page_width=None,
126-
page_size=None,
127-
image_dpi=None,
128-
image_quality=None,
115+
cache_dir: Path=DFT_CACHE_DIR,
116+
grayscale: bool=False,
117+
lowquality: bool=False,
118+
margin_bottom: str=None,
119+
margin_left: str=None,
120+
margin_right: str=None,
121+
margin_top: str=None,
122+
orientation: str=None,
123+
page_height: str=None,
124+
page_width: str=None,
125+
page_size: str=None,
126+
image_dpi: str=None,
127+
image_quality: str=None,
129128
**extra_kwargs):
130129
"""
131130
Generate a pdf from either a url or a html string.
@@ -158,8 +157,8 @@ def generate_pdf(html, *,
158157
:param extra_kwargs: any exotic extra options for wkhtmltopdf
159158
:return: string representing pdf
160159
"""
161-
if html.lstrip().startswith(('http', 'www')):
162-
raise ValueError('pdf generation from urls is not supported')
160+
if not cache_dir.exists():
161+
Path.mkdir(cache_dir)
163162

164163
py_args = dict(
165164
cache_dir=cache_dir,
@@ -177,7 +176,7 @@ def generate_pdf(html, *,
177176
image_quality=image_quality,
178177
)
179178
py_args.update(extra_kwargs)
180-
cmd_args = _convert_args(py_args)
179+
cmd_args = _convert_args(**py_args)
181180

182181
p = _execute_wk(*cmd_args, input=html.encode())
183182
pdf_content = p.stdout

tests/test_sync.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ def test_extra_kwargs():
7272
assert pdf_content[:4] == b'%PDF'
7373

7474

75-
def test_generate_url():
76-
with pytest.raises(ValueError) as exc_info:
77-
generate_pdf('www.google.com')
78-
assert 'pdf generation from urls is not supported' in str(exc_info)
79-
80-
8175
def test_bad_arguments():
8276
with pytest.raises(RuntimeError) as exc_info:
8377
generate_pdf('hello', foobar='broken')

0 commit comments

Comments
 (0)