11"""
2- A PostScript backend, which can produce both PostScript .ps and .eps
2+ A PostScript backend, which can produce both PostScript .ps and .eps.
33"""
4- import glob , os , shutil , sys , time , datetime
5- import io
4+
5+ import binascii
6+ import datetime
7+ import glob
8+ from io import StringIO , TextIOWrapper
69import logging
10+ import os
11+ import pathlib
12+ import re
13+ import shutil
714import subprocess
8-
15+ import sys
916from tempfile import mkstemp
17+ import time
18+
19+ import numpy as np
20+
1021from matplotlib import cbook , __version__ , rcParams , checkdep_ghostscript
1122from matplotlib .afm import AFM
1223from matplotlib .backend_bases import (
1324 _Backend , FigureCanvasBase , FigureManagerBase , GraphicsContextBase ,
1425 RendererBase )
15-
1626from matplotlib .cbook import (get_realpath_and_stat , is_writable_file_like ,
1727 maxdict , file_requires_unicode )
18-
1928from matplotlib .font_manager import findfont , is_opentype_cff_font , get_font
2029from matplotlib .ft2font import KERNING_DEFAULT , LOAD_NO_HINTING
2130from matplotlib .ttconv import convert_ttf_to_ps
2433from matplotlib .path import Path
2534from matplotlib import _path
2635from matplotlib .transforms import Affine2D
27-
2836from matplotlib .backends .backend_mixed import MixedModeRenderer
2937
30-
31- import numpy as np
32- import binascii
33- import re
34-
3538_log = logging .getLogger (__name__ )
3639
3740backend_version = 'Level II'
@@ -159,17 +162,13 @@ def _move_path_to_path_or_stream(src, dst):
159162 If *dst* is a path, the metadata of *src* are *not* copied.
160163 """
161164 if is_writable_file_like (dst ):
162- fh = (io . open (src , 'r' , encoding = 'latin-1' )
165+ fh = (open (src , 'r' , encoding = 'latin-1' )
163166 if file_requires_unicode (dst )
164- else io . open (src , 'rb' ))
167+ else open (src , 'rb' ))
165168 with fh :
166169 shutil .copyfileobj (fh , dst )
167170 else :
168- # Py3: shutil.move(src, dst, copy_function=shutil.copyfile)
169- open (dst , 'w' ).close ()
170- mode = os .stat (dst ).st_mode
171- shutil .move (src , dst )
172- os .chmod (dst , mode )
171+ shutil .move (src , dst , copy_function = shutil .copyfile )
173172
174173
175174class RendererPS (RendererBase ):
@@ -369,7 +368,7 @@ def _get_font_afm(self, prop):
369368 "Helvetica" , fontext = 'afm' , directory = self ._afm_font_dir )
370369 font = self .afmfontd .get (fname )
371370 if font is None :
372- with io . open (fname , 'rb' ) as fh :
371+ with open (fname , 'rb' ) as fh :
373372 font = AFM (fh )
374373 self .afmfontd [fname ] = font
375374 self .afmfontd [key ] = font
@@ -1030,7 +1029,7 @@ def write(self, *kl, **kwargs):
10301029
10311030 self ._pswriter = NullWriter ()
10321031 else :
1033- self ._pswriter = io . StringIO ()
1032+ self ._pswriter = StringIO ()
10341033
10351034 # mixed mode rendering
10361035 ps_renderer = self ._renderer_class (width , height , self ._pswriter ,
@@ -1153,7 +1152,7 @@ def print_figure_impl(fh):
11531152 # Write to a temporary file.
11541153 fd , tmpfile = mkstemp ()
11551154 try :
1156- with io . open (fd , 'w' , encoding = 'latin-1' ) as fh :
1155+ with open (fd , 'w' , encoding = 'latin-1' ) as fh :
11571156 print_figure_impl (fh )
11581157 if rcParams ['ps.usedistiller' ] == 'ghostscript' :
11591158 gs_distill (tmpfile , isEPSF , ptype = papertype , bbox = bbox )
@@ -1171,10 +1170,9 @@ def print_figure_impl(fh):
11711170 requires_unicode = file_requires_unicode (outfile )
11721171
11731172 if not requires_unicode :
1174- fh = io .TextIOWrapper (outfile , encoding = "latin-1" )
1175-
1176- # Prevent the io.TextIOWrapper from closing the
1177- # underlying file
1173+ fh = TextIOWrapper (outfile , encoding = "latin-1" )
1174+ # Prevent the TextIOWrapper from closing the underlying
1175+ # file.
11781176 def do_nothing ():
11791177 pass
11801178 fh .close = do_nothing
@@ -1183,7 +1181,7 @@ def do_nothing():
11831181
11841182 print_figure_impl (fh )
11851183 else :
1186- with io . open (outfile , 'w' , encoding = 'latin-1' ) as fh :
1184+ with open (outfile , 'w' , encoding = 'latin-1' ) as fh :
11871185 print_figure_impl (fh )
11881186
11891187 def _print_figure_tex (
@@ -1231,7 +1229,7 @@ def write(self, *kl, **kwargs):
12311229
12321230 self ._pswriter = NullWriter ()
12331231 else :
1234- self ._pswriter = io . StringIO ()
1232+ self ._pswriter = StringIO ()
12351233
12361234 # mixed mode rendering
12371235 ps_renderer = self ._renderer_class (width , height ,
@@ -1259,7 +1257,7 @@ def write(self, *kl, **kwargs):
12591257
12601258 fd , tmpfile = mkstemp ()
12611259 try :
1262- with io . open (fd , 'w' , encoding = 'latin-1' ) as fh :
1260+ with open (fd , 'w' , encoding = 'latin-1' ) as fh :
12631261 # write the Encapsulated PostScript headers
12641262 print ("%!PS-Adobe-3.0 EPSF-3.0" , file = fh )
12651263 if title :
@@ -1399,17 +1397,13 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
13991397 paperWidth , paperHeight ,
14001398 '\n ' .join (psfrags ), angle , os .path .split (epsfile )[- 1 ])
14011399
1402- with io .open (latexfile , 'wb' ) as latexh :
1403- if rcParams ['text.latex.unicode' ]:
1404- latexh .write (s .encode ('utf8' ))
1405- else :
1406- try :
1407- latexh .write (s .encode ('ascii' ))
1408- except UnicodeEncodeError :
1409- _log .info ("You are using unicode and latex, but have "
1410- "not enabled the matplotlib 'text.latex.unicode' "
1411- "rcParam." )
1412- raise
1400+ try :
1401+ pathlib .Path (latexfile ).write_text (
1402+ s , encoding = 'utf-8' if rcParams ['text.latex.unicode' ] else 'ascii' )
1403+ except UnicodeEncodeError :
1404+ _log .info ("You are using unicode and latex, but have not enabled the "
1405+ "Matplotlib 'text.latex.unicode' rcParam." )
1406+ raise
14131407
14141408 # Replace \\ for / so latex does not think there is a function call
14151409 latexfile = latexfile .replace ("\\ " , "/" )
@@ -1454,7 +1448,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
14541448 # the generated ps file is in landscape and return this
14551449 # information. The return value is used in pstoeps step to recover
14561450 # the correct bounding box. 2010-06-05 JJL
1457- with io . open (tmpfile ) as fh :
1451+ with open (tmpfile ) as fh :
14581452 if "Landscape" in fh .read (1000 ):
14591453 psfrag_rotated = True
14601454 else :
@@ -1648,7 +1642,7 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
16481642 bbox_info , rotate = None , None
16491643
16501644 epsfile = tmpfile + '.eps'
1651- with io . open (epsfile , 'wb' ) as epsh , io . open (tmpfile , 'rb' ) as tmph :
1645+ with open (epsfile , 'wb' ) as epsh , open (tmpfile , 'rb' ) as tmph :
16521646 write = epsh .write
16531647 # Modify the header:
16541648 for line in tmph :
0 commit comments