@@ -164,6 +164,25 @@ def quote_ps_string(s):
164
164
return s .decode ('ascii' )
165
165
166
166
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
+
167
186
class RendererPS (RendererBase ):
168
187
"""
169
188
The renderer handles all the drawing primitives using a graphics
@@ -1153,18 +1172,7 @@ def print_figure_impl(fh):
1153
1172
elif rcParams ['ps.usedistiller' ] == 'xpdf' :
1154
1173
xpdf_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox )
1155
1174
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 )
1168
1176
finally :
1169
1177
if os .path .isfile (tmpfile ):
1170
1178
os .unlink (tmpfile )
@@ -1329,10 +1337,9 @@ def write(self, *kl, **kwargs):
1329
1337
paperWidth , paperHeight = papersize [papertype ]
1330
1338
if (width > paperWidth or height > paperHeight ) and isEPSF :
1331
1339
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 )
1336
1343
1337
1344
texmanager = ps_renderer .get_texmanager ()
1338
1345
font_preamble = texmanager .get_font_preamble ()
@@ -1352,17 +1359,7 @@ def write(self, *kl, **kwargs):
1352
1359
xpdf_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox ,
1353
1360
rotated = psfrag_rotated )
1354
1361
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 )
1366
1363
finally :
1367
1364
if os .path .isfile (tmpfile ):
1368
1365
os .unlink (tmpfile )
@@ -1483,6 +1480,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
1483
1480
1484
1481
return psfrag_rotated
1485
1482
1483
+
1486
1484
def gs_distill (tmpfile , eps = False , ptype = 'letter' , bbox = None , rotated = False ):
1487
1485
"""
1488
1486
Use ghostscript's pswrite or epswrite device to distill a file.
0 commit comments