-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathconfiguration.py
More file actions
61 lines (50 loc) · 2.56 KB
/
configuration.py
File metadata and controls
61 lines (50 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
import os
import subprocess
import sys
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
class Configuration(object):
def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-', environ=''):
self.meta_tag_prefix = meta_tag_prefix
self.wkhtmltopdf = wkhtmltopdf
try:
if not self.wkhtmltopdf:
if sys.platform == 'win32':
#hide cmd window
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
self.wkhtmltopdf = subprocess.Popen(
['where.exe', 'wkhtmltopdf'], stdout=subprocess.PIPE, startupinfo=startupinfo).communicate()[0]
else:
try:
subprocess.Popen(
['which'], stdout=subprocess.PIPE).communicate()[0]
except (IOError, FileNotFoundError) as e:
raise IOError('No which executable found. '
'If this file exists please check that this process can '
'read it or you can pass path to it manually in method call, '
'check README. Otherwise please install which - '
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-which')
self.wkhtmltopdf = subprocess.Popen(
['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0]
lines = self.wkhtmltopdf.splitlines()
if len(lines) > 0:
self.wkhtmltopdf = lines[0].strip()
with open(self.wkhtmltopdf) as f:
pass
except (IOError, FileNotFoundError) as e:
raise IOError('No wkhtmltopdf executable found: "%s"\n'
'If this file exists please check that this process can '
'read it or you can pass path to it manually in method call, '
'check README. Otherwise please install wkhtmltopdf - '
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
self.environ = environ
if not self.environ:
self.environ = os.environ
for key in self.environ.keys():
if not isinstance(self.environ[key], str):
self.environ[key] = str(self.environ[key])