Skip to content

Commit 5bd73d2

Browse files
committed
update error handling in Configuration class
1 parent ac0406a commit 5bd73d2

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

pdfkit/configuration.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import os
33
import subprocess
44
import sys
5+
try:
6+
FileNotFoundError
7+
except NameError:
8+
FileNotFoundError = IOError
59

610

711
class Configuration(object):
@@ -10,23 +14,29 @@ def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-', environ=''):
1014

1115
self.wkhtmltopdf = wkhtmltopdf
1216

13-
if not self.wkhtmltopdf:
14-
if sys.platform == 'win32':
15-
self.wkhtmltopdf = subprocess.Popen(
16-
['where.exe', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].split()[0].strip()
17-
else:
18-
self.wkhtmltopdf = subprocess.Popen(
19-
['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].split()[0].strip()
20-
2117
try:
18+
if not self.wkhtmltopdf:
19+
if sys.platform == 'win32':
20+
self.wkhtmltopdf = subprocess.Popen(
21+
['where.exe', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0]
22+
else:
23+
self.wkhtmltopdf = subprocess.Popen(
24+
['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0]
25+
26+
lines = self.wkhtmltopdf.splitlines()
27+
if len(lines) > 0:
28+
self.wkhtmltopdf = lines[0].strip()
29+
2230
with open(self.wkhtmltopdf) as f:
2331
pass
24-
except IOError:
32+
except (IOError, FileNotFoundError) as e:
2533
raise IOError('No wkhtmltopdf executable found: "%s"\n'
2634
'If this file exists please check that this process can '
27-
'read it. Otherwise please install wkhtmltopdf - '
35+
'read it or you can pass path to it manually in method call, '
36+
'check README. Otherwise please install wkhtmltopdf - '
2837
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
2938

39+
3040
self.environ = environ
3141

3242
if not self.environ:

0 commit comments

Comments
 (0)