From 72e7b30fc7ba968739af9d87d07de245a01823a9 Mon Sep 17 00:00:00 2001 From: bymoye <964907582@qq.com> Date: Fri, 3 Nov 2023 10:23:10 +0800 Subject: [PATCH] fix: python <3.9 absent importlib.resources.files --- blacksheep/server/authentication/cookie.py | 9 ++++++- blacksheep/server/resources.py | 31 +++++++++++++++++----- tests/test_application.py | 15 ++++++----- tests/test_auth.py | 5 ++-- tests/test_files_serving.py | 9 ++++--- 5 files changed, 50 insertions(+), 19 deletions(-) diff --git a/blacksheep/server/authentication/cookie.py b/blacksheep/server/authentication/cookie.py index 408ad030..2086c438 100644 --- a/blacksheep/server/authentication/cookie.py +++ b/blacksheep/server/authentication/cookie.py @@ -1,4 +1,11 @@ -from datetime import datetime, UTC +from datetime import datetime + +try: + from datetime import UTC +except ImportError: + from datetime import timezone + + UTC = timezone.utc from typing import Any, Optional, Sequence from guardpost import AuthenticationHandler, Identity diff --git a/blacksheep/server/resources.py b/blacksheep/server/resources.py index 6bd9b04d..4454a7f3 100644 --- a/blacksheep/server/resources.py +++ b/blacksheep/server/resources.py @@ -1,12 +1,29 @@ from os import path # import pkg_resources -from importlib.resources import files as pkg_resources_files +try: + from importlib.resources import files as pkg_resources_files + def get_resource_file_path(anchor, resource_name: str) -> str: + return str(pkg_resources_files(anchor) / resource_name) -def get_resource_file_content(file_name: str) -> str: - with open( - pkg_resources_files(__name__) / path.join(".", "res", file_name), - mode="rt", - ) as source: - return source.read() + def get_resource_file_content(file_name: str) -> str: + with open( + get_resource_file_path(__name__, path.join(".", "res", file_name)), + mode="rt", + ) as source: + return source.read() + +except ImportError: + # import pkg_resources + from pkg_resources import resource_filename + + def get_resource_file_path(anchor, resource_name: str) -> str: + return resource_filename(anchor, resource_name) + + def get_resource_file_content(file_name: str) -> str: + with open( + get_resource_file_path(__name__, path.join(".", "res", file_name)), + mode="rt", + ) as source: + return source.read() diff --git a/tests/test_application.py b/tests/test_application.py index 95853684..b812a0b2 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -9,7 +9,7 @@ from uuid import UUID, uuid4 # import pkg_resources -from importlib.resources import files as pkg_resources_files +# from importlib.resources import files as pkg_resources_files import pytest from guardpost import AuthenticationHandler, Identity, User from openapidocs.v3 import Info @@ -37,6 +37,7 @@ from blacksheep.server.di import di_scope_middleware from blacksheep.server.normalization import ensure_response from blacksheep.server.openapi.v3 import OpenAPIHandler +from blacksheep.server.resources import get_resource_file_path from blacksheep.server.responses import status_code, text from blacksheep.server.security.hsts import HSTSMiddleware from blacksheep.testing.helpers import get_example_scope @@ -66,7 +67,7 @@ def __init__(self, item) -> None: def read_multipart_mix_dat(): with open( - pkg_resources_files(__name__) / os.path.join("res", "multipart-mix.dat"), + get_resource_file_path(__name__, os.path.join("res", "multipart-mix.dat")), mode="rb", ) as dat_file: return dat_file.read() @@ -567,7 +568,9 @@ async def upload_files(request): # NB: in this example; we save files to output folder and verify # that their binaries are identical for part in files: - full_path = pkg_resources_files(__name__) / f"out/{part.file_name.decode()}" + full_path = get_resource_file_path( + __name__, f"out/{part.file_name.decode()}" + ) with open(full_path, mode="wb") as saved_file: saved_file.write(part.data) @@ -585,7 +588,7 @@ async def upload_files(request): rel_path = "files/" for file_name in file_names: - full_path = pkg_resources_files(__name__) / f"{rel_path}{file_name}" + full_path = get_resource_file_path(__name__, f"{rel_path}{file_name}") with open(full_path, mode="rb") as source_file: binary = source_file.read() lines += [ @@ -621,8 +624,8 @@ async def upload_files(request): # now files are in both folders: compare to ensure they are identical for file_name in file_names: - full_path = pkg_resources_files(__name__) / f"{rel_path}{file_name}" - copy_full_path = pkg_resources_files(__name__) / f"out/{file_name}" + full_path = get_resource_file_path(__name__, f"{rel_path}{file_name}") + copy_full_path = get_resource_file_path(__name__, f"out/{file_name}") with open(full_path, mode="rb") as source_file: binary = source_file.read() diff --git a/tests/test_auth.py b/tests/test_auth.py index 403c9388..7ca06432 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -4,7 +4,7 @@ import jwt # import pkg_resources -from importlib.resources import files as pkg_resources_files +# from importlib.resources import files as pkg_resources_files import pytest from guardpost import AuthorizationContext, Identity, Policy, UnauthorizedError from guardpost.common import AuthenticatedRequirement @@ -27,6 +27,7 @@ get_www_authenticated_header_from_generic_unauthorized_error, ) from blacksheep.server.di import di_scope_middleware, register_http_context +from blacksheep.server.resources import get_resource_file_path from blacksheep.testing.helpers import get_example_scope from blacksheep.testing.messages import MockReceive, MockSend from tests.test_files_serving import get_folder_path @@ -34,7 +35,7 @@ def get_file_path(file_name, folder_name: str = "res") -> str: - return str(pkg_resources_files(__name__) / f"{folder_name}/{file_name}") + return get_resource_file_path(__name__, f"{folder_name}/{file_name}") # region JWTBearer diff --git a/tests/test_files_serving.py b/tests/test_files_serving.py index 9bff5b91..a9b2e0c6 100644 --- a/tests/test_files_serving.py +++ b/tests/test_files_serving.py @@ -4,7 +4,7 @@ from typing import List from unittest.mock import create_autospec -from importlib.resources import files as pkg_resources_files +# from importlib.resources import files as pkg_resources_files import pytest from essentials.folders import get_file_extension @@ -24,6 +24,7 @@ from blacksheep.server.files.dynamic import get_response_for_file from blacksheep.server.files.static import get_response_for_static_content from blacksheep.server.headers.cache import CacheControlHeaderValue +from blacksheep.server.resources import get_resource_file_path from blacksheep.server.responses import text from blacksheep.testing.helpers import get_example_scope from blacksheep.testing.messages import MockReceive, MockSend @@ -31,11 +32,13 @@ def get_folder_path(folder_name: str) -> str: - return str(pkg_resources_files(__name__) / folder_name) + return get_resource_file_path(__name__, folder_name) + # return str(pkg_resources_files(__name__) / folder_name) def get_file_path(file_name, folder_name: str = "files") -> str: - return str(pkg_resources_files(__name__) / f"{folder_name}/{file_name}") + return get_resource_file_path(__name__, f"{folder_name}/{file_name}") + # return str(pkg_resources_files(__name__) / f"{folder_name}/{file_name}") files2_index_path = get_file_path("index.html", "files2")