Skip to content

Commit

Permalink
Fix resource files paths issues
Browse files Browse the repository at this point in the history
- Remove commented imports and commented code lines
- Replace pkg_resources with importlib.resources.files
- Fix problems caused by importlib.resources.files handling the anchor differnetly than pkg_resources
- Sort imports
  • Loading branch information
RobertoPrevato committed Nov 3, 2023
1 parent 72e7b30 commit 16aea75
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 45 deletions.
34 changes: 9 additions & 25 deletions blacksheep/server/resources.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
from os import path
from importlib.resources import files

# import pkg_resources
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_path(anchor, file_name: str) -> str:
return str(files(anchor) / file_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()

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()
def get_resource_file_content(file_name: str) -> str:
with open(
get_resource_file_path("blacksheep.server.res", file_name),
mode="rt",
) as source:
return source.read()
2 changes: 1 addition & 1 deletion blacksheep/utils/time.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from datetime import datetime, MINYEAR, timezone
from datetime import MINYEAR, datetime, timezone

UTC = timezone.utc

Expand Down
3 changes: 1 addition & 2 deletions tests/client/test_cookiejar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from datetime import timedelta

from blacksheep.utils.time import utcnow

import pytest

from blacksheep import URL, Cookie, Response, TextContent
Expand All @@ -14,6 +12,7 @@
)
from blacksheep.cookies import datetime_from_cookie_format
from blacksheep.scribe import write_response_cookie
from blacksheep.utils.time import utcnow

from . import FakePools

Expand Down
12 changes: 5 additions & 7 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from typing import Any, Dict, List, Optional, TypeVar
from uuid import UUID, uuid4

# import pkg_resources
# 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 @@ -67,7 +65,7 @@ def __init__(self, item) -> None:

def read_multipart_mix_dat():
with open(
get_resource_file_path(__name__, os.path.join("res", "multipart-mix.dat")),
get_resource_file_path("tests", os.path.join("res", "multipart-mix.dat")),
mode="rb",
) as dat_file:
return dat_file.read()
Expand Down Expand Up @@ -569,7 +567,7 @@ async def upload_files(request):
# that their binaries are identical
for part in files:
full_path = get_resource_file_path(
__name__, f"out/{part.file_name.decode()}"
"tests", f"out/{part.file_name.decode()}"
)
with open(full_path, mode="wb") as saved_file:
saved_file.write(part.data)
Expand All @@ -588,7 +586,7 @@ async def upload_files(request):
rel_path = "files/"

for file_name in file_names:
full_path = get_resource_file_path(__name__, f"{rel_path}{file_name}")
full_path = get_resource_file_path("tests", f"{rel_path}{file_name}")
with open(full_path, mode="rb") as source_file:
binary = source_file.read()
lines += [
Expand Down Expand Up @@ -624,8 +622,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 = get_resource_file_path(__name__, f"{rel_path}{file_name}")
copy_full_path = get_resource_file_path(__name__, f"out/{file_name}")
full_path = get_resource_file_path("tests", f"{rel_path}{file_name}")
copy_full_path = get_resource_file_path("tests", f"out/{file_name}")

with open(full_path, mode="rb") as source_file:
binary = source_file.read()
Expand Down
5 changes: 1 addition & 4 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
from typing import Any, Dict, Optional

import jwt

# import pkg_resources
# from importlib.resources import files as pkg_resources_files
import pytest
from guardpost import AuthorizationContext, Identity, Policy, UnauthorizedError
from guardpost.common import AuthenticatedRequirement
Expand Down Expand Up @@ -35,7 +32,7 @@


def get_file_path(file_name, folder_name: str = "res") -> str:
return get_resource_file_path(__name__, f"{folder_name}/{file_name}")
return get_resource_file_path("tests", f"{folder_name}/{file_name}")


# region JWTBearer
Expand Down
7 changes: 2 additions & 5 deletions tests/test_files_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import List
from unittest.mock import create_autospec

# from importlib.resources import files as pkg_resources_files
import pytest
from essentials.folders import get_file_extension

Expand Down Expand Up @@ -32,13 +31,11 @@


def get_folder_path(folder_name: str) -> str:
return get_resource_file_path(__name__, folder_name)
# return str(pkg_resources_files(__name__) / folder_name)
return get_resource_file_path("tests", folder_name)


def get_file_path(file_name, folder_name: str = "files") -> str:
return get_resource_file_path(__name__, f"{folder_name}/{file_name}")
# return str(pkg_resources_files(__name__) / f"{folder_name}/{file_name}")
return get_resource_file_path("tests", f"{folder_name}/{file_name}")


files2_index_path = get_file_path("index.html", "files2")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_openapi_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ValueFormat,
ValueType,
)
from pydantic import BaseModel, HttpUrl, VERSION
from pydantic import VERSION, BaseModel, HttpUrl
from pydantic.types import NegativeFloat, PositiveInt, condecimal, confloat, conint

from blacksheep.server.application import Application
Expand Down
1 change: 1 addition & 0 deletions tests/test_pathutils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys

import pytest

from blacksheep.common.files.pathsutils import (
Expand Down

0 comments on commit 16aea75

Please sign in to comment.