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
23
19
from matplotlib .backends .backend_mixed import MixedModeRenderer
24
20
from matplotlib .cbook import is_writable_file_like
25
21
from matplotlib .compat import subprocess
26
- from matplotlib .compat .subprocess import check_output
27
22
from matplotlib .path import Path
28
23
24
+ _log = logging .getLogger (__name__ )
25
+
29
26
30
27
###############################################################################
31
28
42
39
# assuming fontconfig is installed and the command 'fc-list' exists
43
40
try :
44
41
# list scalable (non-bitmap) fonts
45
- fc_list = check_output ([str ('fc-list' ), ':outline,scalable' , 'family' ])
42
+ fc_list = subprocess .check_output (
43
+ ['fc-list' , ':outline,scalable' , 'family' ])
46
44
fc_list = fc_list .decode ('utf8' )
47
45
system_fonts = [f .split (',' )[0 ] for f in fc_list .splitlines ()]
48
46
system_fonts = list (set (system_fonts ))
@@ -173,7 +171,7 @@ def make_pdf_to_png_converter():
173
171
tools_available = []
174
172
# check for pdftocairo
175
173
try :
176
- check_output ([str ( "pdftocairo" ) , "-v" ], stderr = subprocess .STDOUT )
174
+ subprocess . check_output (["pdftocairo" , "-v" ], stderr = subprocess .STDOUT )
177
175
tools_available .append ("pdftocairo" )
178
176
except :
179
177
pass
@@ -185,31 +183,31 @@ def make_pdf_to_png_converter():
185
183
# pick converter
186
184
if "pdftocairo" in tools_available :
187
185
def cairo_convert (pdffile , pngfile , dpi ):
188
- cmd = [str ( "pdftocairo" ) , "-singlefile" , "-png" , "-r" , "%d" % dpi ,
186
+ cmd = ["pdftocairo" , "-singlefile" , "-png" , "-r" , "%d" % dpi ,
189
187
pdffile , os .path .splitext (pngfile )[0 ]]
190
- check_output (cmd , stderr = subprocess .STDOUT )
188
+ subprocess . check_output (cmd , stderr = subprocess .STDOUT )
191
189
return cairo_convert
192
190
elif "gs" in tools_available :
193
191
def gs_convert (pdffile , pngfile , dpi ):
194
- cmd = [str ( gs ) ,
192
+ cmd = [gs ,
195
193
'-dQUIET' , '-dSAFER' , '-dBATCH' , '-dNOPAUSE' , '-dNOPROMPT' ,
196
194
'-dUseCIEColor' , '-dTextAlphaBits=4' ,
197
195
'-dGraphicsAlphaBits=4' , '-dDOINTERPOLATE' ,
198
196
'-sDEVICE=png16m' , '-sOutputFile=%s' % pngfile ,
199
197
'-r%d' % dpi , pdffile ]
200
- check_output (cmd , stderr = subprocess .STDOUT )
198
+ subprocess . check_output (cmd , stderr = subprocess .STDOUT )
201
199
return gs_convert
202
200
else :
203
201
raise RuntimeError ("No suitable pdf to png renderer found." )
204
202
205
203
206
204
class LatexError (Exception ):
207
205
def __init__ (self , message , latex_output = "" ):
208
- Exception .__init__ (self , message )
206
+ super () .__init__ (message )
209
207
self .latex_output = latex_output
210
208
211
209
212
- class LatexManagerFactory ( object ) :
210
+ class LatexManagerFactory :
213
211
previous_instance = None
214
212
215
213
@staticmethod
@@ -221,18 +219,16 @@ def get_latex_manager():
221
219
# Check if the previous instance of LatexManager can be reused.
222
220
if (prev and prev .latex_header == latex_header
223
221
and prev .texcommand == texcommand ):
224
- if rcParams ["pgf.debug" ]:
225
- print ("reusing LatexManager" )
222
+ _log .debug ("reusing LatexManager" )
226
223
return prev
227
224
else :
228
- if rcParams ["pgf.debug" ]:
229
- print ("creating LatexManager" )
225
+ _log .debug ("creating LatexManager" )
230
226
new_inst = LatexManager ()
231
227
LatexManagerFactory .previous_instance = new_inst
232
228
return new_inst
233
229
234
230
235
- class LatexManager ( object ) :
231
+ class LatexManager :
236
232
"""
237
233
The LatexManager opens an instance of the LaTeX application for
238
234
determining the metrics of text elements. The LaTeX environment can be
@@ -285,7 +281,6 @@ def __init__(self):
285
281
# store references for __del__
286
282
self ._os_path = os .path
287
283
self ._shutil = shutil
288
- self ._debug = rcParams ["pgf.debug" ]
289
284
290
285
# create a tmp directory for running latex, remember to cleanup
291
286
self .tmpdir = tempfile .mkdtemp (prefix = "mpl_pgf_lm_" )
@@ -296,26 +291,24 @@ def __init__(self):
296
291
self .latex_header = LatexManager ._build_latex_header ()
297
292
latex_end = "\n \\ makeatletter\n \\ @@end\n "
298
293
try :
299
- latex = subprocess .Popen ([str ( self .texcommand ) , "-halt-on-error" ],
294
+ latex = subprocess .Popen ([self .texcommand , "-halt-on-error" ],
300
295
stdin = subprocess .PIPE ,
301
296
stdout = subprocess .PIPE ,
302
297
cwd = self .tmpdir )
303
- except OSError as e :
304
- if e .errno == errno .ENOENT :
305
- raise RuntimeError (
306
- "Latex command not found. Install %r or change "
307
- "pgf.texsystem to the desired command." % self .texcommand )
308
- else :
309
- raise RuntimeError (
310
- "Error starting process %r" % self .texcommand )
298
+ except FileNotFoundError :
299
+ raise RuntimeError (
300
+ "Latex command not found. Install %r or change "
301
+ "pgf.texsystem to the desired command." % self .texcommand )
302
+ except OSError :
303
+ raise RuntimeError ("Error starting process %r" % self .texcommand )
311
304
test_input = self .latex_header + latex_end
312
305
stdout , stderr = latex .communicate (test_input .encode ("utf-8" ))
313
306
if latex .returncode != 0 :
314
307
raise LatexError ("LaTeX returned an error, probably missing font "
315
308
"or error in preamble:\n %s" % stdout )
316
309
317
310
# open LaTeX process for real work
318
- latex = subprocess .Popen ([str ( self .texcommand ) , "-halt-on-error" ],
311
+ latex = subprocess .Popen ([self .texcommand , "-halt-on-error" ],
319
312
stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
320
313
cwd = self .tmpdir )
321
314
self .latex = latex
@@ -345,8 +338,7 @@ def _cleanup(self):
345
338
sys .stderr .write ("error deleting tmp directory %s\n " % self .tmpdir )
346
339
347
340
def __del__ (self ):
348
- if self ._debug :
349
- print ("deleting LatexManager" )
341
+ _log .debug ("deleting LatexManager" )
350
342
self ._cleanup ()
351
343
352
344
def get_width_height_descent (self , text , prop ):
@@ -766,7 +758,7 @@ class GraphicsContextPgf(GraphicsContextBase):
766
758
########################################################################
767
759
768
760
769
- class TmpDirCleaner ( object ) :
761
+ class TmpDirCleaner :
770
762
remaining_tmpdirs = set ()
771
763
772
764
@staticmethod
@@ -776,10 +768,10 @@ def add(tmpdir):
776
768
@staticmethod
777
769
def cleanup_remaining_tmpdirs ():
778
770
for tmpdir in TmpDirCleaner .remaining_tmpdirs :
779
- try :
780
- shutil . rmtree ( tmpdir )
781
- except :
782
- sys . stderr . write ( "error deleting tmp directory %s \n " % tmpdir )
771
+ shutil . rmtree (
772
+ tmpdir ,
773
+ onerror = lambda * args : print ( "error deleting tmp directory %s"
774
+ % tmpdir , file = sys . stderr ) )
783
775
784
776
785
777
class FigureCanvasPgf (FigureCanvasBase ):
@@ -858,7 +850,7 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
858
850
return
859
851
860
852
# figure out where the pgf is to be written to
861
- if isinstance (fname_or_fh , six . string_types ):
853
+ if isinstance (fname_or_fh , str ):
862
854
with codecs .open (fname_or_fh , "w" , encoding = "utf-8" ) as fh :
863
855
self ._print_pgf_to_fh (fh , * args , ** kwargs )
864
856
elif is_writable_file_like (fname_or_fh ):
@@ -897,10 +889,11 @@ def _print_pdf_to_fh(self, fh, *args, **kwargs):
897
889
fh_tex .write (latexcode )
898
890
899
891
texcommand = get_texcommand ()
900
- cmdargs = [str ( texcommand ) , "-interaction=nonstopmode" ,
892
+ cmdargs = [texcommand , "-interaction=nonstopmode" ,
901
893
"-halt-on-error" , "figure.tex" ]
902
894
try :
903
- check_output (cmdargs , stderr = subprocess .STDOUT , cwd = tmpdir )
895
+ subprocess .check_output (
896
+ cmdargs , stderr = subprocess .STDOUT , cwd = tmpdir )
904
897
except subprocess .CalledProcessError as e :
905
898
raise RuntimeError (
906
899
"%s was not able to process your file.\n \n Full log:\n %s"
@@ -924,7 +917,7 @@ def print_pdf(self, fname_or_fh, *args, **kwargs):
924
917
return
925
918
926
919
# figure out where the pdf is to be written to
927
- if isinstance (fname_or_fh , six . string_types ):
920
+ if isinstance (fname_or_fh , str ):
928
921
with open (fname_or_fh , "wb" ) as fh :
929
922
self ._print_pdf_to_fh (fh , * args , ** kwargs )
930
923
elif is_writable_file_like (fname_or_fh ):
@@ -960,7 +953,7 @@ def print_png(self, fname_or_fh, *args, **kwargs):
960
953
self ._print_pgf_to_fh (None , * args , ** kwargs )
961
954
return
962
955
963
- if isinstance (fname_or_fh , six . string_types ):
956
+ if isinstance (fname_or_fh , str ):
964
957
with open (fname_or_fh , "wb" ) as fh :
965
958
self ._print_png_to_fh (fh , * args , ** kwargs )
966
959
elif is_writable_file_like (fname_or_fh ):
@@ -973,8 +966,7 @@ def get_renderer(self):
973
966
974
967
975
968
class FigureManagerPgf (FigureManagerBase ):
976
- def __init__ (self , * args ):
977
- FigureManagerBase .__init__ (self , * args )
969
+ pass
978
970
979
971
980
972
@_Backend .export
0 commit comments