|
5 | 5 | import socket |
6 | 6 | import uuid |
7 | 7 | from experimaestro.scheduler.base import Job |
8 | | -import pkg_resources |
| 8 | +import sys |
9 | 9 | import http |
10 | 10 | import threading |
11 | 11 | 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 |
12 | 27 | from experimaestro.scheduler import Scheduler, Listener as BaseListener |
13 | 28 | from experimaestro.scheduler.services import Service, ServiceListener |
14 | 29 | from experimaestro.settings import ServerSettings |
@@ -143,6 +158,7 @@ def proxy_response(base_url: str, request: Request, path: str): |
143 | 158 | return flask_response |
144 | 159 |
|
145 | 160 |
|
| 161 | +# flake8: noqa: C901 |
146 | 162 | def start_app(server: "Server"): |
147 | 163 | logging.debug("Starting Flask server...") |
148 | 164 | app = Flask("experimaestro") |
@@ -256,10 +272,25 @@ def static_route(path): |
256 | 272 |
|
257 | 273 | datapath = "data/%s" % path |
258 | 274 | 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) |
263 | 294 | return Response("Page not found", status=404) |
264 | 295 |
|
265 | 296 | # Start the app |
|
0 commit comments