1
- from __future__ import (absolute_import , division , print_function ,
2
- unicode_literals )
3
-
4
- import six
5
-
6
1
import atexit
7
2
import codecs
8
3
import errno
4
+ import logging
9
5
import math
10
6
import os
11
7
import re
27
23
from matplotlib .figure import Figure
28
24
from matplotlib ._pylab_helpers import Gcf
29
25
26
+ _log = logging .getLogger (__name__ )
27
+
30
28
31
29
###############################################################################
32
30
@@ -193,10 +191,9 @@ def make_pdf_to_png_converter():
193
191
tools_available = []
194
192
# check for pdftocairo
195
193
try :
196
- subprocess .check_output (
197
- ["pdftocairo" , "-v" ], stderr = subprocess .STDOUT )
194
+ subprocess .check_output (["pdftocairo" , "-v" ], stderr = subprocess .STDOUT )
198
195
tools_available .append ("pdftocairo" )
199
- except :
196
+ except OSError :
200
197
pass
201
198
# check for ghostscript
202
199
gs , ver = mpl .checkdep_ghostscript ()
@@ -212,7 +209,7 @@ def cairo_convert(pdffile, pngfile, dpi):
212
209
return cairo_convert
213
210
elif "gs" in tools_available :
214
211
def gs_convert (pdffile , pngfile , dpi ):
215
- cmd = [str ( gs ) ,
212
+ cmd = [gs ,
216
213
'-dQUIET' , '-dSAFER' , '-dBATCH' , '-dNOPAUSE' , '-dNOPROMPT' ,
217
214
'-dUseCIEColor' , '-dTextAlphaBits=4' ,
218
215
'-dGraphicsAlphaBits=4' , '-dDOINTERPOLATE' ,
@@ -226,11 +223,11 @@ def gs_convert(pdffile, pngfile, dpi):
226
223
227
224
class LatexError (Exception ):
228
225
def __init__ (self , message , latex_output = "" ):
229
- Exception .__init__ (self , message )
226
+ super () .__init__ (message )
230
227
self .latex_output = latex_output
231
228
232
229
233
- class LatexManagerFactory ( object ) :
230
+ class LatexManagerFactory :
234
231
previous_instance = None
235
232
236
233
@staticmethod
@@ -242,18 +239,16 @@ def get_latex_manager():
242
239
# Check if the previous instance of LatexManager can be reused.
243
240
if (prev and prev .latex_header == latex_header
244
241
and prev .texcommand == texcommand ):
245
- if rcParams ["pgf.debug" ]:
246
- print ("reusing LatexManager" )
242
+ _log .debug ("reusing LatexManager" )
247
243
return prev
248
244
else :
249
- if rcParams ["pgf.debug" ]:
250
- print ("creating LatexManager" )
245
+ _log .debug ("creating LatexManager" )
251
246
new_inst = LatexManager ()
252
247
LatexManagerFactory .previous_instance = new_inst
253
248
return new_inst
254
249
255
250
256
- class LatexManager ( object ) :
251
+ class LatexManager :
257
252
"""
258
253
The LatexManager opens an instance of the LaTeX application for
259
254
determining the metrics of text elements. The LaTeX environment can be
@@ -306,7 +301,6 @@ def __init__(self):
306
301
# store references for __del__
307
302
self ._os_path = os .path
308
303
self ._shutil = shutil
309
- self ._debug = rcParams ["pgf.debug" ]
310
304
311
305
# create a tmp directory for running latex, remember to cleanup
312
306
self .tmpdir = tempfile .mkdtemp (prefix = "mpl_pgf_lm_" )
@@ -317,26 +311,24 @@ def __init__(self):
317
311
self .latex_header = LatexManager ._build_latex_header ()
318
312
latex_end = "\n \\ makeatletter\n \\ @@end\n "
319
313
try :
320
- latex = subprocess .Popen ([str ( self .texcommand ) , "-halt-on-error" ],
314
+ latex = subprocess .Popen ([self .texcommand , "-halt-on-error" ],
321
315
stdin = subprocess .PIPE ,
322
316
stdout = subprocess .PIPE ,
323
317
cwd = self .tmpdir )
324
- except OSError as e :
325
- if e .errno == errno .ENOENT :
326
- raise RuntimeError (
327
- "Latex command not found. Install %r or change "
328
- "pgf.texsystem to the desired command." % self .texcommand )
329
- else :
330
- raise RuntimeError (
331
- "Error starting process %r" % self .texcommand )
318
+ except FileNotFoundError :
319
+ raise RuntimeError (
320
+ "Latex command not found. Install %r or change "
321
+ "pgf.texsystem to the desired command." % self .texcommand )
322
+ except OSError :
323
+ raise RuntimeError ("Error starting process %r" % self .texcommand )
332
324
test_input = self .latex_header + latex_end
333
325
stdout , stderr = latex .communicate (test_input .encode ("utf-8" ))
334
326
if latex .returncode != 0 :
335
327
raise LatexError ("LaTeX returned an error, probably missing font "
336
328
"or error in preamble:\n %s" % stdout )
337
329
338
330
# open LaTeX process for real work
339
- latex = subprocess .Popen ([str ( self .texcommand ) , "-halt-on-error" ],
331
+ latex = subprocess .Popen ([self .texcommand , "-halt-on-error" ],
340
332
stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
341
333
cwd = self .tmpdir )
342
334
self .latex = latex
@@ -366,8 +358,7 @@ def _cleanup(self):
366
358
sys .stderr .write ("error deleting tmp directory %s\n " % self .tmpdir )
367
359
368
360
def __del__ (self ):
369
- if self ._debug :
370
- print ("deleting LatexManager" )
361
+ _log .debug ("deleting LatexManager" )
371
362
self ._cleanup ()
372
363
373
364
def get_width_height_descent (self , text , prop ):
@@ -787,7 +778,7 @@ class GraphicsContextPgf(GraphicsContextBase):
787
778
########################################################################
788
779
789
780
790
- class TmpDirCleaner ( object ) :
781
+ class TmpDirCleaner :
791
782
remaining_tmpdirs = set ()
792
783
793
784
@staticmethod
@@ -797,10 +788,10 @@ def add(tmpdir):
797
788
@staticmethod
798
789
def cleanup_remaining_tmpdirs ():
799
790
for tmpdir in TmpDirCleaner .remaining_tmpdirs :
800
- try :
801
- shutil . rmtree ( tmpdir )
802
- except :
803
- sys . stderr . write ( "error deleting tmp directory %s \n " % tmpdir )
791
+ shutil . rmtree (
792
+ tmpdir ,
793
+ onerror = lambda * args : print ( "error deleting tmp directory %s"
794
+ % tmpdir , file = sys . stderr ) )
804
795
805
796
806
797
class FigureCanvasPgf (FigureCanvasBase ):
@@ -879,7 +870,7 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
879
870
return
880
871
881
872
# figure out where the pgf is to be written to
882
- if isinstance (fname_or_fh , six . string_types ):
873
+ if isinstance (fname_or_fh , str ):
883
874
with codecs .open (fname_or_fh , "w" , encoding = "utf-8" ) as fh :
884
875
self ._print_pgf_to_fh (fh , * args , ** kwargs )
885
876
elif is_writable_file_like (fname_or_fh ):
@@ -918,7 +909,7 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs):
918
909
fh_tex .write (latexcode )
919
910
920
911
texcommand = get_texcommand ()
921
- cmdargs = [str ( texcommand ) , "-interaction=nonstopmode" ,
912
+ cmdargs = [texcommand , "-interaction=nonstopmode" ,
922
913
"-halt-on-error" , "figure.tex" ]
923
914
try :
924
915
subprocess .check_output (
@@ -946,7 +937,7 @@ def print_pdf(self, fname_or_fh, *args, **kwargs):
946
937
return
947
938
948
939
# figure out where the pdf is to be written to
949
- if isinstance (fname_or_fh , six . string_types ):
940
+ if isinstance (fname_or_fh , str ):
950
941
with open (fname_or_fh , "wb" ) as fh :
951
942
self ._print_pdf_to_fh (fh , * args , ** kwargs )
952
943
elif is_writable_file_like (fname_or_fh ):
@@ -982,7 +973,7 @@ def print_png(self, fname_or_fh, *args, **kwargs):
982
973
self ._print_pgf_to_fh (None , * args , ** kwargs )
983
974
return
984
975
985
- if isinstance (fname_or_fh , six . string_types ):
976
+ if isinstance (fname_or_fh , str ):
986
977
with open (fname_or_fh , "wb" ) as fh :
987
978
self ._print_png_to_fh (fh , * args , ** kwargs )
988
979
elif is_writable_file_like (fname_or_fh ):
@@ -995,8 +986,7 @@ def get_renderer(self):
995
986
996
987
997
988
class FigureManagerPgf (FigureManagerBase ):
998
- def __init__ (self , * args ):
999
- FigureManagerBase .__init__ (self , * args )
989
+ pass
1000
990
1001
991
1002
992
@_Backend .export
0 commit comments