Skip to content

Commit

Permalink
fix: python <3.9 absent importlib.resources.files
Browse files Browse the repository at this point in the history
  • Loading branch information
bymoye committed Nov 3, 2023
1 parent 57675b0 commit 72e7b30
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
9 changes: 8 additions & 1 deletion blacksheep/server/authentication/cookie.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 24 additions & 7 deletions blacksheep/server/resources.py
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 9 additions & 6 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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 += [
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,14 +27,15 @@
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
from tests.utils.application import FakeApplication


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
Expand Down
9 changes: 6 additions & 3 deletions tests/test_files_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,18 +24,21 @@
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
from blacksheep.utils.aio import get_running_loop


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")
Expand Down

0 comments on commit 72e7b30

Please sign in to comment.