Skip to content

Commit

Permalink
Merge pull request #250 from phenobarbital/dev
Browse files Browse the repository at this point in the history
removed support for legacy aiohttp-cors
  • Loading branch information
phenobarbital authored Apr 24, 2024
2 parents 582c7ca + d8ae0dd commit 857a81d
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 33 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions navigator/applications/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion navigator/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 11 additions & 9 deletions navigator/handlers/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand All @@ -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
Expand Down
29 changes: 21 additions & 8 deletions navigator/navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion navigator/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__description__ = (
"Navigator Web Framework based on aiohttp, " "with batteries included."
)
__version__ = "2.8.41"
__version__ = "2.8.42"
__author__ = "Jesus Lara"
__author_email__ = "jesuslarag@gmail.com"
__license__ = "BSD"
3 changes: 2 additions & 1 deletion requirements/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -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
19 changes: 10 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=[
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 857a81d

Please sign in to comment.