Skip to content

Commit 025f835

Browse files
chore: remove usage of importlib.resources.path as it deprecated (#72)
Used the official migration receipe at https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy
1 parent 5e89f8e commit 025f835

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

jpyinterpreter/src/main/python/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
This module acts as an interface to the Python bytecode to Java bytecode interpreter
33
"""
4-
from .jvm_setup import init, set_class_output_directory
4+
from .jvm_setup import init, set_class_output_directory, get_path
55
from .annotations import JavaAnnotation, AnnotationValueSupplier, add_class_annotation, add_java_interface
66
from .conversions import (convert_to_java_python_like_object, unwrap_python_like_object,
77
update_python_object_from_java, is_c_native, add_python_java_type_mapping)

jpyinterpreter/src/main/python/jvm_setup.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,34 @@
22
import jpype
33
import jpype.imports
44
import importlib.resources
5-
from typing import List
5+
import os
6+
from typing import List, ContextManager
7+
8+
9+
def _normalize_path(path):
10+
"""Normalize a path by ensuring it is a string.
11+
12+
If the resulting string contains path separators, an exception is raised.
13+
"""
14+
str_path = str(path)
15+
parent, file_name = os.path.split(str_path)
16+
if parent:
17+
raise ValueError(f'{path!r} must be only a file name')
18+
return file_name
19+
20+
21+
def get_path(package: str, resource: str) -> ContextManager[pathlib.Path]:
22+
"""
23+
Workaround since importlib.resources.path is now deprecated for removal
24+
"""
25+
return importlib.resources.as_file(importlib.resources.files(package) /
26+
_normalize_path(resource))
627

728

829
def extract_python_translator_jars() -> list[str]:
930
"""Extracts and return a list of the Python Translator Java dependencies
1031
"""
11-
return [str(importlib.resources.path('jpyinterpreter.jars', p.name).__enter__())
32+
return [str(get_path('jpyinterpreter.jars', p.name).__enter__())
1233
for p in importlib.resources.files('jpyinterpreter.jars').iterdir()
1334
if p.name.endswith(".jar")]
1435

timefold-solver-python-core/src/main/python/_timefold_java_interop.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from jpype.types import *
66
import importlib.resources
77
from typing import cast, TypeVar, Callable, Union, TYPE_CHECKING, Any
8+
from _jpyinterpreter import get_path
89
from ._jpype_type_conversions import PythonSupplier, ConstraintProviderFunction, PythonConsumer
910

1011
if TYPE_CHECKING:
@@ -43,15 +44,15 @@ def extract_timefold_jars() -> list[str]:
4344
global _enterprise_installed
4445
try:
4546

46-
enterprise_dependencies = [str(importlib.resources.path('timefold.solver.enterprise.jars',
47-
p.name).__enter__())
47+
enterprise_dependencies = [str(get_path('timefold.solver.enterprise.jars',
48+
p.name).__enter__())
4849
for p in importlib.resources.files('timefold.solver.enterprise.jars').iterdir()
4950
if p.name.endswith(".jar")]
5051
_enterprise_installed = True
5152
except ModuleNotFoundError:
5253
enterprise_dependencies = []
5354
_enterprise_installed = False
54-
return [str(importlib.resources.path('timefold.solver.jars', p.name).__enter__())
55+
return [str(get_path('timefold.solver.jars', p.name).__enter__())
5556
for p in importlib.resources.files('timefold.solver.jars').iterdir()
5657
if p.name.endswith(".jar")] + enterprise_dependencies
5758

0 commit comments

Comments
 (0)