From 66348a9259bc034d0f6b21cfb554f8b2f0af94b0 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 23 Mar 2018 00:27:08 -0700 Subject: [PATCH] Rely on rglob support rather than os.walk. In OSXInstalledFonts, the `fontext is None` path could never be taken, because `get_fontext_synonyms` never returns None. --- .../2018-02-15-AL-deprecations.rst | 2 +- doc/api/next_api_changes/2018-03-23-AL.rst | 4 +++ lib/matplotlib/__init__.py | 11 +++---- lib/matplotlib/cbook/__init__.py | 1 + lib/matplotlib/font_manager.py | 30 ++++++++----------- tools/triage_tests.py | 14 +++------ 6 files changed, 26 insertions(+), 36 deletions(-) create mode 100644 doc/api/next_api_changes/2018-03-23-AL.rst diff --git a/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst b/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst index 4d48437e0988..15ab3de234da 100644 --- a/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst +++ b/doc/api/next_api_changes/2018-02-15-AL-deprecations.rst @@ -13,7 +13,7 @@ The following classes, methods, functions, and attributes are deprecated: - ``Annotation.arrow``, - ``cbook.GetRealpathAndStat``, ``cbook.Locked``, - ``cbook.is_numlike`` (use ``isinstance(..., numbers.Number)`` instead), - ``cbook.unicode_safe`` + ``cbook.listFiles``, ``cbook.unicode_safe`` - ``container.Container.set_remove_method``, - ``dates.DateFormatter.strftime_pre_1900``, ``dates.DateFormatter.strftime``, - ``font_manager.TempCache``, diff --git a/doc/api/next_api_changes/2018-03-23-AL.rst b/doc/api/next_api_changes/2018-03-23-AL.rst new file mode 100644 index 000000000000..7a5f87f590d7 --- /dev/null +++ b/doc/api/next_api_changes/2018-03-23-AL.rst @@ -0,0 +1,4 @@ +``font_manager.list_fonts`` now follows the platform's casefolding semantics +```````````````````````````````````````````````````````````````````````````` + +i.e., it behaves case-insensitively on Windows only. diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index b2186d1ef762..b94df1ec6ca9 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -743,14 +743,11 @@ def _get_data_path_cached(): def get_py2exe_datafiles(): - datapath = get_data_path() - _, tail = os.path.split(datapath) + data_path = Path(get_data_path()) d = {} - for root, _, files in os.walk(datapath): - files = [os.path.join(root, filename) for filename in files] - root = root.replace(tail, 'mpl-data') - root = root[root.index('mpl-data'):] - d[root] = files + for path in filter(Path.is_file, data_path.glob("**/*")): + (d.setdefault(str(path.parent.relative_to(data_path.parent)), []) + .append(str(path))) return list(d.items()) diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 87cfe7fd4d84..85b5baf9a464 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -676,6 +676,7 @@ def dedent(s): return result +@deprecated("3.0") def listFiles(root, patterns='*', recurse=1, return_folders=0): """ Recursively list files diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 02cbd9633fcd..c6c8e3be587f 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -35,6 +35,7 @@ from functools import lru_cache import json import os +from pathlib import Path import subprocess import sys from threading import Timer @@ -150,12 +151,13 @@ def get_fontext_synonyms(fontext): def list_fonts(directory, extensions): """ - Return a list of all fonts matching any of the extensions, - possibly upper-cased, found recursively under the directory. + Return a list of all fonts matching any of the extensions, found + recursively under the directory. """ - pattern = ';'.join(['*.%s;*.%s' % (ext, ext.upper()) - for ext in extensions]) - return cbook.listFiles(directory, pattern) + extensions = ["." + ext for ext in extensions] + return [str(path) + for path in filter(Path.is_file, Path(directory).glob("**/*.*")) + if path.suffix in extensions] def win32FontDirectory(): @@ -231,21 +233,13 @@ def win32InstalledFonts(directory=None, fontext='ttf'): def OSXInstalledFonts(directories=None, fontext='ttf'): - """ - Get list of font files on OS X - ignores font suffix by default. - """ + """Get list of font files on OS X.""" if directories is None: directories = OSXFontDirectories - - fontext = get_fontext_synonyms(fontext) - - files = [] - for path in directories: - if fontext is None: - files.extend(cbook.listFiles(path, '*')) - else: - files.extend(list_fonts(path, fontext)) - return files + return [path + for directory in directories + for ext in get_fontext_synonyms(fontext) + for path in list_fonts(directory, ext)] @lru_cache() diff --git a/tools/triage_tests.py b/tools/triage_tests.py index 0c0f4528a893..c8c60f8142b2 100644 --- a/tools/triage_tests.py +++ b/tools/triage_tests.py @@ -329,16 +329,10 @@ def find_failing_tests(result_images, source): Find all of the failing tests by looking for files with `-failed-diff` at the end of the basename. """ - entries = [] - for root, dirs, files in os.walk(result_images): - for fname in files: - basename, ext = os.path.splitext(fname) - if basename.endswith('-failed-diff'): - path = os.path.join(root, fname) - entry = Entry(path, result_images, source) - entries.append(entry) - entries.sort(key=lambda x: x.name) - return entries + return sorted( + (Entry(path, result_images, source) + for path in Path(result_images).glob("**/*-failed-diff.*")), + key=lambda x: x.name) def launch(result_images, source):