Skip to content

Commit 099307d

Browse files
authored
Replace pkg_resources with importlib_resources (#1131)
* Replace pkg_resources with importlib_resources * black * implement Cosimo feedback * work on py3.9 * black * fix py3.9 again. Thanks Cosimo * implement naughty boy feedback. * implement Cosimo feedback again * simplify version import
1 parent 631992e commit 099307d

File tree

6 files changed

+36
-34
lines changed

6 files changed

+36
-34
lines changed

Lib/gftools/scripts/add_font.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
from axisregistry import AxisRegistry
5959
from gfsubsets import SubsetsInFont
6060
from google.protobuf import text_format
61-
from pkg_resources import resource_filename
6261
from gftools.utils import remove_url_prefix, primary_script
6362

6463

Lib/gftools/scripts/lang_support.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
from gftools import fonts_public_pb2
2121
from gftools.util import google_fonts as fonts
2222
from google.protobuf import text_format
23-
from pkg_resources import resource_filename
2423
import csv
2524
import os
26-
from pkg_resources import resource_filename
2725

2826
parser = argparse.ArgumentParser(
2927
description="Add language support metadata to METADATA.pb files"

Lib/gftools/scripts/push_stats.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212
from gftools.push.trafficjam import PushItems
1313
from jinja2 import Environment, FileSystemLoader, select_autoescape
14-
from pkg_resources import resource_filename
14+
from importlib.resources import files, as_file
1515
from datetime import datetime
1616
import pygit2
1717
from github import Github
@@ -76,12 +76,6 @@ def main(args=None):
7676
parser.add_argument("out")
7777
args = parser.parse_args(args)
7878

79-
push_template_dir = resource_filename("gftools", "push-templates")
80-
env = Environment(
81-
loader=FileSystemLoader(push_template_dir),
82-
autoescape=select_autoescape(),
83-
)
84-
8579
data_out = os.path.join(os.path.dirname(args.out), "gf_repo_data.json")
8680
if os.path.exists(data_out):
8781
commit_data = json.load(open(data_out, encoding="utf8"))
@@ -100,22 +94,27 @@ def main(args=None):
10094
prod_path = os.path.join(args.repo_path, "to_production.txt")
10195
prod_families = PushItems.from_server_file(prod_path)
10296

103-
template = env.get_template("index.html")
104-
105-
print("Writing json data")
106-
commit_data = {
107-
"last_run": datetime.now().strftime("%Y-%m-%d"),
108-
"commits": commits,
109-
"pushes": {
110-
"sandbox": [i.to_json() for i in sb_families],
111-
"production": [i.to_json() for i in prod_families],
112-
},
113-
}
114-
json.dump(commit_data, open(data_out, "w", encoding="utf8"), indent=4)
115-
116-
print("Writing report")
117-
with open(args.out, "w") as doc:
118-
doc.write(template.render(commit_data=json.dumps(commit_data)))
97+
with as_file(files("gftools") / "push-templates") as push_template_dir:
98+
env = Environment(
99+
loader=FileSystemLoader(str(push_template_dir)),
100+
autoescape=select_autoescape(),
101+
)
102+
template = env.get_template("index.html")
103+
104+
print("Writing json data")
105+
commit_data = {
106+
"last_run": datetime.now().strftime("%Y-%m-%d"),
107+
"commits": commits,
108+
"pushes": {
109+
"sandbox": [i.to_json() for i in sb_families],
110+
"production": [i.to_json() for i in prod_families],
111+
},
112+
}
113+
json.dump(commit_data, open(data_out, "w", encoding="utf8"), indent=4)
114+
115+
print("Writing report")
116+
with open(args.out, "w") as doc:
117+
doc.write(template.render(commit_data=json.dumps(commit_data)))
119118

120119

121120
if __name__ == "__main__":

Lib/gftools/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from collections import namedtuple
2929
from gflanguages import LoadLanguages
3030
from github import Github
31-
from pkg_resources import resource_filename
31+
import importlib.resources
3232
from google.protobuf import text_format
3333
import json
3434
from PIL import Image
@@ -540,7 +540,8 @@ def font_sample_text(ttFont):
540540
that can be formed using the ttFont instance.
541541
542542
UDHR has been chosen due to the many languages it covers"""
543-
with open(resource_filename("gftools", "udhr_all.txt"), encoding="utf-8") as doc:
543+
ref = importlib.resources.files("gftools") / "udhr_all.txt"
544+
with importlib.resources.as_file(ref) as doc:
544545
uhdr = doc.read()
545546

546547
cmap = set(ttFont.getBestCmap())

bin/gftools

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717
from __future__ import print_function
1818
from argparse import RawTextHelpFormatter
19-
from pkg_resources import get_distribution
19+
from gftools import __version__
2020
from warnings import warn
2121
import sys
2222
import os
@@ -47,7 +47,6 @@ def _get_subcommands():
4747

4848

4949
def print_menu():
50-
__version__ = get_distribution('gftools').version
5150
print(" o-o o o--o")
5251
print("o | | o")
5352
print("| o-o o-o o-o o-o o o-o o-o o-o o-o -o- o-o")
@@ -64,7 +63,6 @@ def print_menu():
6463

6564
subcommands = _get_subcommands()
6665

67-
__version__ = get_distribution('gftools').version
6866

6967
description = "Run gftools subcommands:{0}".format(''.join(
7068
['\n {0}'.format(sc) for sc in sorted(subcommands.keys())]))

tests/push/test_items.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
from pathlib import Path
44

55
from gftools.push.items import Family, FamilyMeta, Designer, Axis, AxisFallback
6-
from pkg_resources import resource_filename
6+
from importlib.resources import files, as_file
77
import json
8+
import atexit
9+
from contextlib import ExitStack
10+
11+
file_manager = ExitStack()
12+
atexit.register(file_manager.close)
813

914

1015
CWD = os.path.dirname(__file__)
1116
TEST_DIR = os.path.join(CWD, "..", "..", "data", "test", "gf_fonts")
1217
SERVER_DIR = os.path.join(CWD, "..", "..", "data", "test", "servers")
1318
TEST_FAMILY_DIR = Path(TEST_DIR) / "ofl" / "mavenpro"
1419
DESIGNER_DIR = Path(TEST_DIR) / "joeprince"
15-
AXES_DIR = Path(resource_filename("axisregistry", "data"))
20+
WEIGHT_AXIS = file_manager.enter_context(
21+
as_file(files("axisregistry") / "data" / "weight.textproto")
22+
)
1623
FAMILY_JSON = json.load(open(os.path.join(SERVER_DIR, "family.json"), encoding="utf8"))
1724
FONTS_JSON = json.load(open(os.path.join(SERVER_DIR, "fonts.json"), encoding="utf8"))
1825

@@ -68,7 +75,7 @@
6875
),
6976
(
7077
Axis,
71-
AXES_DIR / "weight.textproto",
78+
WEIGHT_AXIS,
7279
next(a for a in FONTS_JSON["axisRegistry"] if a["tag"] == "wght"),
7380
Axis(
7481
tag="wght",

0 commit comments

Comments
 (0)