Remove import side effect of creating font cache in font_manager#488
Conversation
They take some time to run
- Extract function - Make load function not to modify the global variable - Evaluate cache path in a function - Remove _fmcache definition - Add a test for findfont before changing it - Remove one global - Use default_font_manager in tests
- Rename rebuild_or_load_from_cache -> load_from_cache_or_rebuild to reflect actual order - Expand documentation and comments - Rename functions to make them more obviously private
|
CI is failing while trying to build the EDM environment: it could not access a repository. It is orthogonal but is nonetheless blocking. |
|
Rebuilding CI following the fix in #489 |
|
Ah... I need to merge master in because this requires changes to the CI config... |
Codecov Report
@@ Coverage Diff @@
## master #488 +/- ##
==========================================
+ Coverage 30.51% 30.61% +0.10%
==========================================
Files 206 206
Lines 17853 17865 +12
Branches 2461 2462 +1
==========================================
+ Hits 5447 5469 +22
+ Misses 12068 12062 -6
+ Partials 338 334 -4
Continue to review full report at Codecov.
|
| import copy | ||
| from kiva.constants import (DEFAULT, DECORATIVE, ROMAN, SCRIPT, SWISS, MODERN, | ||
| TELETYPE, NORMAL, ITALIC, BOLD, BOLD_ITALIC) | ||
| from .font_manager import default_font_manager, FontProperties |
There was a problem hiding this comment.
This undoes #368 because the import side effect being avoided is now removed. While this move is strictly speaking unnecessary, it reduces cognitive load for developers: Any imports that are not at the top always beg the question "why are they not at the top".
jwiggins
left a comment
There was a problem hiding this comment.
Tests look good. I can't see anything that this would break but I'm still a bit wary of Hyrum's Law...
| def findfont(prop, **kw): | ||
| global fontManager | ||
| font = fontManager.findfont(prop, **kw) | ||
| font = default_font_manager().findfont(prop, **kw) | ||
| return font |
There was a problem hiding this comment.
This function doesn't appear to be used anywhere outside of the tests. I checked chaco and two proprietary projects...
There was a problem hiding this comment.
Opened #497
(Part of me wonders if this needs a deprecation warning or if we are confident no one uses this enough it can just die.)
There was a problem hiding this comment.
Thanks. I noticed some confusion between findfont and findFont in that issue, but the matter is settled here.
There was a problem hiding this comment.
Well spotted. Will fix the spelling on the issue.
| def test_find_font_some_face_name(self): | ||
| font = Font(face_name="ProbablyNotFound") | ||
|
|
||
| # There will be warnings as there will be no match for the requested | ||
| # face name. | ||
| with self.assertWarns(UserWarning): | ||
| font_file_path = font.findfont() | ||
| self.assertTrue(os.path.exists(font_file_path)) |
There was a problem hiding this comment.
I'm not excited that this is the established behavior but it's good to have it covered with a test before our changes get any more invasive.
The extraction for testability wasn't necessary. It is fine to instantiate the KivaFontEditor
|
Thank you @jwiggins Done switching to use |
Closes #362
This PR removes the import side effect of building font cache in the
font_managermodule. The action of loading fonts and building the cache is deferred until the global font manager singleton is requested. All code external tofont_managershould use thedefault_font_managercallable to access this font manager singleton.Benefits of this change:
font_managerwithout the import side effect. (The global singleton still makes testing harder.)It might be easier to review this PR by commits. As an overview:
default_font_managerfunction and add tests for modules (outside offont_managerthat uses thefontManagersingletonNote:
If downstream code has only ever access the FontManager via
kiva.font.Font.findfont, then this PR does not change the behaviour perceived externally because #368 already defers the import side effect to wherekiva.font.Font.findfontis called.