Skip to content

[MRG+2] MAINT: Refactor the converted-image cache #7764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reinstate deleted functions
With a deprecation decorator
  • Loading branch information
jkseppan committed Feb 20, 2017
commit 33f2528cf9e68b5adcf5f6a94de54f5e07450c25
20 changes: 20 additions & 0 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import matplotlib
from matplotlib import cbook
from matplotlib.compat import subprocess
from matplotlib.testing import conversion_cache as ccache
from matplotlib.testing.exceptions import ImageComparisonFailure
from matplotlib import _png

Expand Down Expand Up @@ -120,6 +121,25 @@ def comparable_formats():
return ['png'] + list(converter)


@cbook.deprecated('2.1', addendum='Use ConversionCache instead')
def get_cache_dir():
return ccache.ConversionCache.get_cache_dir()


@cbook.deprecated('2.1', addendum='Use ConversionCache instead')
def get_file_hash(path, block_size=2 ** 20):
if path.endswith('.pdf'):
from matplotlib import checkdep_ghostscript
version_tag = checkdep_ghostscript()[1].encode('utf-8')
elif path.endswith('.svg'):
from matplotlib import checkdep_inkscape
version_tag = checkdep_inkscape().encode('utf-8')
else:
version_tag = None
return ccache.ConversionCache._get_file_hash_static(
path, block_size, version_tag)


def convert(filename, cache=None):
"""
Convert the named file into a png file. Returns the name of the
Expand Down
29 changes: 18 additions & 11 deletions lib/matplotlib/testing/conversion_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,32 @@ def put(self, original, converted):
def _get_file_hash(self, path, block_size=2 ** 20):
if path in self.hash_cache:
return self.hash_cache[path]
_, ext = os.path.splitext(path)
version_tag = self.converter_version.get(ext)
if version_tag is None:
warnings.warn(
("Don't know the external converter for files with extension "
"%s, cannot ensure cache invalidation on version update.")
% ext)
result = self._get_file_hash_static(path, block_size, version_tag)
self.hash_cache[path] = result
return result

@staticmethod
def _get_file_hash_static(path, block_size, version_tag):
# the parts of _get_file_hash that are called from the deprecated
# compare.get_file_hash; can merge into _get_file_hash once that
# function is removed
md5 = hashlib.md5()
with open(path, 'rb') as fd:
while True:
data = fd.read(block_size)
if not data:
break
md5.update(data)
_, ext = os.path.splitext(path)
version_tag = self.converter_version.get(ext)
if version_tag:
if version_tag is not None:
md5.update(version_tag)
else:
warnings.warn(("Don't know the external converter for %s, cannot "
"ensure cache invalidation on version update.")
% path)

result = md5.hexdigest()
self.hash_cache[path] = result
return result
return md5.hexdigest()

def report(self):
"""Return information about the cache.
Expand Down