Skip to content

Commit

Permalink
Remove deprecated feature
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoPrevato committed Jan 3, 2023
1 parent dfa834b commit 9fd845c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 116 deletions.
23 changes: 2 additions & 21 deletions neoteroi/web/server/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
from neoteroi.web.server.cors import CORSPolicy, CORSStrategy, get_cors_middleware
from neoteroi.web.server.env import EnvironmentSettings
from neoteroi.web.server.errors import ServerErrorDetailsHandler
from neoteroi.web.server.files import ServeFilesOptions
from neoteroi.web.server.files.dynamic import serve_files_dynamic
from neoteroi.web.server.normalization import normalize_handler, normalize_middleware
from neoteroi.web.server.responses import _ensure_bytes
Expand Down Expand Up @@ -435,7 +434,8 @@ def exception_handler(
self, exception: Union[int, Type[Exception]]
) -> Callable[..., Any]:
"""
Registers an exception handler function in the application exception handler.
Registers an exception handler function in the application exception handlers
dictionary.
"""

def decorator(f):
Expand Down Expand Up @@ -475,25 +475,6 @@ def serve_files(
response would be otherwise 404 Not Found; e.g. use this to serve SPA that
use HTML5 History API for client side routing.
"""
if isinstance(source_folder, ServeFilesOptions):
# deprecated class, will be removed in the next version
from typing import cast

deprecated_arg = cast(ServeFilesOptions, source_folder)
deprecated_arg.validate()
serve_files_dynamic(
self.router,
self.files_handler,
str(deprecated_arg.source_folder),
discovery=deprecated_arg.discovery,
cache_time=deprecated_arg.cache_time,
extensions=deprecated_arg.extensions,
root_path=deprecated_arg.root_path,
index_document=deprecated_arg.index_document,
fallback_document=deprecated_arg.fallback_document,
anonymous_access=deprecated_arg.allow_anonymous,
)
return
serve_files_dynamic(
self.router,
self.files_handler,
Expand Down
76 changes: 1 addition & 75 deletions neoteroi/web/server/files/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid
from pathlib import Path
from typing import AsyncIterable, Callable, Optional, Set, Union
from typing import AsyncIterable, Callable, Optional, Set

from neoteroi.web import Request, Response, StreamedContent
from neoteroi.web.common.files.asyncfs import FilesHandler
Expand Down Expand Up @@ -184,80 +184,6 @@ def validate_source_path(source_folder: str) -> None:
raise InvalidArgument("The given root path is not a directory.")


class ServeFilesOptions:

__slots__ = (
"source_folder",
"extensions",
"discovery",
"cache_time",
"root_path",
"allow_anonymous",
"index_document",
"fallback_document",
)

def __init__(
self,
source_folder: Union[str, Path],
*,
extensions: Optional[Set[str]] = None,
discovery: bool = False,
cache_time: int = 10800,
root_path: str = "",
allow_anonymous: bool = True,
index_document: Optional[str] = "index.html",
fallback_document: Optional[str] = None,
):
"""
Options for static files serving.
Args:
source_folder (str): Path to the source folder containing static files.
extensions: The set of files extensions to serve.
discovery: Whether to enable file discovery, serving HTML pages for folders.
cache_time: Controls the Cache-Control Max-Age in seconds for static files.
root_path: Path prefix used for routing requests.
For example, if set to "public", files are served at "/public/*".
allow_anonymous: Whether to enable anonymous access to static files, true by
default.
index_document: The name of the index document to display, if present,
in folders. Requests for folders that contain a file with matching produce
a response with this document.
fallback_document: Optional file name, for a document to serve when a
response would be otherwise 404 Not Found; e.g. use this to serve SPA that
use HTML5 History API for client side routing.
"""
import warnings

warnings.warn(
"The ServeFilesOptions is deprecated and will "
"be removed in the version 1.0.0. Update your code to not use this class "
"and pass the same arguments to the application.serve_files method. "
"For more information see "
"https://www.neoteroi.dev/blacksheep/static-files/",
DeprecationWarning,
)

if extensions is None:
extensions = self.get_default_extensions()
self.source_folder = source_folder
self.extensions = set(extensions)
self.discovery = bool(discovery)
self.cache_time = int(cache_time)
self.root_path = root_path
self.allow_anonymous = allow_anonymous
self.index_document = index_document
self.fallback_document = fallback_document

def get_default_extensions(self) -> Set[str]:
"""Returns a set of extensions that are served by default."""
return get_default_extensions()

def validate(self) -> None:
validate_source_path(str(self.source_folder))


def get_response_for_file(
files_handler: FilesHandler,
request: Request,
Expand Down
20 changes: 0 additions & 20 deletions tests/test_files_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from neoteroi.web.server.files import (
FileInfo,
RangeNotSatisfiable,
ServeFilesOptions,
_get_requested_range,
get_default_extensions,
get_range_file_getter,
Expand Down Expand Up @@ -338,25 +337,6 @@ async def test_serve_files_no_discovery(app):
assert response.status == 404


@pytest.mark.asyncio
async def test_serve_files_deprecated_serve_files_options(files2_index_contents, app):
with pytest.deprecated_call():
app.serve_files(ServeFilesOptions(get_folder_path("files2"))) # type: ignore

await app.start()

scope = get_example_scope("GET", "/", [])
await app(
scope,
MockReceive(),
MockSend(),
)

response = app.response
assert response.status == 200
assert files2_index_contents == await response.read()


@pytest.mark.asyncio
async def test_serve_files_fallback_document(files2_index_contents: bytes, app):
"""Feature used to serve SPAs that use HTML5 History API"""
Expand Down

0 comments on commit 9fd845c

Please sign in to comment.