mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Show ANSI color codes in logs in Hass.io (#18834)
* Hass.io: Show ANSI color codes in logs * Lint * Fix test * Lint
This commit is contained in:
parent
b7e2522083
commit
eec4564c71
@ -15,7 +15,6 @@ from aiohttp import web
|
|||||||
from aiohttp.hdrs import CONTENT_TYPE
|
from aiohttp.hdrs import CONTENT_TYPE
|
||||||
from aiohttp.web_exceptions import HTTPBadGateway
|
from aiohttp.web_exceptions import HTTPBadGateway
|
||||||
|
|
||||||
from homeassistant.const import CONTENT_TYPE_TEXT_PLAIN
|
|
||||||
from homeassistant.components.http import KEY_AUTHENTICATED, HomeAssistantView
|
from homeassistant.components.http import KEY_AUTHENTICATED, HomeAssistantView
|
||||||
|
|
||||||
from .const import X_HASSIO
|
from .const import X_HASSIO
|
||||||
@ -63,8 +62,6 @@ class HassIOView(HomeAssistantView):
|
|||||||
client = await self._command_proxy(path, request)
|
client = await self._command_proxy(path, request)
|
||||||
|
|
||||||
data = await client.read()
|
data = await client.read()
|
||||||
if path.endswith('/logs'):
|
|
||||||
return _create_response_log(client, data)
|
|
||||||
return _create_response(client, data)
|
return _create_response(client, data)
|
||||||
|
|
||||||
get = _handle
|
get = _handle
|
||||||
@ -114,18 +111,6 @@ def _create_response(client, data):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _create_response_log(client, data):
|
|
||||||
"""Convert a response from client request."""
|
|
||||||
# Remove color codes
|
|
||||||
log = re.sub(r"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))", "", data.decode())
|
|
||||||
|
|
||||||
return web.Response(
|
|
||||||
text=log,
|
|
||||||
status=client.status,
|
|
||||||
content_type=CONTENT_TYPE_TEXT_PLAIN,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_timeout(path):
|
def _get_timeout(path):
|
||||||
"""Return timeout for a URL path."""
|
"""Return timeout for a URL path."""
|
||||||
if NO_TIMEOUT.match(path):
|
if NO_TIMEOUT.match(path):
|
||||||
|
@ -102,15 +102,15 @@ def test_forward_request_no_auth_for_logo(hassio_client):
|
|||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_forward_log_request(hassio_client):
|
def test_forward_log_request(hassio_client):
|
||||||
"""Test fetching normal log path."""
|
"""Test fetching normal log path doesn't remove ANSI color escape codes."""
|
||||||
response = MagicMock()
|
response = MagicMock()
|
||||||
response.read.return_value = mock_coro('data')
|
response.read.return_value = mock_coro('data')
|
||||||
|
|
||||||
with patch('homeassistant.components.hassio.HassIOView._command_proxy',
|
with patch('homeassistant.components.hassio.HassIOView._command_proxy',
|
||||||
Mock(return_value=mock_coro(response))), \
|
Mock(return_value=mock_coro(response))), \
|
||||||
patch('homeassistant.components.hassio.http.'
|
patch('homeassistant.components.hassio.http.'
|
||||||
'_create_response_log') as mresp:
|
'_create_response') as mresp:
|
||||||
mresp.return_value = 'response'
|
mresp.return_value = '\033[32mresponse\033[0m'
|
||||||
resp = yield from hassio_client.get('/api/hassio/beer/logs', headers={
|
resp = yield from hassio_client.get('/api/hassio/beer/logs', headers={
|
||||||
HTTP_HEADER_HA_AUTH: API_PASSWORD
|
HTTP_HEADER_HA_AUTH: API_PASSWORD
|
||||||
})
|
})
|
||||||
@ -118,7 +118,7 @@ def test_forward_log_request(hassio_client):
|
|||||||
# Check we got right response
|
# Check we got right response
|
||||||
assert resp.status == 200
|
assert resp.status == 200
|
||||||
body = yield from resp.text()
|
body = yield from resp.text()
|
||||||
assert body == 'response'
|
assert body == '\033[32mresponse\033[0m'
|
||||||
|
|
||||||
# Check we forwarded command
|
# Check we forwarded command
|
||||||
assert len(mresp.mock_calls) == 1
|
assert len(mresp.mock_calls) == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user