Skip to content

Commit 4b1c8b6

Browse files
committed
upd: updates imports of deprecated functions
1 parent 5927095 commit 4b1c8b6

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

src/experimaestro/connectors/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from experimaestro.locking import Lock
1717
from experimaestro.tokens import Token
1818
from experimaestro.utils.asyncio import asyncThreadcheck
19-
import pkg_resources
19+
from importlib.metadata import entry_points
2020

2121

2222
class RedirectType(enum.Enum):
@@ -101,7 +101,7 @@ def handler(key: str) -> Type["Process"]:
101101
"""Get a handler"""
102102
if Process.HANDLERS is None:
103103
Process.HANDLERS = {}
104-
for ep in pkg_resources.iter_entry_points(group="experimaestro.process"):
104+
for ep in entry_points(group="experimaestro.process"):
105105
logging.debug("Adding process handler for type %s", ep.name)
106106
handler = ep.load()
107107
Process.HANDLERS[ep.name] = handler

src/experimaestro/server/__init__.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@
55
import socket
66
import uuid
77
from experimaestro.scheduler.base import Job
8-
import pkg_resources
8+
import sys
99
import http
1010
import threading
1111
from typing import Optional, Tuple
12+
13+
if sys.version_info >= (3, 9):
14+
from importlib.resources import files
15+
16+
pkg_resources = None
17+
else:
18+
try:
19+
from importlib_resources import files
20+
21+
pkg_resources = None
22+
except ImportError:
23+
# Fallback to pkg_resources if importlib_resources not available
24+
import pkg_resources
25+
26+
files = None
1227
from experimaestro.scheduler import Scheduler, Listener as BaseListener
1328
from experimaestro.scheduler.services import Service, ServiceListener
1429
from experimaestro.settings import ServerSettings
@@ -143,6 +158,7 @@ def proxy_response(base_url: str, request: Request, path: str):
143158
return flask_response
144159

145160

161+
# flake8: noqa: C901
146162
def start_app(server: "Server"):
147163
logging.debug("Starting Flask server...")
148164
app = Flask("experimaestro")
@@ -256,10 +272,25 @@ def static_route(path):
256272

257273
datapath = "data/%s" % path
258274
logging.debug("Looking for %s", datapath)
259-
if pkg_resources.resource_exists("experimaestro.server", datapath):
260-
mimetype = MIMETYPES[datapath.rsplit(".", 1)[1]]
261-
content = pkg_resources.resource_string("experimaestro.server", datapath)
262-
return Response(content, mimetype=mimetype)
275+
276+
if files is not None:
277+
try:
278+
package_files = files("experimaestro.server")
279+
resource_file = package_files / datapath
280+
if resource_file.is_file():
281+
mimetype = MIMETYPES[datapath.rsplit(".", 1)[1]]
282+
content = resource_file.read_bytes()
283+
return Response(content, mimetype=mimetype)
284+
except (FileNotFoundError, KeyError):
285+
pass
286+
elif pkg_resources is not None:
287+
# Fallback to pkg_resources
288+
if pkg_resources.resource_exists("experimaestro.server", datapath):
289+
mimetype = MIMETYPES[datapath.rsplit(".", 1)[1]]
290+
content = pkg_resources.resource_string(
291+
"experimaestro.server", datapath
292+
)
293+
return Response(content, mimetype=mimetype)
263294
return Response("Page not found", status=404)
264295

265296
# Start the app

src/experimaestro/typingutils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
if sys.version_info.minor < 11:
77
from typing import _collect_type_vars as _collect_parameters
88
else:
9-
from typing import _collect_parameters
9+
10+
def _collect_parameters(bases):
11+
"""Collect type parameters from generic bases"""
12+
parameters = []
13+
for base in bases:
14+
if hasattr(base, "__parameters__"):
15+
parameters.extend(base.__parameters__)
16+
return tuple(parameters)
17+
18+
# from typing import _collect_parameters
1019

1120
from typing import _AnnotatedAlias as AnnotatedAlias, get_args, get_origin
1221

@@ -62,7 +71,7 @@ def get_type(typehint):
6271
while True:
6372
if t := get_optional(typehint):
6473
typehint = t
65-
if isinstance(typehint, AnnotatedAlias):
74+
if is_annotated(typehint):
6675
typehint = get_args(typehint)[0]
6776
else:
6877
break

0 commit comments

Comments
 (0)