Skip to content

Commit 479d656

Browse files
[CLEANUP] reformat code
1 parent 2c613d6 commit 479d656

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

pdfkit/pdfkit.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,14 @@ def command(self, path=None):
127127

128128
def to_pdf(self, path=None):
129129
args = self.command(path)
130-
131-
result = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
132-
stderr=subprocess.PIPE, env=self.environ)
130+
131+
result = subprocess.Popen(
132+
args,
133+
stdin=subprocess.PIPE,
134+
stdout=subprocess.PIPE,
135+
stderr=subprocess.PIPE,
136+
env=self.environ
137+
)
133138

134139
# If the source is a string then we will pipe it into wkhtmltopdf.
135140
# If we want to add custom CSS to file then we read input file to
@@ -141,18 +146,18 @@ def to_pdf(self, path=None):
141146
input = self.source.source.read().encode('utf-8')
142147
else:
143148
input = None
149+
144150
stdout, stderr = result.communicate(input=input)
145151
stderr = stderr or stdout
146-
152+
stderr = stderr.decode('utf-8', errors='replace')
147153
exit_code = result.returncode
148-
if exit_code != 0:
149-
stderr = stderr.decode('utf-8', errors='replace')
150154

155+
if exit_code != 0:
151156
if 'cannot connect to X server' in stderr:
152157
raise IOError('%s\n'
153-
'You will need to run wkhtmltopdf within a "virtual" X server.\n'
154-
'Go to the link below for more information\n'
155-
'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr)
158+
'You will need to run wkhtmltopdf within a "virtual" X server.\n'
159+
'Go to the link below for more information\n'
160+
'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr)
156161

157162
if 'Error' in stderr:
158163
raise IOError('wkhtmltopdf reported an error:\n' + stderr)
@@ -163,24 +168,24 @@ def to_pdf(self, path=None):
163168
# Since wkhtmltopdf sends its output to stderr we will capture it
164169
# and properly send to stdout
165170
if '--quiet' not in args:
166-
sys.stdout.write(stderr.decode('utf-8', errors='replace'))
171+
sys.stdout.write(stderr)
167172

168173
if not path:
169174
return stdout
170-
else:
171-
try:
172-
with codecs.open(path, encoding='utf-8') as f:
173-
# read 4 bytes to get PDF signature '%PDF'
174-
text = f.read(4)
175-
if text == '':
176-
raise IOError('Command failed: %s\n'
177-
'Check whhtmltopdf output without \'quiet\' '
178-
'option' % ' '.join(args))
179-
return True
180-
except (IOError, OSError) as e:
181-
raise IOError('Command failed: %s\n'
182-
'Check whhtmltopdf output without \'quiet\' option\n'
183-
'%s ' % (' '.join(args), e))
175+
176+
try:
177+
with codecs.open(path, encoding='utf-8') as f:
178+
# read 4 bytes to get PDF signature '%PDF'
179+
text = f.read(4)
180+
if text == '':
181+
raise IOError('Command failed: %s\n'
182+
'Check whhtmltopdf output without \'quiet\' '
183+
'option' % ' '.join(args))
184+
return True
185+
except (IOError, OSError) as e:
186+
raise IOError('Command failed: %s\n'
187+
'Check whhtmltopdf output without \'quiet\' option\n'
188+
'%s ' % (' '.join(args), e))
184189

185190
def _normalize_options(self, options):
186191
""" Generator of 2-tuples (option-key, option-value).
@@ -206,7 +211,6 @@ def _normalize_options(self, options):
206211
else:
207212
yield (normalized_key, str(value) if value else value)
208213

209-
210214
def _normalize_arg(self, arg):
211215
return arg.lower()
212216

0 commit comments

Comments
 (0)