5
5
from __future__ import (absolute_import , division , print_function ,
6
6
unicode_literals )
7
7
8
- import six
9
-
10
- import hashlib
11
8
import os
12
- import shutil
13
9
14
10
import numpy as np
15
11
16
12
import matplotlib
17
13
from matplotlib .compat import subprocess
18
14
from matplotlib .testing .exceptions import ImageComparisonFailure
19
15
from matplotlib import _png
20
- from matplotlib import _get_cachedir
21
- from matplotlib import cbook
22
- from distutils import version
23
16
24
17
__all__ = ['compare_float' , 'compare_images' , 'comparable_formats' ]
25
18
@@ -76,40 +69,6 @@ def compare_float(expected, actual, relTol=None, absTol=None):
76
69
return msg or None
77
70
78
71
79
- def get_cache_dir ():
80
- cachedir = _get_cachedir ()
81
- if cachedir is None :
82
- raise RuntimeError ('Could not find a suitable configuration directory' )
83
- cache_dir = os .path .join (cachedir , 'test_cache' )
84
- if not os .path .exists (cache_dir ):
85
- try :
86
- cbook .mkdirs (cache_dir )
87
- except IOError :
88
- return None
89
- if not os .access (cache_dir , os .W_OK ):
90
- return None
91
- return cache_dir
92
-
93
-
94
- def get_file_hash (path , block_size = 2 ** 20 ):
95
- md5 = hashlib .md5 ()
96
- with open (path , 'rb' ) as fd :
97
- while True :
98
- data = fd .read (block_size )
99
- if not data :
100
- break
101
- md5 .update (data )
102
-
103
- if path .endswith ('.pdf' ):
104
- from matplotlib import checkdep_ghostscript
105
- md5 .update (checkdep_ghostscript ()[1 ].encode ('utf-8' ))
106
- elif path .endswith ('.svg' ):
107
- from matplotlib import checkdep_inkscape
108
- md5 .update (checkdep_inkscape ().encode ('utf-8' ))
109
-
110
- return md5 .hexdigest ()
111
-
112
-
113
72
def make_external_conversion_command (cmd ):
114
73
def convert (old , new ):
115
74
cmdline = cmd (old , new )
@@ -160,16 +119,20 @@ def comparable_formats():
160
119
return ['png' ] + list (converter )
161
120
162
121
163
- def convert (filename , cache ):
122
+ def convert (filename , cache = None ):
164
123
"""
165
124
Convert the named file into a png file. Returns the name of the
166
125
created file.
167
126
168
- If *cache* is True, the result of the conversion is cached in
169
- `matplotlib._get_cachedir() + '/test_cache/'`. The caching is based
170
- on a hash of the exact contents of the input file. The is no limit
171
- on the size of the cache, so it may need to be manually cleared
172
- periodically.
127
+ Parameters
128
+ ----------
129
+ filename : str
130
+ cache : ConversionCache, optional
131
+
132
+ Returns
133
+ -------
134
+ str
135
+ The converted file.
173
136
174
137
"""
175
138
base , extension = filename .rsplit ('.' , 1 )
@@ -184,23 +147,12 @@ def convert(filename, cache):
184
147
# is out of date.
185
148
if (not os .path .exists (newname ) or
186
149
os .stat (newname ).st_mtime < os .stat (filename ).st_mtime ):
187
- if cache :
188
- cache_dir = get_cache_dir ()
189
- else :
190
- cache_dir = None
191
-
192
- if cache_dir is not None :
193
- hash_value = get_file_hash (filename )
194
- new_ext = os .path .splitext (newname )[1 ]
195
- cached_file = os .path .join (cache_dir , hash_value + new_ext )
196
- if os .path .exists (cached_file ):
197
- shutil .copyfile (cached_file , newname )
198
- return newname
199
-
150
+ in_cache = cache and cache .get (filename , newname )
151
+ if in_cache :
152
+ return newname
200
153
converter [extension ](filename , newname )
201
-
202
- if cache_dir is not None :
203
- shutil .copyfile (newname , cached_file )
154
+ if cache :
155
+ cache .put (filename , newname )
204
156
205
157
return newname
206
158
@@ -262,7 +214,7 @@ def calculate_rms(expectedImage, actualImage):
262
214
return rms
263
215
264
216
265
- def compare_images (expected , actual , tol , in_decorator = False ):
217
+ def compare_images (expected , actual , tol , in_decorator = False , cache = None ):
266
218
"""
267
219
Compare two "image" files checking differences within a tolerance.
268
220
@@ -283,6 +235,7 @@ def compare_images(expected, actual, tol, in_decorator=False):
283
235
in_decorator : bool
284
236
If called from image_comparison decorator, this should be
285
237
True. (default=False)
238
+ cache : cache.ConversionCache, optional
286
239
287
240
Example
288
241
-------
@@ -308,8 +261,8 @@ def compare_images(expected, actual, tol, in_decorator=False):
308
261
raise IOError ('Baseline image %r does not exist.' % expected )
309
262
310
263
if extension != 'png' :
311
- actual = convert (actual , False )
312
- expected = convert (expected , True )
264
+ actual = convert (actual , cache )
265
+ expected = convert (expected , cache )
313
266
314
267
# open the image files and remove the alpha channel (if it exists)
315
268
expectedImage = _png .read_png_int (expected )
0 commit comments