Skip to content

Commit

Permalink
Update resource import strategy to resolve deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kroenlein committed Apr 3, 2024
1 parent ce04668 commit c70c3b0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion gemd/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.1.0"
__version__ = "2.1.1"
6 changes: 2 additions & 4 deletions gemd/demo/cake.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Bake a cake."""
from importlib_resources import files
from io import BytesIO
import random

Expand Down Expand Up @@ -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():
Expand Down
5 changes: 2 additions & 3 deletions gemd/demo/strehlow_and_cook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
28 changes: 14 additions & 14 deletions gemd/units/impl.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = {".", "+", "-", "*", "/", "//", "^", "**", "(", ")"}


Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pint==0.20
deprecation==2.1.0
typing-extensions==4.8.0
importlib-resources==5.3.0
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
4 changes: 2 additions & 2 deletions tests/units/test_parser.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c70c3b0

Please sign in to comment.