Skip to content

Commit

Permalink
Releasing 2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Oct 28, 2020
1 parent dd5defc commit 3ac7736
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 35 deletions.
2 changes: 1 addition & 1 deletion emmett/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.1.0"
__version__ = "2.1.1"
21 changes: 13 additions & 8 deletions emmett/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
from functools import partial
from typing import Any, Callable, Dict

from rapidjson import (
DM_ISO8601, DM_NAIVE_IS_UTC,
NM_NATIVE,
loads as _json_loads
)
try:
import orjson
_json_impl = orjson.loads
_json_opts = {}
except ImportError:
import rapidjson
_json_impl = rapidjson.loads
_json_opts = {
"datetime_mode": rapidjson.DM_ISO8601 | rapidjson.DM_NAIVE_IS_UTC,
"number_mode": rapidjson.NM_NATIVE
}


class Parsers(object):
Expand All @@ -33,9 +39,8 @@ def get_for(cls, target):


json = partial(
_json_loads,
datetime_mode=DM_ISO8601 | DM_NAIVE_IS_UTC,
number_mode=NM_NATIVE
_json_impl,
**_json_opts
)

Parsers.register_for('json')(json)
23 changes: 17 additions & 6 deletions emmett/routing/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,19 @@ def __call__(self, output: Any, response: Response) -> HTTP:
return self.http_cls(
response.status,
output,
response.headers,
response.cookies
headers=response.headers,
cookies=response.cookies
)


class EmptyResponseBuilder(ResponseBuilder):
http_cls = HTTPResponse

def __call__(self, output: Any, response: Response) -> HTTPResponse:
return self.http_cls(
response.status,
headers=response.headers,
cookies=response.cookies
)


Expand All @@ -54,8 +65,8 @@ def __call__(self, output: Any, response: Response) -> HTTP:
return self.http_cls(
response.status,
self.process(output, response),
response.headers,
response.cookies
headers=response.headers,
cookies=response.cookies
)


Expand All @@ -66,8 +77,8 @@ def __call__(self, output: Any, response: Response) -> HTTPBytes:
return self.http_cls(
response.status,
output,
response.headers,
response.cookies
headers=response.headers,
cookies=response.cookies
)


Expand Down
2 changes: 2 additions & 0 deletions emmett/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from ..http import HTTP
from .response import (
MetaResponseBuilder,
EmptyResponseBuilder,
ResponseBuilder,
AutoResponseBuilder,
BytesResponseBuilder,
Expand Down Expand Up @@ -167,6 +168,7 @@ class HTTPRouter(Router):
_routing_rec_builder = RouteRecReq

_outputs = {
'empty': EmptyResponseBuilder,
'auto': AutoResponseBuilder,
'bytes': BytesResponseBuilder,
'str': ResponseBuilder,
Expand Down
5 changes: 1 addition & 4 deletions emmett/routing/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from typing import Any, Callable

from ..cache import RouteCacheRule
from ..http import HTTPResponse
from ..pipeline import RequestPipeline, WebsocketPipeline, Pipe
from .routes import HTTPRoute, WebsocketRoute

Expand Down Expand Up @@ -115,9 +114,7 @@ def __init__(

def _make_builders(self, output_type):
builder_cls = self.router._outputs[output_type]
response, head = builder_cls(self), builder_cls(self)
head.http_cls = HTTPResponse
return response, head
return builder_cls(self), self.router._outputs['empty'](self)

def __call__(self, f: Callable[..., Any]) -> Callable[..., Any]:
if not self.paths:
Expand Down
22 changes: 18 additions & 4 deletions emmett/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@
from functools import partial
from typing import Any, Callable, Dict, Union

from orjson import dumps as _json_dumps, OPT_NAIVE_UTC, OPT_NON_STR_KEYS

from .html import tag, htmlescape

try:
import orjson
_json_impl = orjson.dumps
_json_opts = {
"option": orjson.OPT_NON_STR_KEYS | orjson.OPT_NAIVE_UTC
}
_json_type = "bytes"
except ImportError:
import rapidjson
_json_impl = rapidjson.dumps
_json_opts = {
"datetime_mode": rapidjson.DM_ISO8601 | rapidjson.DM_NAIVE_IS_UTC,
"number_mode": rapidjson.NM_NATIVE | rapidjson.NM_DECIMAL
}
_json_type = "str"

_json_safe_table = {
'u2028': [r'\u2028', '\\u2028'],
'u2029': [r'\u2029', '\\u2029']
Expand Down Expand Up @@ -42,9 +56,9 @@ def _json_default(obj):


json = partial(
_json_dumps,
_json_impl,
default=_json_default,
option=OPT_NON_STR_KEYS | OPT_NAIVE_UTC
**_json_opts
)


Expand Down
4 changes: 2 additions & 2 deletions emmett/tools/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from ..ctx import current
from ..parsers import Parsers
from ..pipeline import Pipe
from ..serializers import Serializers
from ..serializers import Serializers, _json_type


class JSONServicePipe(Pipe):
__slots__ = ['decoder', 'encoder']
output = 'bytes'
output = _json_type

def __init__(self):
self.decoder = Parsers.get_for('json')
Expand Down
16 changes: 10 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "Emmett"
version = "2.1.0"
version = "2.1.1"
description = "The web framework for inventors"
authors = ["Giovanni Barillari <[email protected]>"]
license = "BSD-3-Clause"
Expand Down Expand Up @@ -46,26 +46,30 @@ python = "^3.7"
click = ">=6.0"
h11 = "~0.10.0"
h2 = "~4.0.0"
orjson = "~3.4.0"
pendulum = "~2.1.2"
pyaes = "~1.6.0"
pyaes = "~1.6.1"
pyDAL = "17.3"
python-rapidjson = "~0.9.1"
pyyaml = "~5.3.0"
python-rapidjson = "~0.9.3"
pyyaml = "~5.3.1"
renoir = "^1.2"
severus = "^1.0"
uvicorn = "0.12.1"
websockets = "^8.0"

httptools = { version = "~0.1.0", markers = "sys_platform != 'win32'" }
httptools = { version = "~0.1.1", markers = "sys_platform != 'win32'" }
uvloop = { version = "~0.14.0", markers = "sys_platform != 'win32'" }

orjson = { version = "~3.4.1", optional = true }

[tool.poetry.dev-dependencies]
ipaddress = "^1.0"
pylint = "^2.4.4"
pytest = "^5.3"
pytest-asyncio = "^0.10"

[tool.poetry.extras]
orjson = ["orjson"]

[tool.poetry.urls]
"Issue Tracker" = "https://github.com/emmett-framework/emmett/issues"

Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ h2==4.0.0
hpack==4.0.0
httptools==0.1.1; sys_platform != "win32"
hyperframe==6.0.0
orjson==3.4.0
pendulum==2.1.2
pyaes==1.6.1
pydal==17.3
python-dateutil==2.8.1
python-rapidjson==0.9.1
python-rapidjson==0.9.3
pytzdata==2020.1
pyyaml==5.3.1
renoir==1.2.0
Expand Down
8 changes: 6 additions & 2 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from emmett.http import HTTP
from emmett.pipeline import Pipe, Injector
from emmett.parsers import Parsers
from emmett.serializers import Serializers
from emmett.serializers import Serializers, _json_type

json_load = Parsers.get_for('json')
json_dump = Serializers.get_for('json')
Expand Down Expand Up @@ -556,6 +556,10 @@ async def test_module_pipeline_composition(app):

@pytest.mark.asyncio
async def test_receive_send_flow(app):
send_storage_key = {
"str": "text",
"bytes": "bytes"
}[_json_type]
with ws_ctx('/ws_inject') as ctx:
parallel_flow = [
'Pipe1.open', 'Pipe2.open_ws', 'Pipe3.open',
Expand All @@ -575,7 +579,7 @@ async def test_receive_send_flow(app):
'foo': 'bar',
'pipe1r': 'receive_inject', 'pipe2r': 'receive_inject'
}
assert json_load(ctx._send_storage[-1]['bytes']) == {
assert json_load(ctx._send_storage[-1][send_storage_key]) == {
'foo': 'bar',
'pipe1r': 'receive_inject', 'pipe2r': 'receive_inject',
'pipe1s': 'send_inject', 'pipe2s': 'send_inject'
Expand Down

0 comments on commit 3ac7736

Please sign in to comment.