From c70c3b080c4a7da353f4d45ff57469507e80a96b Mon Sep 17 00:00:00 2001 From: Ken Kroenlein Date: Wed, 3 Apr 2024 12:01:28 -0600 Subject: [PATCH] Update resource import strategy to resolve deprecation warnings --- gemd/__version__.py | 2 +- gemd/demo/cake.py | 6 ++---- gemd/demo/strehlow_and_cook.py | 5 ++--- gemd/units/impl.py | 28 ++++++++++++++-------------- requirements.txt | 1 + setup.py | 3 ++- tests/units/test_parser.py | 4 ++-- 7 files changed, 24 insertions(+), 25 deletions(-) diff --git a/gemd/__version__.py b/gemd/__version__.py index 9aa3f903..58039f50 100644 --- a/gemd/__version__.py +++ b/gemd/__version__.py @@ -1 +1 @@ -__version__ = "2.1.0" +__version__ = "2.1.1" diff --git a/gemd/demo/cake.py b/gemd/demo/cake.py index 95dc2bda..8586dac2 100644 --- a/gemd/demo/cake.py +++ b/gemd/demo/cake.py @@ -1,4 +1,5 @@ """Bake a cake.""" +from importlib_resources import files from io import BytesIO import random @@ -67,10 +68,7 @@ def get_template_scope(): def import_toothpick_picture() -> BytesIO: """Return the stream of the toothpick picture.""" - from importlib.resources import read_binary - resource = read_binary("gemd.demo", "toothpick.jpg") - - return BytesIO(resource) + return files("gemd.demo").joinpath("toothpick.jpg").open("rb") def make_cake_templates(): diff --git a/gemd/demo/strehlow_and_cook.py b/gemd/demo/strehlow_and_cook.py index 9c5a37f2..1bd5dad0 100644 --- a/gemd/demo/strehlow_and_cook.py +++ b/gemd/demo/strehlow_and_cook.py @@ -45,11 +45,10 @@ def import_table(filename=SMALL_TABLE): """Return the deserialized JSON table.""" - from importlib.resources import read_text + from importlib_resources import files import json - table = json.loads(read_text("gemd.demo", filename)) - return table + return json.loads(files("gemd.demo").joinpath(filename).read_text()) def _fingerprint(row): diff --git a/gemd/units/impl.py b/gemd/units/impl.py index 2b0830a3..7c3c8014 100644 --- a/gemd/units/impl.py +++ b/gemd/units/impl.py @@ -1,7 +1,7 @@ """Implementation of units.""" from deprecation import deprecated import functools -from importlib.resources import read_text +from importlib_resources import files import os from pathlib import Path import re @@ -28,18 +28,19 @@ ] -def _deploy_default_files() -> str: +def _deploy_default_files() -> Tuple[Path, Path]: """Copy the units & constants file into a temporary directory.""" - units_path = Path(_TEMP_DIRECTORY.name) / "citrine_en.txt" - units_path.write_text(read_text("gemd.units", "citrine_en.txt"), encoding="utf-8") + resources = files("gemd.units") + target_dir = Path(_TEMP_DIRECTORY.name) + target_paths = tuple(target_dir / f for f in ("citrine_en.txt", "constants_en.txt")) + for target in target_paths: + source = resources.joinpath(target.name) + target.write_text(source.read_text(), encoding="utf-8") - constants_path = Path(_TEMP_DIRECTORY.name) / "constants_en.txt" - constants_path.write_text(read_text("gemd.units", "constants_en.txt"), encoding="utf-8") + return target_paths - return str(units_path) - -DEFAULT_FILE = _deploy_default_files() +DEFAULT_FILE, DEFAULT_CONSTANTS = _deploy_default_files() _ALLOWED_OPERATORS = {".", "+", "-", "*", "/", "//", "^", "**", "(", ")"} @@ -393,15 +394,14 @@ def change_definitions_file(filename: str = None): if filename is None: target = DEFAULT_FILE else: - # TODO: Handle case where user provides a units file but no constants file target = Path(filename).expanduser().resolve(strict=True) current_dir = Path.cwd() try: - path = Path(target) - os.chdir(path.parent) - # Need to re-verify path because of some slippiness around tmp on MacOS - _REGISTRY = _ScaleFactorRegistry(filename=Path.cwd() / path.name, + os.chdir(target.parent) + # Need to re-verify path because of some slippiness around tmp on macOS + updated = (Path.cwd() / target.name).resolve(strict=True) + _REGISTRY = _ScaleFactorRegistry(filename=updated, preprocessors=[_space_after_minus_preprocessor, _scientific_notation_preprocessor, _scaling_preprocessor diff --git a/requirements.txt b/requirements.txt index 5bf007ea..d97eb0e2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pint==0.20 deprecation==2.1.0 typing-extensions==4.8.0 +importlib-resources==5.3.0 diff --git a/setup.py b/setup.py index 5fd4a733..8aac4cc8 100644 --- a/setup.py +++ b/setup.py @@ -38,7 +38,8 @@ install_requires=[ "pint>=0.20,<0.24", "deprecation>=2.1.0,<3", - "typing_extensions>=4.8,<5" + "typing_extensions>=4.8,<5", + "importlib-resources>=5.3,<7" ], extras_require={ "tests": [ diff --git a/tests/units/test_parser.py b/tests/units/test_parser.py index 85696b2e..7c2f86e4 100644 --- a/tests/units/test_parser.py +++ b/tests/units/test_parser.py @@ -1,6 +1,6 @@ from contextlib import contextmanager from deprecation import DeprecatedWarning -from importlib.resources import read_binary +from importlib_resources import files import re from pint import UnitRegistry import pytest @@ -180,7 +180,7 @@ def test_file_change(tmpdir): assert convert_units(1, 'usd', 'USD') == 1 test_file = tmpdir / "test_units.txt" - test_file.write_binary(read_binary("tests.units", "test_units.txt")) + test_file.write_binary(files("tests.units").joinpath("test_units.txt").read_bytes()) with _change_units(filename=test_file): with pytest.raises(UndefinedUnitError): assert convert_units(1, 'm', 'cm') == 100