Skip to content

Commit

Permalink
Fix compatibility with sanic-ext >= 22.6.0 (fixes #63)
Browse files Browse the repository at this point in the history
Replace distutils with packaging, (fixes #61)
Bump version
  • Loading branch information
ashleysommer committed Aug 9, 2022
1 parent 618a0dd commit 6a2a330
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Change Log

## 2.1.0
- Fix compatibility with Sanic-EXT v22.6.0+
- Replace deprecated setuptools/`distutils` dependency with `packaging`

## 2.0.1
- Fix constructor of CORSErrorHandler, to remove 22.6 deprecation warning. Fixes #57

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Sanic-Ext Usage
~~~~~~~~~~~~~~~

Sanic-CORS can use Sanic-Ext to load the plugin for you.
( But you need to make sure to disable the built-in sanic-ext CORS support too)
(But you need to make sure to disable the built-in sanic-ext CORS support too)

.. code:: python
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sanic>=21.9.3
packaging>=21.3
22 changes: 16 additions & 6 deletions sanic_cors/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@
from sanic.models.futures import FutureMiddleware

from .core import *
from distutils.version import LooseVersion
from packaging.version import Version
import logging


try:
import sanic_ext
from sanic_ext.extensions.base import Extension
from sanic_ext.config import Config
SANIC_EXT_VERSION = Version(sanic_ext.__version__)
use_ext = True
except ImportError:
use_ext = False
Extension = object
SANIC_EXT_VERSION = Version("0.0.0")
from sanic.config import Config


SANIC_VERSION = LooseVersion(sanic_version)
SANIC_21_9_0 = LooseVersion("21.9.0")
SANIC_VERSION = Version(sanic_version)
SANIC_21_9_0 = Version("21.9.0")
SANIC_EXT_22_6_0 = Version("22.6.0")

USE_ASYNC_EXCEPTION_HANDLER = False

Expand Down Expand Up @@ -153,14 +156,20 @@ class CORS(Extension):

name: str = "SanicCORS"

def __init__(self, app: Sanic, config: Optional[Config] = None, *args, **kwargs):
def __init__(self, app: Optional[Sanic] = None, config: Optional[Config] = None, *args, **kwargs):
if SANIC_21_9_0 > SANIC_VERSION:
raise RuntimeError(
"You cannot use this version of Sanic-CORS with "
"Sanic earlier than v21.9.0")
self._options = kwargs
if use_ext:
super(CORS, self).__init__(app, config)
if SANIC_EXT_22_6_0 > SANIC_EXT_VERSION:
if app is None:
raise RuntimeError("Sanic-CORS Extension not registered on app properly. "
"Please upgrade to newer sanic-ext version.")
super(CORS, self).__init__(app, config)
else:
super(CORS, self).__init__()
else:
super(CORS, self).__init__(*args)
self.app = app
Expand All @@ -182,7 +191,8 @@ def startup(self, bootstrap):
"""
Used by sanic-ext to start up an extension
"""
assert bootstrap.app == self.app
if bootstrap.app != self.app:
raise RuntimeError("Sanic-CORS bootstrap got the wrong app instance. Is sanic-ext enabled properly?")
_options = self._options
cors_options = _options or bootstrap.config.get("CORS_OPTIONS", {})
no_startup = cors_options.pop("no_startup", None)
Expand Down
2 changes: 1 addition & 1 deletion sanic_cors/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.1'
__version__ = '2.1.0'

0 comments on commit 6a2a330

Please sign in to comment.