Skip to content

Commit f84671e

Browse files
authored
Update resource import strategy to resolve deprecation warnings (#213)
1 parent ce04668 commit f84671e

File tree

7 files changed

+24
-25
lines changed

7 files changed

+24
-25
lines changed

gemd/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.1.0"
1+
__version__ = "2.1.1"

gemd/demo/cake.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Bake a cake."""
2+
from importlib_resources import files
23
from io import BytesIO
34
import random
45

@@ -67,10 +68,7 @@ def get_template_scope():
6768

6869
def import_toothpick_picture() -> BytesIO:
6970
"""Return the stream of the toothpick picture."""
70-
from importlib.resources import read_binary
71-
resource = read_binary("gemd.demo", "toothpick.jpg")
72-
73-
return BytesIO(resource)
71+
return files("gemd.demo").joinpath("toothpick.jpg").open("rb")
7472

7573

7674
def make_cake_templates():

gemd/demo/strehlow_and_cook.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@
4545

4646
def import_table(filename=SMALL_TABLE):
4747
"""Return the deserialized JSON table."""
48-
from importlib.resources import read_text
48+
from importlib_resources import files
4949
import json
50-
table = json.loads(read_text("gemd.demo", filename))
5150

52-
return table
51+
return json.loads(files("gemd.demo").joinpath(filename).read_text())
5352

5453

5554
def _fingerprint(row):

gemd/units/impl.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Implementation of units."""
22
from deprecation import deprecated
33
import functools
4-
from importlib.resources import read_text
4+
from importlib_resources import files
55
import os
66
from pathlib import Path
77
import re
@@ -28,18 +28,19 @@
2828
]
2929

3030

31-
def _deploy_default_files() -> str:
31+
def _deploy_default_files() -> Tuple[Path, Path]:
3232
"""Copy the units & constants file into a temporary directory."""
33-
units_path = Path(_TEMP_DIRECTORY.name) / "citrine_en.txt"
34-
units_path.write_text(read_text("gemd.units", "citrine_en.txt"), encoding="utf-8")
33+
resources = files("gemd.units")
34+
target_dir = Path(_TEMP_DIRECTORY.name)
35+
target_paths = tuple(target_dir / f for f in ("citrine_en.txt", "constants_en.txt"))
36+
for target in target_paths:
37+
source = resources.joinpath(target.name)
38+
target.write_text(source.read_text(), encoding="utf-8")
3539

36-
constants_path = Path(_TEMP_DIRECTORY.name) / "constants_en.txt"
37-
constants_path.write_text(read_text("gemd.units", "constants_en.txt"), encoding="utf-8")
40+
return target_paths
3841

39-
return str(units_path)
4042

41-
42-
DEFAULT_FILE = _deploy_default_files()
43+
DEFAULT_FILE, DEFAULT_CONSTANTS = _deploy_default_files()
4344
_ALLOWED_OPERATORS = {".", "+", "-", "*", "/", "//", "^", "**", "(", ")"}
4445

4546

@@ -393,15 +394,14 @@ def change_definitions_file(filename: str = None):
393394
if filename is None:
394395
target = DEFAULT_FILE
395396
else:
396-
# TODO: Handle case where user provides a units file but no constants file
397397
target = Path(filename).expanduser().resolve(strict=True)
398398

399399
current_dir = Path.cwd()
400400
try:
401-
path = Path(target)
402-
os.chdir(path.parent)
403-
# Need to re-verify path because of some slippiness around tmp on MacOS
404-
_REGISTRY = _ScaleFactorRegistry(filename=Path.cwd() / path.name,
401+
os.chdir(target.parent)
402+
# Need to re-verify path because of some slippiness around tmp on macOS
403+
updated = (Path.cwd() / target.name).resolve(strict=True)
404+
_REGISTRY = _ScaleFactorRegistry(filename=updated,
405405
preprocessors=[_space_after_minus_preprocessor,
406406
_scientific_notation_preprocessor,
407407
_scaling_preprocessor

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pint==0.20
22
deprecation==2.1.0
33
typing-extensions==4.8.0
4+
importlib-resources==5.3.0

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
install_requires=[
3939
"pint>=0.20,<0.24",
4040
"deprecation>=2.1.0,<3",
41-
"typing_extensions>=4.8,<5"
41+
"typing_extensions>=4.8,<5",
42+
"importlib-resources>=5.3,<7"
4243
],
4344
extras_require={
4445
"tests": [

tests/units/test_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from contextlib import contextmanager
22
from deprecation import DeprecatedWarning
3-
from importlib.resources import read_binary
3+
from importlib_resources import files
44
import re
55
from pint import UnitRegistry
66
import pytest
@@ -180,7 +180,7 @@ def test_file_change(tmpdir):
180180
assert convert_units(1, 'usd', 'USD') == 1
181181

182182
test_file = tmpdir / "test_units.txt"
183-
test_file.write_binary(read_binary("tests.units", "test_units.txt"))
183+
test_file.write_binary(files("tests.units").joinpath("test_units.txt").read_bytes())
184184
with _change_units(filename=test_file):
185185
with pytest.raises(UndefinedUnitError):
186186
assert convert_units(1, 'm', 'cm') == 100

0 commit comments

Comments
 (0)