@@ -1288,112 +1288,115 @@ def write(self, *kl, **kwargs):
1288
1288
# write to a temp file, we'll move it to outfile when done
1289
1289
1290
1290
fd , tmpfile = mkstemp ()
1291
-
1292
- with io .open (fd , 'w' , encoding = 'latin-1' ) as fh :
1293
- # write the Encapsulated PostScript headers
1294
- print ("%!PS-Adobe-3.0 EPSF-3.0" , file = fh )
1295
- if title :
1296
- print ("%%Title: " + title , file = fh )
1297
- print ("%%Creator: " + creator_str , file = fh )
1298
- # get source date from SOURCE_DATE_EPOCH, if set
1299
- # See https://reproducible-builds.org/specs/source-date-epoch/
1300
- source_date_epoch = os .getenv ("SOURCE_DATE_EPOCH" )
1301
- if source_date_epoch :
1302
- source_date = datetime .datetime .utcfromtimestamp (
1303
- int (source_date_epoch )).strftime (
1304
- "%a %b %d %H:%M:%S %Y" )
1305
- else :
1306
- source_date = time .ctime ()
1307
- print ("%%CreationDate: " + source_date , file = fh )
1308
- print ("%%%%BoundingBox: %d %d %d %d" % bbox , file = fh )
1309
- print ("%%EndComments" , file = fh )
1310
-
1311
- Ndict = len (psDefs )
1312
- print ("%%BeginProlog" , file = fh )
1313
- print ("/mpldict %d dict def" % Ndict , file = fh )
1314
- print ("mpldict begin" , file = fh )
1315
- for d in psDefs :
1316
- d = d .strip ()
1317
- for l in d .split ('\n ' ):
1318
- print (l .strip (), file = fh )
1319
- print ("end" , file = fh )
1320
- print ("%%EndProlog" , file = fh )
1321
-
1322
- print ("mpldict begin" , file = fh )
1323
- print ("%s translate" % _nums_to_str (xo , yo ), file = fh )
1324
- print ("%s clipbox" % _nums_to_str (width * 72 , height * 72 , 0 , 0 ),
1325
- file = fh )
1326
-
1327
- # write the figure
1328
- print (self ._pswriter .getvalue (), file = fh )
1329
-
1330
- # write the trailer
1331
- print ("end" , file = fh )
1332
- print ("showpage" , file = fh )
1333
- fh .flush ()
1334
-
1335
- if isLandscape : # now we are ready to rotate
1336
- isLandscape = True
1337
- width , height = height , width
1338
- bbox = (lly , llx , ury , urx )
1339
-
1340
- # set the paper size to the figure size if isEPSF. The
1341
- # resulting ps file has the given size with correct bounding
1342
- # box so that there is no need to call 'pstoeps'
1343
- if isEPSF :
1344
- paperWidth , paperHeight = self .figure .get_size_inches ()
1345
- if isLandscape :
1346
- paperWidth , paperHeight = paperHeight , paperWidth
1347
- else :
1348
- temp_papertype = _get_papertype (width , height )
1349
- if papertype == 'auto' :
1350
- papertype = temp_papertype
1351
- paperWidth , paperHeight = papersize [temp_papertype ]
1291
+ try :
1292
+ with io .open (fd , 'w' , encoding = 'latin-1' ) as fh :
1293
+ # write the Encapsulated PostScript headers
1294
+ print ("%!PS-Adobe-3.0 EPSF-3.0" , file = fh )
1295
+ if title :
1296
+ print ("%%Title: " + title , file = fh )
1297
+ print ("%%Creator: " + creator_str , file = fh )
1298
+ # get source date from SOURCE_DATE_EPOCH, if set
1299
+ # See https://reproducible-builds.org/specs/source-date-epoch/
1300
+ source_date_epoch = os .getenv ("SOURCE_DATE_EPOCH" )
1301
+ if source_date_epoch :
1302
+ source_date = datetime .datetime .utcfromtimestamp (
1303
+ int (source_date_epoch )).strftime (
1304
+ "%a %b %d %H:%M:%S %Y" )
1305
+ else :
1306
+ source_date = time .ctime ()
1307
+ print ("%%CreationDate: " + source_date , file = fh )
1308
+ print ("%%%%BoundingBox: %d %d %d %d" % bbox , file = fh )
1309
+ print ("%%EndComments" , file = fh )
1310
+
1311
+ Ndict = len (psDefs )
1312
+ print ("%%BeginProlog" , file = fh )
1313
+ print ("/mpldict %d dict def" % Ndict , file = fh )
1314
+ print ("mpldict begin" , file = fh )
1315
+ for d in psDefs :
1316
+ d = d .strip ()
1317
+ for l in d .split ('\n ' ):
1318
+ print (l .strip (), file = fh )
1319
+ print ("end" , file = fh )
1320
+ print ("%%EndProlog" , file = fh )
1321
+
1322
+ print ("mpldict begin" , file = fh )
1323
+ print ("%s translate" % _nums_to_str (xo , yo ), file = fh )
1324
+ print ("%s clipbox" % _nums_to_str (width * 72 , height * 72 , 0 , 0 ),
1325
+ file = fh )
1326
+
1327
+ # write the figure
1328
+ print (self ._pswriter .getvalue (), file = fh )
1329
+
1330
+ # write the trailer
1331
+ print ("end" , file = fh )
1332
+ print ("showpage" , file = fh )
1333
+ fh .flush ()
1334
+
1335
+ if isLandscape : # now we are ready to rotate
1336
+ isLandscape = True
1337
+ width , height = height , width
1338
+ bbox = (lly , llx , ury , urx )
1339
+
1340
+ # set the paper size to the figure size if isEPSF. The
1341
+ # resulting ps file has the given size with correct bounding
1342
+ # box so that there is no need to call 'pstoeps'
1343
+ if isEPSF :
1344
+ paperWidth , paperHeight = self .figure .get_size_inches ()
1345
+ if isLandscape :
1346
+ paperWidth , paperHeight = paperHeight , paperWidth
1352
1347
else :
1353
- paperWidth , paperHeight = papersize [papertype ]
1354
- if (width > paperWidth or height > paperHeight ) and isEPSF :
1348
+ temp_papertype = _get_papertype (width , height )
1349
+ if papertype == 'auto' :
1350
+ papertype = temp_papertype
1355
1351
paperWidth , paperHeight = papersize [temp_papertype ]
1356
- verbose .report (
1357
- ('Your figure is too big to fit on %s paper. %s '
1358
- 'paper will be used to prevent clipping.'
1359
- ) % (papertype , temp_papertype ), 'helpful' )
1360
-
1361
- texmanager = ps_renderer .get_texmanager ()
1362
- font_preamble = texmanager .get_font_preamble ()
1363
- custom_preamble = texmanager .get_custom_preamble ()
1364
-
1365
- psfrag_rotated = convert_psfrags (tmpfile , ps_renderer .psfrag ,
1366
- font_preamble ,
1367
- custom_preamble , paperWidth ,
1368
- paperHeight ,
1369
- orientation )
1370
-
1371
- if rcParams ['ps.usedistiller' ] == 'ghostscript' :
1372
- gs_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox ,
1373
- rotated = psfrag_rotated )
1374
- elif rcParams ['ps.usedistiller' ] == 'xpdf' :
1375
- xpdf_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox ,
1376
- rotated = psfrag_rotated )
1377
- elif rcParams ['text.usetex' ]:
1378
- if False :
1379
- pass # for debugging
1380
- else :
1352
+ else :
1353
+ paperWidth , paperHeight = papersize [papertype ]
1354
+ if (width > paperWidth or height > paperHeight ) and isEPSF :
1355
+ paperWidth , paperHeight = papersize [temp_papertype ]
1356
+ verbose .report (
1357
+ ('Your figure is too big to fit on %s paper. %s '
1358
+ 'paper will be used to prevent clipping.'
1359
+ ) % (papertype , temp_papertype ), 'helpful' )
1360
+
1361
+ texmanager = ps_renderer .get_texmanager ()
1362
+ font_preamble = texmanager .get_font_preamble ()
1363
+ custom_preamble = texmanager .get_custom_preamble ()
1364
+
1365
+ psfrag_rotated = convert_psfrags (tmpfile , ps_renderer .psfrag ,
1366
+ font_preamble ,
1367
+ custom_preamble , paperWidth ,
1368
+ paperHeight ,
1369
+ orientation )
1370
+
1371
+ if rcParams ['ps.usedistiller' ] == 'ghostscript' :
1381
1372
gs_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox ,
1382
1373
rotated = psfrag_rotated )
1374
+ elif rcParams ['ps.usedistiller' ] == 'xpdf' :
1375
+ xpdf_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox ,
1376
+ rotated = psfrag_rotated )
1377
+ elif rcParams ['text.usetex' ]:
1378
+ if False :
1379
+ pass # for debugging
1380
+ else :
1381
+ gs_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox ,
1382
+ rotated = psfrag_rotated )
1383
1383
1384
- if is_writable_file_like (outfile ):
1385
- if file_requires_unicode (outfile ):
1386
- with io .open (tmpfile , 'rb' ) as fh :
1387
- outfile .write (fh .read ().decode ('latin-1' ))
1384
+ if is_writable_file_like (outfile ):
1385
+ if file_requires_unicode (outfile ):
1386
+ with io .open (tmpfile , 'rb' ) as fh :
1387
+ outfile .write (fh .read ().decode ('latin-1' ))
1388
+ else :
1389
+ with io .open (tmpfile , 'rb' ) as fh :
1390
+ outfile .write (fh .read ())
1388
1391
else :
1389
- with io .open (tmpfile , 'rb ' ) as fh :
1390
- outfile . write ( fh . read ())
1391
- else :
1392
- with io . open ( outfile , 'wb' ) as fh :
1393
- pass
1394
- mode = os . stat ( outfile ). st_mode
1395
- shutil . move (tmpfile , outfile )
1396
- os .chmod ( outfile , mode )
1392
+ with io .open (outfile , 'wb ' ) as fh :
1393
+ pass
1394
+ mode = os . stat ( outfile ). st_mode
1395
+ shutil . move ( tmpfile , outfile )
1396
+ os . chmod ( outfile , mode )
1397
+ finally :
1398
+ if os . path . isfile (tmpfile ):
1399
+ os .unlink ( tmpfile )
1397
1400
1398
1401
1399
1402
def convert_psfrags (tmpfile , psfrags , font_preamble , custom_preamble ,
0 commit comments