Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f02b34a

Browse files
authored
Merge pull request #10250 from anntzer/backend_ps-cleanup
Minor refactor of backend_ps.
2 parents b25bb5c + 5bebf4b commit f02b34a

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ def quote_ps_string(s):
164164
return s.decode('ascii')
165165

166166

167+
def _move_path_to_path_or_stream(src, dst):
168+
"""Move the contents of file at *src* to path-or-filelike *dst*.
169+
170+
If *dst* is a path, the metadata of *src* are *not* copied.
171+
"""
172+
if is_writable_file_like(dst):
173+
fh = (io.open(src, 'r', encoding='latin-1')
174+
if file_requires_unicode(dst)
175+
else io.open(src, 'rb'))
176+
with fh:
177+
shutil.copyfileobj(fh, dst)
178+
else:
179+
# Py3: shutil.move(src, dst, copy_function=shutil.copyfile)
180+
open(dst, 'w').close()
181+
mode = os.stat(dst).st_mode
182+
shutil.move(src, dst)
183+
os.chmod(dst, mode)
184+
185+
167186
class RendererPS(RendererBase):
168187
"""
169188
The renderer handles all the drawing primitives using a graphics
@@ -1153,18 +1172,7 @@ def print_figure_impl(fh):
11531172
elif rcParams['ps.usedistiller'] == 'xpdf':
11541173
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
11551174

1156-
if passed_in_file_object:
1157-
fh = (io.open(tmpfile, 'r', encoding='latin-1')
1158-
if file_requires_unicode(outfile)
1159-
else io.open(tmpfile, 'rb'))
1160-
with fh:
1161-
shutil.copyfileobj(fh, outfile)
1162-
else:
1163-
with io.open(outfile, 'w') as fh:
1164-
pass
1165-
mode = os.stat(outfile).st_mode
1166-
shutil.move(tmpfile, outfile)
1167-
os.chmod(outfile, mode)
1175+
_move_path_to_path_or_stream(tmpfile, outfile)
11681176
finally:
11691177
if os.path.isfile(tmpfile):
11701178
os.unlink(tmpfile)
@@ -1329,10 +1337,9 @@ def write(self, *kl, **kwargs):
13291337
paperWidth, paperHeight = papersize[papertype]
13301338
if (width > paperWidth or height > paperHeight) and isEPSF:
13311339
paperWidth, paperHeight = papersize[temp_papertype]
1332-
_log.info(
1333-
('Your figure is too big to fit on %s paper. %s '
1334-
'paper will be used to prevent clipping.'
1335-
) % (papertype, temp_papertype))
1340+
_log.info('Your figure is too big to fit on %s paper. '
1341+
'%s paper will be used to prevent clipping.',
1342+
papertype, temp_papertype)
13361343

13371344
texmanager = ps_renderer.get_texmanager()
13381345
font_preamble = texmanager.get_font_preamble()
@@ -1352,17 +1359,7 @@ def write(self, *kl, **kwargs):
13521359
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,
13531360
rotated=psfrag_rotated)
13541361

1355-
if is_writable_file_like(outfile):
1356-
with (io.open(tmpfile, 'r', encoding='latin-1')
1357-
if file_requires_unicode(outfile)
1358-
else io.open(tmpfile, 'rb')) as fh:
1359-
shutil.copyfileobj(fh, outfile)
1360-
else:
1361-
with io.open(outfile, 'wb') as fh:
1362-
pass
1363-
mode = os.stat(outfile).st_mode
1364-
shutil.move(tmpfile, outfile)
1365-
os.chmod(outfile, mode)
1362+
_move_path_to_path_or_stream(tmpfile, outfile)
13661363
finally:
13671364
if os.path.isfile(tmpfile):
13681365
os.unlink(tmpfile)
@@ -1483,6 +1480,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
14831480

14841481
return psfrag_rotated
14851482

1483+
14861484
def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
14871485
"""
14881486
Use ghostscript's pswrite or epswrite device to distill a file.

0 commit comments

Comments
 (0)