Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18ff095

Browse files
committed
Pathlibify font_manager (only internally, doesn't change the API).
1 parent 2099af4 commit 18ff095

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,7 @@ def dedent(s):
10231023
return result
10241024

10251025

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

lib/matplotlib/font_manager.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@
4646

4747
from collections import Iterable
4848
import json
49+
import logging
4950
import os
51+
from pathlib import Path
5052
import sys
5153
from threading import Timer
5254
import warnings
53-
import logging
5455

5556
from matplotlib import afm, cbook, ft2font, rcParams, get_cachedir
5657
from matplotlib.compat import subprocess
@@ -146,13 +147,8 @@
146147
]
147148

148149
if not USE_FONTCONFIG and sys.platform != 'win32':
149-
home = os.environ.get('HOME')
150-
if home is not None:
151-
# user fonts on OSX
152-
path = os.path.join(home, 'Library', 'Fonts')
153-
OSXFontDirectories.append(path)
154-
path = os.path.join(home, '.fonts')
155-
X11FontDirectories.append(path)
150+
OSXFontDirectories.append(str(Path.home() / "Library/Fonts"))
151+
X11FontDirectories.append(str(Path.home() / ".fonts"))
156152

157153

158154
def get_fontext_synonyms(fontext):
@@ -168,19 +164,20 @@ def get_fontext_synonyms(fontext):
168164
def list_fonts(directory, extensions):
169165
"""
170166
Return a list of all fonts matching any of the extensions,
171-
possibly upper-cased, found recursively under the directory.
167+
found recursively under the directory.
172168
"""
173-
pattern = ';'.join(['*.%s;*.%s' % (ext, ext.upper())
174-
for ext in extensions])
175-
return cbook.listFiles(directory, pattern)
169+
return [str(path)
170+
for ext in extensions
171+
for path in filter(Path.is_file,
172+
Path(directory).rglob("*.{}".format(ext)))]
176173

177174

178175
def win32FontDirectory():
179-
"""
176+
r"""
180177
Return the user-specified font directory for Win32. This is
181178
looked up from the registry key::
182179
183-
\\\\HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Fonts
180+
\\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
184181
185182
If the key is not found, $WINDIR/Fonts will be returned.
186183
"""
@@ -234,16 +231,10 @@ def win32InstalledFonts(directory=None, fontext='ttf'):
234231
# Work around for https://bugs.python.org/issue25778, which
235232
# is fixed in Py>=3.6.1.
236233
direc = direc.split("\0", 1)[0]
237-
if not os.path.dirname(direc):
238-
direc = os.path.join(directory, direc)
239-
direc = os.path.abspath(direc).lower()
240-
if os.path.splitext(direc)[1][1:] in fontext:
241-
items.add(direc)
242-
except EnvironmentError:
243-
continue
244-
except WindowsError:
245-
continue
246-
except MemoryError:
234+
path = Path(directory, direc).resolve()
235+
if path.suffix.lower() in fontext:
236+
items.add(str(path))
237+
except (OSError, MemoryError):
247238
continue
248239
return list(items)
249240
finally:
@@ -263,6 +254,9 @@ def OSXInstalledFonts(directories=None, fontext='ttf'):
263254
files = []
264255
for path in directories:
265256
if fontext is None:
257+
cbook.warn_deprecated(
258+
"3.0", "Support for listing all files regardless of extension "
259+
"is deprecated.")
266260
files.extend(cbook.listFiles(path, '*'))
267261
else:
268262
files.extend(list_fonts(path, fontext))
@@ -299,7 +293,7 @@ def get_fontconfig_fonts(fontext='ttf'):
299293
"""
300294
fontext = get_fontext_synonyms(fontext)
301295
return [fname for fname in _call_fc_list()
302-
if os.path.splitext(fname)[1][1:] in fontext]
296+
if Path(fname).suffix in fontext]
303297

304298

305299
def findSystemFonts(fontpaths=None, fontext='ttf'):
@@ -320,8 +314,7 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
320314
fontpaths = [fontdir]
321315
# now get all installed fonts directly...
322316
for f in win32InstalledFonts(fontdir):
323-
base, ext = os.path.splitext(f)
324-
if len(ext)>1 and ext[1:].lower() in fontexts:
317+
if Path(f).suffix in fontexts:
325318
fontfiles.add(f)
326319
else:
327320
fontpaths = X11FontDirectories
@@ -1290,16 +1283,12 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
12901283
else:
12911284
fontlist = self.ttflist
12921285

1293-
if directory is not None:
1294-
directory = os.path.normcase(directory)
1295-
12961286
best_score = 1e64
12971287
best_font = None
12981288

12991289
for font in fontlist:
13001290
if (directory is not None and
1301-
os.path.commonprefix([os.path.normcase(font.fname),
1302-
directory]) != directory):
1291+
Path(directory) not in Path(font.fname).parents):
13031292
continue
13041293
# Matching family should have highest priority, so it is multiplied
13051294
# by 10.0

0 commit comments

Comments
 (0)