Skip to content

Commit 5691383

Browse files
committed
Use as_file context correctly
1 parent d289f33 commit 5691383

4 files changed

Lines changed: 50 additions & 60 deletions

File tree

Lib/fontbakery/checks/vendorspecific/googlefonts/conditions.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,19 @@ def rfn_exception(font):
307307
been published previously with an RFN, or fonts which benefit from
308308
an agreement with Google Fonts.
309309
"""
310-
from fontbakery.utils import get_resource_file_path
310+
from importlib.resources import as_file, files
311311

312312
rfn_exceptions_txt = "data/googlefonts/reserved_font_name_exceptions.txt"
313-
filename = get_resource_file_path(rfn_exceptions_txt)
314-
for exception in open(filename, "r", encoding="utf-8").readlines():
315-
exception = exception.split("#")[0].strip()
316-
exception = exception.replace(" ", "")
317-
if exception == "":
318-
continue
319-
320-
if exception in font.familyname:
321-
return True
313+
with as_file(files("fontbakery").joinpath(rfn_exceptions_txt)) as filename:
314+
with open(filename, "r", encoding="utf-8") as f:
315+
for exception in f.readlines():
316+
exception = exception.split("#")[0].strip()
317+
exception = exception.replace(" ", "")
318+
if exception == "":
319+
continue
320+
321+
if exception in font.familyname:
322+
return True
322323

323324

324325
@condition(Font)

Lib/fontbakery/checks/vendorspecific/googlefonts/family_name_compliance.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
def check_family_name_compliance(ttFont):
2626
"""Check family name for GF Guide compliance."""
2727
import re
28+
from importlib.resources import as_file, files
2829

29-
from fontbakery.utils import get_name_entries, get_resource_file_path
30+
from fontbakery.utils import get_name_entries
3031

3132
camelcase_exceptions_txt = "data/googlefonts/camelcased_familyname_exceptions.txt"
3233
abbreviations_exceptions_txt = (
@@ -45,18 +46,21 @@ def check_family_name_compliance(ttFont):
4546
known_exception = False
4647

4748
# Process exceptions
48-
filename = get_resource_file_path(camelcase_exceptions_txt)
49-
for exception in open(filename, "r", encoding="utf-8").readlines():
50-
exception = exception.split("#")[0].strip()
51-
if exception == "":
52-
continue
53-
if exception in family_name:
54-
known_exception = True
55-
yield PASS, Message(
56-
"known-camelcase-exception",
57-
"Family name is a known exception to the CamelCase rule.",
58-
)
59-
break
49+
with as_file(
50+
files("fontbakery").joinpath(camelcase_exceptions_txt)
51+
) as filename:
52+
with open(filename, "r", encoding="utf-8") as f:
53+
for exception in f.readlines():
54+
exception = exception.split("#")[0].strip()
55+
if exception == "":
56+
continue
57+
if exception in family_name:
58+
known_exception = True
59+
yield PASS, Message(
60+
"known-camelcase-exception",
61+
"Family name is a known exception to the CamelCase rule.",
62+
)
63+
break
6064

6165
if not known_exception:
6266
yield FAIL, Message(
@@ -71,18 +75,21 @@ def check_family_name_compliance(ttFont):
7175
known_exception = False
7276

7377
# Process exceptions
74-
filename = get_resource_file_path(abbreviations_exceptions_txt)
75-
for exception in open(filename, "r", encoding="utf-8").readlines():
76-
exception = exception.split("#")[0].strip()
77-
if exception == "":
78-
continue
79-
if exception in family_name:
80-
known_exception = True
81-
yield PASS, Message(
82-
"known-abbreviation-exception",
83-
"Family name is a known exception to the abbreviation rule.",
84-
)
85-
break
78+
with as_file(
79+
files("fontbakery").joinpath(abbreviations_exceptions_txt)
80+
) as filename:
81+
with open(filename, "r", encoding="utf-8") as f:
82+
for exception in f.readlines():
83+
exception = exception.split("#")[0].strip()
84+
if exception == "":
85+
continue
86+
if exception in family_name:
87+
known_exception = True
88+
yield PASS, Message(
89+
"known-abbreviation-exception",
90+
"Family name is a known exception to the abbreviation rule.",
91+
)
92+
break
8693

8794
if not known_exception:
8895
# Allow SC ending

Lib/fontbakery/checks/vendorspecific/googlefonts/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
from functools import lru_cache
3+
from importlib.resources import as_file, files
34

4-
from fontbakery.utils import exit_with_install_instructions, get_resource_file_path
55

66

77
@lru_cache(maxsize=1)
@@ -21,8 +21,11 @@ def registered_vendor_ids():
2121
exit_with_install_instructions("googlefonts")
2222

2323
registered_vendor_ids = {}
24-
CACHED = get_resource_file_path("data/fontbakery-microsoft-vendorlist.cache")
25-
content = open(CACHED, encoding="utf-8").read()
24+
with as_file(
25+
files("fontbakery").joinpath("data/fontbakery-microsoft-vendorlist.cache")
26+
) as CACHED:
27+
with open(CACHED, encoding="utf-8") as f:
28+
content = f.read()
2629
# Strip all <A> HTML tags from the raw HTML. The current page contains a
2730
# closing </A> for which no opening <A> is present, which causes
2831
# beautifulsoup to silently stop processing that section from the error

Lib/fontbakery/utils.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import sys
2121
import traceback
2222
from copy import deepcopy
23-
from typing import TYPE_CHECKING, Optional
23+
from typing import Optional
2424

2525
from fontTools.pens.basePen import BasePen
2626
from fontTools.ttLib import TTFont
@@ -33,27 +33,6 @@
3333
PANOSE_Family_Type,
3434
)
3535

36-
if TYPE_CHECKING:
37-
from pathlib import Path
38-
39-
40-
def get_resource_file_path(sub_file_path: str) -> Path:
41-
"""Return the full file path of a resource file inside the fontbakery
42-
directory.
43-
44-
Args:
45-
sub_file_path (str): The file path relative to the `fontbakery`
46-
directory.
47-
48-
Returns:
49-
Path: The full file path
50-
"""
51-
from importlib.resources import as_file, files
52-
53-
with as_file(files("fontbakery").joinpath(sub_file_path)) as path:
54-
file_path = path
55-
return file_path
56-
5736

5837
def exit_with_install_instructions(profile_name):
5938
sys.exit(

0 commit comments

Comments
 (0)