From 5f2eada9519b4a18335101225723b8da9291d678 Mon Sep 17 00:00:00 2001 From: Jesus Lara Date: Thu, 25 Apr 2024 01:41:29 +0200 Subject: [PATCH] removed support for legacy aiohttp-cors --- Makefile | 1 - navigator/applications/startup.py | 7 +++++-- navigator/extensions.py | 4 +++- navigator/handlers/base.pyx | 20 +++++++++++--------- navigator/navigator.py | 29 +++++++++++++++++++++-------- navigator/version.py | 2 +- requirements/requirements-dev.txt | 3 ++- setup.cfg | 9 +++++++++ setup.py | 19 ++++++++++--------- tox.ini | 2 +- 10 files changed, 63 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index fcd345f5..90d19764 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,6 @@ develop: python -m pip install -Ur requirements/requirements-dev.txt # add other dependencies: pip install --upgrade navigator-session navigator-auth - pip install -e services/aiohttp-cors echo 'start develop Navigator' setup: diff --git a/navigator/applications/startup.py b/navigator/applications/startup.py index 40a9c649..0116df08 100644 --- a/navigator/applications/startup.py +++ b/navigator/applications/startup.py @@ -7,6 +7,7 @@ """ import importlib from types import ModuleType +from aiohttp.web import AppKey from navconfig.logging import logging from navconfig import BASE_DIR @@ -100,9 +101,11 @@ def app_startup(app_list: list, app: WebApp, context: dict = None, **kwargs): # TODO: build automatic documentation try: # can I add Main to subApp? - sub_app["Main"] = app + main_key = AppKey("Main", WebApp) + sub_app[main_key] = app for name, ext in app.extensions.items(): - sub_app[name] = ext + sub_key = AppKey(name, WebApp) + sub_app[sub_key] = ext sub_app.extensions[name] = ext except (KeyError, AttributeError) as err: logging.warning(err) diff --git a/navigator/extensions.py b/navigator/extensions.py index 44ded756..a98e5f61 100644 --- a/navigator/extensions.py +++ b/navigator/extensions.py @@ -3,6 +3,7 @@ from typing import Optional from collections.abc import Callable from abc import ABC +from aiohttp.web import AppKey from navconfig import config from navconfig.logging import logging from .exceptions import NavException, ConfigError @@ -62,7 +63,8 @@ def setup(self, app: WebApp) -> WebApp: else: raise TypeError(f"Invalid type for Application Setup: {app}:{type(app)}") # register the extension into the app - self.app[self.name] = self + self.app[AppKey(self.name, WebApp)] = self + # self.app[self.name] = self try: self.app.extensions[self.name] = self except AttributeError: diff --git a/navigator/handlers/base.pyx b/navigator/handlers/base.pyx index 1bddacf3..e30186b2 100644 --- a/navigator/handlers/base.pyx +++ b/navigator/handlers/base.pyx @@ -7,6 +7,7 @@ from collections.abc import Callable from aiohttp import web from aiohttp.abc import AbstractView import aiohttp_cors +from aiohttp_cors import setup as cors_setup, ResourceOptions from pathlib import Path from navconfig import config, DEBUG, BASE_DIR from ..functions import cPrint @@ -76,13 +77,13 @@ cdef class BaseAppHandler: if 'extensions' not in app: app.extensions = {} # empty directory of extensions # CORS - self.cors = aiohttp_cors.setup( + self.cors = cors_setup( app, defaults={ - "*": aiohttp_cors.ResourceOptions( + "*": ResourceOptions( allow_credentials=True, expose_headers="*", - allow_methods="*", + # -- allow_methods="*", allow_headers="*", max_age=1600, ) @@ -94,12 +95,13 @@ cdef class BaseAppHandler: # CORS: for route in list(self.app.router.routes()): try: - if inspect.isclass(route.handler) and issubclass( - route.handler, AbstractView - ): - self.cors.add(route, webview=True) - else: - self.cors.add(route) + if not isinstance(route.resource, web.StaticResource): + if inspect.isclass(route.handler) and issubclass( + route.handler, AbstractView + ): + self.cors.add(route, webview=True) + else: + self.cors.add(route) except (TypeError, ValueError, RuntimeError) as exc: if 'already has OPTIONS handler' in str(exc): continue diff --git a/navigator/navigator.py b/navigator/navigator.py index d84e657d..d997062e 100644 --- a/navigator/navigator.py +++ b/navigator/navigator.py @@ -80,6 +80,8 @@ def __init__( self._loop = asyncio.get_event_loop() except RuntimeError: self._loop = asyncio.new_event_loop() + if 'evt' in kwargs: + self._loop = kwargs.pop('evt') self._loop.set_exception_handler(nav_exception_handler) asyncio.set_event_loop(self._loop) # May want to catch other signals too @@ -492,14 +494,25 @@ def run(self): access_log=enable_access_log ) else: - web.run_app( - app, - host=self.host, - port=self.port, - loop=self._loop, - handle_signals=True, - access_log=enable_access_log - ) + try: + web.run_app( + app, + host=self.host, + port=self.port, + loop=self._loop, + handle_signals=True, + access_log=enable_access_log + ) + except RuntimeError: + # loop already running + web.run_app( + app, + host=self.host, + port=self.port, + loop=asyncio.get_running_loop(), + handle_signals=True, + access_log=enable_access_log + ) async def app_runner( diff --git a/navigator/version.py b/navigator/version.py index c623e92a..0c4b4ca1 100644 --- a/navigator/version.py +++ b/navigator/version.py @@ -4,7 +4,7 @@ __description__ = ( "Navigator Web Framework based on aiohttp, " "with batteries included." ) -__version__ = "2.8.39" +__version__ = "2.8.40" __author__ = "Jesus Lara" __author_email__ = "jesuslarag@gmail.com" __license__ = "BSD" diff --git a/requirements/requirements-dev.txt b/requirements/requirements-dev.txt index 6f9ea462..d9ee3bf3 100644 --- a/requirements/requirements-dev.txt +++ b/requirements/requirements-dev.txt @@ -10,7 +10,8 @@ mypy==1.7.1 pylint==2.16.1 pyperf==2.6.2 pytest>=6.0.0 -pytest-asyncio==0.23.2 +pytest-aiohttp==1.0.5 +pytest-asyncio==0.23.6 pytest-cov==4.0.0 pytest-cython==0.2.0 pytest-xdist==3.5.0 diff --git a/setup.cfg b/setup.cfg index 49388311..36fdcd21 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,12 @@ [wheel] python-tag = py39 universal = 1 + +[aliases] +test = pytest + +[tool:pytest] +addopts= --cov=navigator --cov-report=term --cov-report=html --cov-branch --no-cov-on-fail + +[mypy] +files = navigator diff --git a/setup.py b/setup.py index 3454d297..34b70315 100644 --- a/setup.py +++ b/setup.py @@ -81,13 +81,13 @@ def readme(): for node in (n for n in t.body if isinstance(n, ast.Assign)): if len(node.targets) == 1: name = node.targets[0] - if isinstance(name, ast.Name) and \ - name.id in ( - '__version__', - '__title__', - '__description__', - '__author__', - '__license__', '__author_email__'): + if isinstance(name, ast.Name) and name.id in ( + '__version__', + '__title__', + '__description__', + '__author__', + '__license__', '__author_email__' + ): v = node.value if name.id == '__version__': __version__ = v.s @@ -131,6 +131,7 @@ def readme(): packages=find_packages(exclude=('tests', 'docs', )), package_data={"navigator": ["py.typed"]}, include_package_data=True, + zip_safe=False, license=__license__, license_files='LICENSE', setup_requires=[ @@ -143,7 +144,7 @@ def readme(): "Cython==3.0.9", "asyncio==3.4.3", "uvloop==0.19.0", - "aiohttp==3.9.3", + "aiohttp==3.9.5", "PySocks==1.7.1", "aiodns==3.0.0", "asn1crypto==1.4.0", @@ -153,7 +154,6 @@ def readme(): "httptools==0.5.0", "aiosocks==0.2.6", 'python-slugify==8.0.1', - "aiohttp-cors", "proxylists>=0.12.4", "httpx==0.26.0", "beautifulsoup4==4.12.3", @@ -164,6 +164,7 @@ def readme(): "aiohttp-sse==2.2.0", "asyncdb>=2.6.0", "navconfig[default]>=1.6.3", + "alt-aiohttp-cors==0.7.1" # aiohttp-cors replacement ], extras_require={ "locale": [ diff --git a/tox.ini b/tox.ini index cb177ed0..905d2187 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] skipsdist = True -envlist = py38 py39 py310 py311 +envlist = py38, py39, py310, py311, py312, check [testenv] deps= -r{toxinidir}/requirements-dev.txt