1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 04:50:05 +00:00

Move HomeAssistantView to separate file. Convert http to async syntax. [skip ci] (#12982)

* Move HomeAssistantView to separate file. Convert http to async syntax.

* pylint

* websocket api

* update emulated_hue for async/await

* Lint
This commit is contained in:
Boyi C
2018-03-09 09:51:49 +08:00
committed by Paulus Schoutsen
parent 2ee73ca911
commit 321eb2ec6f
17 changed files with 292 additions and 344 deletions

View File

@@ -1,5 +1,5 @@
"""Static file handling for HTTP component."""
import asyncio
import re
from aiohttp import hdrs
@@ -14,8 +14,7 @@ _FINGERPRINT = re.compile(r'^(.+)-[a-z0-9]{32}\.(\w+)$', re.IGNORECASE)
class CachingStaticResource(StaticResource):
"""Static Resource handler that will add cache headers."""
@asyncio.coroutine
def _handle(self, request):
async def _handle(self, request):
filename = URL(request.match_info['filename']).path
try:
# PyLint is wrong about resolve not being a member.
@@ -32,7 +31,7 @@ class CachingStaticResource(StaticResource):
raise HTTPNotFound() from error
if filepath.is_dir():
return (yield from super()._handle(request))
return await super()._handle(request)
elif filepath.is_file():
return CachingFileResponse(filepath, chunk_size=self._chunk_size)
else:
@@ -49,26 +48,24 @@ class CachingFileResponse(FileResponse):
orig_sendfile = self._sendfile
@asyncio.coroutine
def sendfile(request, fobj, count):
async def sendfile(request, fobj, count):
"""Sendfile that includes a cache header."""
cache_time = 31 * 86400 # = 1 month
self.headers[hdrs.CACHE_CONTROL] = "public, max-age={}".format(
cache_time)
yield from orig_sendfile(request, fobj, count)
await orig_sendfile(request, fobj, count)
# Overwriting like this because __init__ can change implementation.
self._sendfile = sendfile
@middleware
@asyncio.coroutine
def staticresource_middleware(request, handler):
async def staticresource_middleware(request, handler):
"""Middleware to strip out fingerprint from fingerprinted assets."""
path = request.path
if not path.startswith('/static/') and not path.startswith('/frontend'):
return (yield from handler(request))
return await handler(request)
fingerprinted = _FINGERPRINT.match(request.match_info['filename'])
@@ -76,4 +73,4 @@ def staticresource_middleware(request, handler):
request.match_info['filename'] = \
'{}.{}'.format(*fingerprinted.groups())
return (yield from handler(request))
return await handler(request)