Skip to content

Commit 0faaded

Browse files
committed
Pathlibify font_manager (only internally, doesn't change the API).
1 parent e8a54b4 commit 0faaded

File tree

3 files changed

+23
-43
lines changed

3 files changed

+23
-43
lines changed

lib/matplotlib/cbook/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ def dedent(s):
10251025
return result
10261026

10271027

1028+
@deprecated("3.0")
10281029
def listFiles(root, patterns='*', recurse=1, return_folders=0):
10291030
"""
10301031
Recursively list files

lib/matplotlib/font_manager.py

+20-32
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@
4747
from collections import Iterable
4848
from functools import lru_cache
4949
import json
50+
import logging
5051
import os
52+
from pathlib import Path
5153
import sys
5254
from threading import Timer
5355
import warnings
54-
import logging
5556

5657
from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
5758
from matplotlib.compat import subprocess
@@ -142,13 +143,8 @@
142143
]
143144

144145
if not USE_FONTCONFIG and sys.platform != 'win32':
145-
home = os.environ.get('HOME')
146-
if home is not None:
147-
# user fonts on OSX
148-
path = os.path.join(home, 'Library', 'Fonts')
149-
OSXFontDirectories.append(path)
150-
path = os.path.join(home, '.fonts')
151-
X11FontDirectories.append(path)
146+
OSXFontDirectories.append(str(Path.home() / "Library/Fonts"))
147+
X11FontDirectories.append(str(Path.home() / ".fonts"))
152148

153149

154150
def get_fontext_synonyms(fontext):
@@ -164,19 +160,19 @@ def get_fontext_synonyms(fontext):
164160
def list_fonts(directory, extensions):
165161
"""
166162
Return a list of all fonts matching any of the extensions,
167-
possibly upper-cased, found recursively under the directory.
163+
found recursively under the directory.
168164
"""
169-
pattern = ';'.join(['*.%s;*.%s' % (ext, ext.upper())
170-
for ext in extensions])
171-
return cbook.listFiles(directory, pattern)
165+
return [str(path)
166+
for path in Path().rglob("*")
167+
if path.is_file() and path.suffix[1:] in extensions]
172168

173169

174170
def win32FontDirectory():
175-
"""
171+
r"""
176172
Return the user-specified font directory for Win32. This is
177173
looked up from the registry key::
178174
179-
\\\\HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Fonts
175+
\\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
180176
181177
If the key is not found, $WINDIR/Fonts will be returned.
182178
"""
@@ -230,16 +226,10 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
230226
# Work around for https://bugs.python.org/issue25778, which
231227
# is fixed in Py>=3.6.1.
232228
direc = direc.split("\0", 1)[0]
233-
if not os.path.dirname(direc):
234-
direc = os.path.join(directory, direc)
235-
direc = os.path.abspath(direc).lower()
236-
if os.path.splitext(direc)[1][1:] in fontext:
237-
items.add(direc)
238-
except EnvironmentError:
239-
continue
240-
except WindowsError:
241-
continue
242-
except MemoryError:
229+
path = Path(directory, direc).resolve()
230+
if path.suffix.lower() in fontext:
231+
items.add(str(path))
232+
except (OSError, MemoryError):
243233
continue
244234
return list(items)
245235
finally:
@@ -259,6 +249,9 @@ def OSXInstalledFonts(directories=None, fontext='ttf'):
259249
files = []
260250
for path in directories:
261251
if fontext is None:
252+
cbook.warn_deprecated(
253+
"3.0", "Support for listing all files regardless of extension "
254+
"is deprecated.")
262255
files.extend(cbook.listFiles(path, '*'))
263256
else:
264257
files.extend(list_fonts(path, fontext))
@@ -295,7 +288,7 @@ def get_fontconfig_fonts(fontext='ttf'):
295288
"""
296289
fontext = get_fontext_synonyms(fontext)
297290
return [fname for fname in _call_fc_list()
298-
if os.path.splitext(fname)[1][1:] in fontext]
291+
if Path(fname).suffix[1:] in fontext]
299292

300293

301294
def findSystemFonts(fontpaths=None, fontext='ttf'):
@@ -316,8 +309,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
316309
fontpaths = [fontdir]
317310
# now get all installed fonts directly...
318311
for f in win32InstalledFonts(fontdir):
319-
base, ext = os.path.splitext(f)
320-
if len(ext)>1 and ext[1:].lower() in fontexts:
312+
if Path(f).suffix in fontexts:
321313
fontfiles.add(f)
322314
else:
323315
fontpaths = X11FontDirectories
@@ -1302,16 +1294,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
13021294
else:
13031295
fontlist = self.ttflist
13041296

1305-
if directory is not None:
1306-
directory = os.path.normcase(directory)
1307-
13081297
best_score = 1e64
13091298
best_font = None
13101299

13111300
for font in fontlist:
13121301
if (directory is not None and
1313-
os.path.commonprefix([os.path.normcase(font.fname),
1314-
directory]) != directory):
1302+
Path(directory) not in Path(font.fname).parents):
13151303
continue
13161304
# Matching family should have highest priority, so it is multiplied
13171305
# by 10.0

lib/matplotlib/tests/test_font_manager.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from __future__ import absolute_import, division, print_function
2-
3-
import six
4-
51
import os
2+
import shutil
63
import tempfile
74
import warnings
85

@@ -14,13 +11,7 @@
1411
get_fontconfig_fonts, is_opentype_cff_font, fontManager as fm)
1512
from matplotlib import rc_context
1613

17-
if six.PY2:
18-
from distutils.spawn import find_executable
19-
has_fclist = find_executable('fc-list') is not None
20-
else:
21-
# py >= 3.3
22-
from shutil import which
23-
has_fclist = which('fc-list') is not None
14+
has_fclist = shutil.which('fc-list') is not None
2415

2516

2617
def test_font_priority():

0 commit comments

Comments
 (0)